[英]Spring Security 4 with custom provider and handlers not caching the url before authentication
我已經在Spring Security(v4.0.1)中配置了一個自定義身份驗證提供程序,一個成功處理程序和一個失敗處理程序。 使用默認的URL時,顯示登錄頁面后,將用戶重定向到先前請求的URL。 但是,我在實現自己的行為時丟失了該行為,因此我試圖恢復它。
基本上,現在,每次登錄時我都會被重定向到主頁,甚至我已經登錄了登錄頁面以嘗試獲取其他資源(在我的情況下為/ web / users )。 這就是我現在的配置:
@Configuration 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
的問題是requestCache
在SavedRequestAwareAuthenticationSuccessHandler#onAuthenticationSuccess
HttpSessionRequestCache#saveRequest
方法中始終為當前請求返回null
,這是因為HttpSessionRequestCache#saveRequest
方法在重定向到登錄頁面之前與我的傳入請求不匹配。
具體來說,Spring使用的HttpSessionRequestCache
是一個AndRequestMatcher
,它丟棄了我所有的傳入請求。 我希望它使用AnyRequestMatcher
,但不知道如何告訴Spring。
更新
在HttpSessionRequestCache#setRequestMatcher
設置一個斷點,這是Spring Security設置斷點的具體點:
但是,我不知道如何為我的案例設置自定義請求緩存配置器! 難道沒有更簡單的方法嗎?
更新2
我現在發現此問題僅在使用Firefox而不是在Chrome或Internet Explorer上發生。
我當前使用的Firefox瀏覽器實際上是一個問題。 即使我刪除了所有導航和緩存數據,它仍然會發生,但是對於其他瀏覽器(如Chrome或IE)卻不會發生,甚至對於其他FF瀏覽器也是如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.