Spring 安全性 4,自定义提供程序和处理程序在身份验证前不缓存 url
Spring Security 4 with custom provider and handlers not caching the url before authentication
我在 Spring 安全性 (v4.0.1) 中配置了一个自定义身份验证提供程序、一个成功处理程序和一个失败处理程序。使用默认的时候,在显示登录页面后,用户被重定向到之前请求的url。但是,我在实施自己的行为时失去了这种行为,所以我试图恢复它。
基本上,现在,我每次登录时都会被重定向到主页,即使我访问了登录页面试图获取另一个资源(在我的例子中 /web/users)。这就是我现在的配置:
@配置bean(扩展WebSecurityConfigurerAdapter
)
@Override
protected void configure(HttpSecurity http) throws Exception {
http.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/web/logout"))
.invalidateHttpSession(true).logoutSuccessUrl("/web/login");
http.authorizeRequests()
.antMatchers("/javax.faces.resource/**")
.permitAll()
.antMatchers("/web/recovery").permitAll()
.antMatchers("/web/users").authenticated().anyRequest()
.authenticated().and().formLogin()
.failureHandler(failureHandler).loginPage("/web/login")
.loginProcessingUrl("/web/j_spring_security_check")
.successHandler(successHandler).permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth,
DataSource ds, PasswordEncoder pwdEncoder) throws Exception {
auth.authenticationProvider(authProvider);
}
自定义 AuthenticationSuccessHandler
public class SystemAuthenticationSuccessHandler extends
SavedRequestAwareAuthenticationSuccessHandler {
private IUserService service;
public SystemAuthenticationSuccessHandler(IUserService service) {
this.service = service;
setDefaultTargetUrl("/web/home");
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
String username = request.getParameter("username");
User user = service.findByIdOrEmail(username, username);
if (user != null) {
service.saveLoginSuccess(user.getId());
}
//Call the parent method to manage the successful authentication
super.onAuthenticationSuccess(request, response, authentication);
}
基本上,我遇到的问题是 requestCache
总是返回 null
,因为 SavedRequestAwareAuthenticationSuccessHandler#onAuthenticationSuccess
method and that's because the HttpSessionRequestCache#saveRequest
方法中的当前请求在被重定向到登录页面之前与我的传入请求不匹配.
具体来说,HttpSessionRequestCache
Spring 使用的是 AndRequestMatcher
which discards all my incoming requests. I want it to use the AnyRequestMatcher
,但不知道如何告诉 Spring。
更新
在 HttpSessionRequestCache#setRequestMatcher
中设置了一个断点,这是 Spring 安全设置它的具体点:
但是,我不知道如何为我的案例设置自定义请求缓存配置器!没有更简单的方法吗?
更新 2
我现在发现这个问题只发生在使用 Firefox 时,而不是 Chrome 或 Internet Explorer。
其实是我目前使用的firefox浏览器的问题。即使我删除了所有导航和缓存数据,它仍然会发生,但对于 Chrome 或 IE 等其他浏览器,甚至其他 FF 浏览器都不会。
我在 Spring 安全性 (v4.0.1) 中配置了一个自定义身份验证提供程序、一个成功处理程序和一个失败处理程序。使用默认的时候,在显示登录页面后,用户被重定向到之前请求的url。但是,我在实施自己的行为时失去了这种行为,所以我试图恢复它。
基本上,现在,我每次登录时都会被重定向到主页,即使我访问了登录页面试图获取另一个资源(在我的例子中 /web/users)。这就是我现在的配置:
@配置bean(扩展WebSecurityConfigurerAdapter
)
@Override
protected void configure(HttpSecurity http) throws Exception {
http.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/web/logout"))
.invalidateHttpSession(true).logoutSuccessUrl("/web/login");
http.authorizeRequests()
.antMatchers("/javax.faces.resource/**")
.permitAll()
.antMatchers("/web/recovery").permitAll()
.antMatchers("/web/users").authenticated().anyRequest()
.authenticated().and().formLogin()
.failureHandler(failureHandler).loginPage("/web/login")
.loginProcessingUrl("/web/j_spring_security_check")
.successHandler(successHandler).permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth,
DataSource ds, PasswordEncoder pwdEncoder) throws Exception {
auth.authenticationProvider(authProvider);
}
自定义 AuthenticationSuccessHandler
public class SystemAuthenticationSuccessHandler extends
SavedRequestAwareAuthenticationSuccessHandler {
private IUserService service;
public SystemAuthenticationSuccessHandler(IUserService service) {
this.service = service;
setDefaultTargetUrl("/web/home");
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
String username = request.getParameter("username");
User user = service.findByIdOrEmail(username, username);
if (user != null) {
service.saveLoginSuccess(user.getId());
}
//Call the parent method to manage the successful authentication
super.onAuthenticationSuccess(request, response, authentication);
}
基本上,我遇到的问题是 requestCache
总是返回 null
,因为 SavedRequestAwareAuthenticationSuccessHandler#onAuthenticationSuccess
method and that's because the HttpSessionRequestCache#saveRequest
方法中的当前请求在被重定向到登录页面之前与我的传入请求不匹配.
具体来说,HttpSessionRequestCache
Spring 使用的是 AndRequestMatcher
which discards all my incoming requests. I want it to use the AnyRequestMatcher
,但不知道如何告诉 Spring。
更新
在 HttpSessionRequestCache#setRequestMatcher
中设置了一个断点,这是 Spring 安全设置它的具体点:
但是,我不知道如何为我的案例设置自定义请求缓存配置器!没有更简单的方法吗?
更新 2
我现在发现这个问题只发生在使用 Firefox 时,而不是 Chrome 或 Internet Explorer。
其实是我目前使用的firefox浏览器的问题。即使我删除了所有导航和缓存数据,它仍然会发生,但对于 Chrome 或 IE 等其他浏览器,甚至其他 FF 浏览器都不会。