SpringSecurity(完整版)

前言:1.它是实现思想是AOP(配置类)

   2.作用:认证,授权(访问控制 )

   3.也可以用拦截器+原生代码实现,但过于冗余  

   4.SpringSecurity是依托与Spring的框架,几乎将所有webSecurity相关的功能全部封装,开发人员仅需使用即可,(约定大于配置)

1.主要的几个类

  WebSecurityConfigurerAdpter:自定义Security策略

  AuthenicationManagerBuilder:自定义认证策略

  @EndableWebSecurity:开启WebSecurity模式

 SpringSecurity主要目标是“认证”和“授权”(访问控制)

  认证(Authentication)

  授权(Authorization)

参考官网:https://spring.io/projects/spring-security

2.下面开始写demo实现它

  2.1导包

        <!--springSecurity-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!--thymeleaf整合SpringSecurity-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

        <!--thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

  

2.2导入静态资源

2.3路由

@Controller
public class RouterController {

    //主页
    @RequestMapping({"/","/index"})
    public String index(){

        return "index";
    }

    //去登陆页
    @RequestMapping("/toLogin")
    public String toLogin(){

        return "views/login";
    }

    //level1
    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id")int id){
        return "views/level1/"+id;
    }

    //level2
    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id")int id){
        return "views/level2/"+id;
    }

    //level3
    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id")int id){
        return "views/level3/"+id;
    }
}

2.4编写SpringSecurity配置类

//声明这是一个webSecurity配置类并且交给spring管理
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //授权(authorize)
    //链式编程,对应设计模式责任链设计模式
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页都能访问,功能页只有对应的有权限的人才能访问

        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限默认会到登陆界面(官方的登录页)
        //loginPage,自定义登录页(改成自己要请求的页面)
        //loginProcessingUrl,前端表单的提交地址(登陆认证)
        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/login");

        //注销,sucess要走的页面
        http.logout().logoutSuccessUrl("/");

        //开启记住我功能 本质:一个cookie的实现
        //页面注销时出现404,这是因为SpringBoot默认开启了 CSRF 防止网站攻击
        //
        //解决方法:将我们的注销请求方式改为 post 请求
        //或者我们可以在 configure(HttpSecurity http)方法中 关闭 csrf(会产生安全隐患)
        http.csrf().disable();
        http.rememberMe().rememberMeParameter("remember");


    }

总结:SpringBoot默认开启了CSRF(防止网站攻击),造成的后果是只能post方式提交,

解决方法:前端提交方式改成post,或者关闭CSRF

http.csrf().disable();//关闭CSRF
原文地址:https://www.cnblogs.com/CL-King/p/14124516.html