java – Spring安全性在尝试注销时返回302
发布时间:2020-12-15 02:14:01 所属栏目:Java 来源:网络整理
导读:我使用 Spring安全性(4.0.2.RELEASE)来保护我的应用程序. 我可以正常登录并且我的身份验证的URL受到保护,但是当我尝试注销时,我会不断获得302 POST响应,然后重定向到我配置的failureUrl(“/ cms / login?error”). 这是我的WebSecurityConfig类 @Configurat
我使用
Spring安全性(4.0.2.RELEASE)来保护我的应用程序.
我可以正常登录并且我的身份验证的URL受到保护,但是当我尝试注销时,我会不断获得302 POST响应,然后重定向到我配置的failureUrl(“/ cms / login?error”). 这是我的WebSecurityConfig类 @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/*").permitAll() .antMatchers("/cms/*").authenticated() .antMatchers("/cms/*/*").authenticated() .antMatchers("/cms/*/*/*").authenticated().and() .formLogin() .loginPage("/cms/login") .defaultSuccessUrl("/cms/login?success") .failureUrl("/cms/login?error") .permitAll().and() .logout() .logoutUrl("/cms/login?logout") .logoutSuccessUrl("/cms/login") .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("u") .password("p") .roles("USER"); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } 这是我的登录控制器: @Slf4j @Controller @RequestMapping(value = {"/cms","/cms/login"}) public class CmsLoginController extends CmsBaseController { @RequestMapping public ModelAndView handleLogin(HttpServletResponse request,Model model,@RequestParam(value = LOGIN_SUCCESS,required = false) String success,@RequestParam(value = LOGIN_ERROR,required = false) String error,@RequestParam(value = LOGOUT,required = false) String logout) { try { if (success != null) { setLoggedIn(true); request.sendRedirect(XXXXX); } if (error != null) { model.addAttribute(LOGIN_ERROR,"Invalid username and password!"); } if (logout != null) { model.addAttribute(LOGOUT,"You've been logged out successfully."); setLoggedIn(false); } return new ModelAndView(CMS_CONTEXT + LOGIN_URL); } catch(Exception e) { setLoggedIn(false); log.error(e.toString(),e); return new ModelAndView(ERROR_VIEW_NAME); } } } 为了记录,我最初得到的注销功能正常,但我必须介绍一些不幸的破坏它的变化…… 有什么想法吗? 解决方法
我认为问题是CSRF过滤器.在Spring Security 4中,默认情况下启用CSRF预防,每个POST请求都需要CSRF令牌.
如果您从以前的Spring版本迁移了代码,则很有可能出现问题. 要快速测试,请在配置中添加http.csrf().disable.如果它以这种方式工作,那么转回CSRF保护并以某种方式将CSRF令牌添加到POST注销请求中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |