Spring security 自定义登录页面,登录后仍然跳转到登录页面 死循环的问题

Spring security 自定义登录页面,登录后仍然跳转到登录页面 死循环的问题

搭建个人博客项目时遇到的bug,查阅多方资料无解,在改了四个小时后发现是csrf关闭失败的原因

版本信息
springboot 2.2.6
springsecurity 5

  • 错误的示例
    protected void configure(HttpSecurity http) throws Exception {
     
        //首页所有人都可以访问
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/admin/**").hasRole("admin");
        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/doLogin");
        http.csrf().disable();
    }
  • 改正:将关闭csrf功能写在最上边
    protected void configure(HttpSecurity http) throws Exception {
     
    	http.csrf().disable();
        //首页所有人都可以访问
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/admin/**").hasRole("admin");
        http.formLogin().loginPage("/toLogin").loginProcessingUrl("/doLogin");
    }

可能是版本的问题!低版本就没有这个bug,因为我之前看的是其他老师的教程,错误的示例是没有问题的,但是我用的最新版本springsecurity5就出现了这个bug

但是无论什么版本,也要将csrf关闭才能生效哦!

你可能感兴趣的:(java,spring,spring,boot)