2016-11-29 2 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; 
    } 
} 

Dies ist MyAuthFilter.Federsicherheit Keine berechtigende Bean vom Typ AuthenticationSuccessHandler und AuthenticationFalureHandler

und hier meine feder 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> 

wenn ich versuche, meine App zu starten ich für eine Ausnahme bekam

Keine Warte Bohnen vom Typ AuthenticationSuccessHandler

und gleichen Fehler für FailureHandler . Ich würde jede Hilfe schätzen.

+0

Try '@ EnableWebSecurity' auf Ihre Sicherheits Config hinzuzufügen. Basierend auf dem Kontext glaube ich nicht, dass Sie Spring Boot verwenden, was normalerweise automatisch geschehen würde. –

Antwort

1

Ihr AuthenticationSuccessHandler wird nicht als Bean deklariert. Sie soll es als Bean erstellen und im Frühjahr security.xml in Tag über Attribut Authentifizierungs-Erfolg-Handler-ref = "nameOfYouSuccessHandlerBean"

So registrieren wird es aussehen werden: einige, wo in der Konfiguration Java-Dateien :

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

und im Frühjahr 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

Wie kann ich mySuccessHandler mit XML statt Konfiguration Java-Datei konfigurieren? – Papich

+0

Natürlich in Ihrer spring.xml – Sobik

Verwandte Themen