2016-11-29 99 views
0
@Component("MyAuthFilter") 
     public class MyAuthFilter extends UsernamePasswordAuthenticationFilter { 

    private int errCode = 0; 

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

    @Override 
    public AuthenticationFailureHandler getFailureHandler() { 
     SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler(); 
     handler.setDefaultFailureUrl("/login?error=" + errCode); 
     return handler; 
    } 

    @Override 
    public AuthenticationSuccessHandler getSuccessHandler() { 
     SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler(); 
     handler.setDefaultTargetUrl("/courses"); 
     return handler; 
    } 

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

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

     String login = (String) request.getParameter("login"); 
     String password = (String) request.getParameter("password"); 
     errCode = validate(login,password);  
     UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(login, password); 
     // Allow subclasses to set the "details" property 
     setDetails(request, authRequest); 

     return this.getAuthenticationManager().authenticate(authRequest); 
    } 

    private int validate(String login,String password){ 

     if (login.isEmpty() && password.isEmpty()){ 
      return 4; 
     } 
     if (login.isEmpty() && !password.isEmpty()){ 
      return 2; 
     } 
      if (!login.isEmpty() && password.isEmpty()){ 
      return 3; 
     } 

     return 1; 
    } 
} 

這是MyAuthFilter。彈簧安全性沒有合格的bean類型AuthenticationSuccessHandler和AuthenticationFalureHandler

,在這裏我的彈簧security.xml文件

<beans:beans xmlns="http://www.springframework.org/schema/security" 
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.2.xsd"> 

    <http auto-config="false" 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" 
      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> 

</beans:beans> 

當我嘗試啓動我的應用程序,我得到一個例外

型AuthenticationSuccessHandler

和同樣的錯誤的不符合條件的豆爲FailureHandler 。我將不勝感激任何幫助。

+0

嘗試增加'@ EnableWebSecurity'您的安全配置。基於上下文,我不認爲你使用的是Spring Boot,通常這會自動處理。 –

回答

1

您的AuthenticationSuccessHandler未聲明爲bean。 你應該將其創建爲bean並通過屬性在標籤的彈簧security.xml文件登記 認證成功處理程序-REF =「nameOfYouSuccessHandlerBean」

所以這將是這樣的:在 配置java文件,其中的一些:

@Bean 
    public AuthenticationSuccessHandler mySuccessHandler() { 
     SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler(); 
     handler.setDefaultTargetUrl("/courses"); 
     return handler; 
    } 

和彈簧security.xml文件

<form-login 
      login-page="/login" 
      default-target-url="/courses" 
      authentication-failure-url="/login" 
      username-parameter="loginField" 
      authentication-success-handler-ref="mySuccessHandler"    
      password-parameter="passwordField" /> 
+0

我該如何配置mySuccessHandler與XML而不是配置Java文件? – Papich

+0

當然, <豆ID = 「org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler」 類= 「net.test.employee.dao.EmployeeDAOImpl」> <屬性名= 「defaultTargedUrl」 值=「/課程「/> in your spring.xml – Sobik