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

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

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

我春天的安全性:春季安全的AuthenticationManager必須指定

<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="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> 
     <authentication-provider> 
      <user-service> 
       <user name="ars" password="1234" authorities="ROLE_USER" /> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 

我試圖加我的自定義過濾器在春天的安全性,但在啓動時我得到的AuthenticationManager必須指定一個錯誤。有人可以看看嗎?

回答

1

嘗試添加@AutowiredAuthenticationManager

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

UPDATE

二傳手添加alias="authenticationManager"爲您驗證管理

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