2016-11-29 51 views
-1

這是我的彈簧security.xml文件:春季安全說明「壞憑據」的錯誤,而不是重定向

<http auto-config="true" use-expressions="true"> 
     <intercept-url pattern="/courses*" access="hasRole('ROLE_USER')" /> 
     <custom-filter before="FORM_LOGIN_FILTER" ref="MyAuthFilter" /> 
     <form-login 
      login-page="/login" 
      default-target-url="/courses" 
      authentication-failure-url="/login?error" 
      username-parameter="loginField" 
      password-parameter="passwordField" /> 
     <csrf disabled="true" /> 
    </http> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider> 
      <user-service> 
       <user name="ars" password="1234" authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 

這裏是MyAuthFilter:

@Component("MyAuthFilter") 
public class MyAuthFilter extends UsernamePasswordAuthenticationFilter { 

    @Autowired 
    @Qualifier("authenticationManager") 
    @Override 
    public void setAuthenticationManager(AuthenticationManager authenticationManager) { 
     super.setAuthenticationManager(authenticationManager); 
    } 

    @Override 
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) 
      throws AuthenticationException { 

     System.out.println("running my own version of UsernmePasswordFilter ... "); 

     LoginForm loginForm = new LoginForm(); 
     loginForm.setUsername(request.getParameter("login")); 
     loginForm.setPassword(request.getParameter("password")); 
     request.setAttribute("error", 3); 
     System.out.println("login : " + loginForm.getUsername()); 
     UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(loginForm.getUsername(), loginForm.getPassword()); 
     setDetails(request, authRequest); 
     Authentication authResult = this.getAuthenticationManager().authenticate(authRequest); 

     return authResult; 
    } 
} 

當我輸入錯誤的登錄名或密碼就說明「錯誤的憑據」錯誤,而不是重定向到登錄頁面。沒有自定義過濾器,它工作正常。 我只想檢查登錄名\密碼有什麼問題,並設置「錯誤」可以修改,我用登錄表單顯示具體的錯誤,如「空通」等。

我需要做一個登錄頁面女巫顯示混凝土錯誤,如「空傳遞\空登錄\兩個空\錯誤的登錄或傳遞」。如果有人能夠通過示例或指導進行驗證,我將會非常高興。

回答

2

在你的過濾器

@Autowired 
    @Qualifier("authenticationManager") 
    @Override 
    public void setAuthenticationManager(AuthenticationManager authenticationManager, AuthenticationSuccessHandler successHandler, AuthenticationFailureHandler failureHandler) { 
     super.setAuthenticationManager(authenticationManager); 
     this.setAuthenticationSuccessHandler(successHandler); 
     this.setAuthenticationFailureHandler(failureHandler); 
    } 
+0

型AuthenticationSuccessHandler的沒有合格的bean定義成功和失敗處理機

@Bean public AuthenticationSuccessHandler getSuccessHandler(){ SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler(); handler.setDefaultTargetUrl("/login.html"); return handler; } @Bean public AuthenticationFailureHandler getFailureHandler(){ SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler(); handler.setDefaultFailureUrl("/login.html"); return handler; } 

。 – CortezDaCardinal

+0

你需要像上面那樣定義那些bean – kuhajeyan

+0

我是這樣做的,它顯示錯誤 'org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有提供'org.springframework.security.web.authentication.AuthenticationSuccessHandler'類型的限定bean可用:預計至少有1個符合自動導向候選人的bean。依賴註釋:{} – CortezDaCardinal