我正面临一个非常烦人的问题,我不知道如何在此问题上取得进展。我正在登录我的应用程序,导航正常,登录/注销正常。但是,当我想在某处保存内容时,我将直接重定向到LoginPage。在日志中,仅执行我的LoginController。当我再次登录时,然后执行我的POST请求,数据就被保护了。我在应用程序中到处都有这种行为,我认为这是Spring Security上的一个配置问题。任何帮助将非常欢迎。
我的Spring安全配置:
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
final FormLoginConfigurerEnhancer enhancer;
final AuthenticationSuccessHandler authenticationSuccessHandler;
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/webjars/**", "/css/**", "/js/**", "/images/**");
}
@Override
public void configure(HttpSecurity http) throws Exception {
enhancer.addRecaptchaSupport(http.formLogin())
.loginPage("/login")
.successHandler(authenticationSuccessHandler)
.and()
.csrf().disable()
.authorizeRequests()
.antMatchers("/", "/login", "/home", "/registration").permitAll()
.anyRequest().authenticated()
;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth, UserDetailsService userDetailsService) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
似乎当我保存(发布)某些内容时,那时我的用户是匿名的。当我按下“保存”按钮时,这是日志的一部分:
2019-06-23 19:14:22.373调试21576 --- [io-8080-exec-10] o.s.s.w.a.i.FilterSecurityInterceptor:先前已认证:org.springframework.security.authentication.AnonymousAuthenticationToken@bffd4a17:委托人:onymousUser;凭证:[受保护];已验证:true;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@b364:RemoteIpAddress:0:0:0:0:0:0:0:1; SessionId:null;授予的权限:ROLE_ANONYMOUS 2019-06-23 19:14:22.373调试21576 --- [io-8080-exec-10] ossaccess.vote.AffirmativeBased:选民:org.springframework.security.web.access.expression.WebExpressionVoter@47fca1e0,返回:-1 2019-06-23 19:14:22.373调试21576 --- [nio-8080-exec-6] o.s.s.w.a.ExceptionTranslationFilter:访问被拒绝(用户是匿名的);重定向到身份验证入口点
我还发现,请求会话ID已变为无效:
2019-06-23 19:22:29.874调试21576 --- [nio-8080-exec-9] o.s.security.web.FilterChainProxy:/ property / edit在附加过滤器链中的11的位置9;触发过滤器:“ SessionManagementFilter” 2019-06-23 19:22:29.874调试21576 --- [nio-8080-exec-9] o.s.s.w.session.SessionManagementFilter:请求的会话ID 9FBCB80752CBCFC16D704D6C3BB62E05无效。
我正在使用最新的Spring Boot 2.1.6版本。
答案 0 :(得分:0)
我找到了解决问题的方法。但这是一个变通办法,现在不知道为什么会起作用,而以前却不知道。因此,我在这里写下了我是如何工作的答案,但是仍然欢迎任何背景知识或更好的解决方案。
主要问题是,当我发送帖子保存内容时,Spring Security退出了。因此,我在Spring Security方法中使用了.logout
。
我的注销URL应该类似于/login?logout
。 Spring Boot的默认URL为/logout
。但是我必须手动设置logoutUrl才能使我的保存功能正常工作。所以现在我将其定义为.logoutUrl("/login?logout")
,然后保存就可以了。 (当我删除它并保存一些东西时,我就注销了!)
现在,我的Spring Security的配置如下:
@Override
public void configure(HttpSecurity http) throws Exception {
enhancer.addRecaptchaSupport(http.formLogin())
.loginPage("/login")
.successHandler(authenticationSuccessHandler)
.and()
.csrf().disable()
.logout().logoutUrl("/login?logout")
.and()
.authorizeRequests()
.antMatchers("/", "/login", "/home", "/registration").permitAll()
.anyRequest().authenticated()
;
}
我仅使用.logoutUrl("/login?logout")
配置了Spring Security,以使我的应用程序能够保存而不注销。当我想注销时,我被重定向到错误的spring默认URL /logout
。因此,我现在在LoginController
中定义了一个方法来重定向正确的URL。
@PostMapping("/logout")
public ModelAndView logout() {
return new ModelAndView("redirect:/login?logout");
}
这一切都非常丑陋和令人困惑,但是我真的不知道这里发生了什么。请把光放入黑暗中。非常感谢