对指定资源进行授权访问

这个功能,整体是在SpringBoot集成框架下完成的。所以需要创建SpringBoot项目。

这个功能是利用SpringSecurity提供的。对指定的资源进行授权访问,其他资源只有登录才能进行访问。但是需要先在pom.xml文件中导入对应的SpringSecurity的依赖。如果需要使用自定义的登录或注册页面,或其他页面,需要导入Thymeleaf依赖。

<dependencies>
    <!-- 这个是SpringSecurity依赖,授权认证的功能是由它提供的-->
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-security</artifactId> 
    </dependency>

    <!-- 接下来需要导入Thymeleaf的依赖,用来访问自定义的登录注册页面。-->

     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>
</dependencies>

静态资源放在resources目录下的static目录中。比如需要在html中导入的js文件。bootstrap的配置文件等等。

自定义显示的资源放在templates中。一般是需要动态显示的页面。前端功能了解不多,这里不过多介绍。

接下来在项目中创security包。在security包下创建SecurityConfig安全配置类

package 包名.security

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration //作为Spring的配置信息类
// 该注解用来开启权限管理功能
@EnableGlobalMethodSecurity(prePostEnabled = true)
/*
 *该类继承 WebSecurityConfigurerAdapter 类,重写configure方法参数为HttpSecurity 的方法
 *指定对哪些请求进行授权放行。
*/ 
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        /*
        * .csrf().disable()关闭跨域攻击防御功能
        * .authorizeRequests() 个人理解就是限制所有访问的功能
        * .antMatchers()括号里面都是一些对访问放行,就是白名单
        * .permitAll()指定白名单里面的的请求,任何人都可以访问
        * .anyRequest().authenticated()除了上面请求之外,其他请求需要登录才能访问
        * .and().formLogin()需要登录才能访问
        * .loginPage("/login.html")设置指定的登录页面
        * .failureUrl("/login.html?error") 如果登录失败,应该显示的页面
        * .loginProcessingUrl("/login")这个URL是Spring-Security过滤器自动进行登录处理的URL
        * 就是处理登录的请求
        * .defaultSuccessUrl("/index.html") 登录成功之后需要重定向的页面
        * .and().logout()设置登出功能
        * .logoutUrl("/logout")登出的请求
        * .logoutSuccessUrl("/login.html?logout");登出成功之后需要访问的页面 
        */
        http.csrf().disable().authorizeRequests()
                .antMatchers(
                        "/login.html",
                        "/register.html",
                        "/register",
                        //其实这里可以加一些静态资源路径,比如JS、CSS、图片路径等等
                        ).permitAll()
                .anyRequest().authenticated()
                .and().formLogin()
                .loginPage("/login.html")
                .failureUrl("/login.html?error")
                .loginProcessingUrl("/login")
                .defaultSuccessUrl("/index.html")
                .and().logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login.html?logout");
    }
}

对于登录和注册的功能,这个授权访问的地方总是记得很模糊。怕以后用到这个功能的时候查官网,想要把这些拼凑起来,有点不容易。自己记录下。没准以后能用到。

要说是转载,其实也不算。这个东西在学习的时候以前讲过。只不过把曾经的笔记拿出来记录在这里。因为笔记太多,需要用的时候再找不太方便。就摘出来一片放在这。也算是原创吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值