Spring AuthenticationFailureHandler和WebSecurityConfigurerAdapter loginPage()

编辑:解决了。 在这篇文章后看到我的评论

我目前正在使用Spring-Security实现Web应用程序。 我已经实现了一个自定义AuthenticationFailureHandler ,它检查用户是否经常尝试使用错误的凭据登录(并阻止他几分钟)。 但正常的登录失败应该将用户重定向到登录页面,参数错误( /login?error )。 此页面显示错误消息,例如“您输入的密码错误”

AutenticationFailureHandler看起来像这样(没有无代码的代码)

 public class CustomAuthenticationHandler implements AuthenticationFailureHandler { // Some variables @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { // some logic here.. request.setAttribute("param", "error"); response.sendRedirect("/login?error"); } 

我的WebApplicationSecurity类如下所示:

 @Configuration @EnableWebMvcSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired CustomAuthenticationHandler customAuthenticationHandler; @Override protected void configure(HttpSecurity http) throws Exception { http.formLogin() .loginPage("/login") .permitAll() .failureHandler(customAuthenticationHandler) .and() .logout() .permitAll(); http.authorizeRequests() .antMatchers("/css/**", "/img/**", "/js/**") .permitAll() .anyRequest() .authenticated(); http .csrf() .disable(); } @Bean CustomAuthenticationHandler authenticationHandler() { return new CustomAuthenticationHandler(); } @Configuration protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter { @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("*******") .password("*******") .roles("USER"); } } } 

现在的问题是AuthenticationFailureHandler重定向到/login?error但(我不知道为什么)另一个重定向到/login

你能帮我解决一下我的问题吗?

好吧,我通过在http.authorizeRequests().antMatchers("/css/**", "/img/**", "/js/**")添加“/ login **”解决了这个问题http.authorizeRequests().antMatchers("/css/**", "/img/**", "/js/**")