2015-12-11 17 views
5

私は2つの異なるソースからパスワードを検証する必要があるので、カスタムユーザー名パスワード認証フィルターを作成しようとしています。私はSpringブート1.2.1とJava設定を使用しています。デプロイ時のエラーはSpring Security認証マネージャーを指定する必要があります - カスタムフィルター

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customUsernamePasswordAuthenticationFilter' defined in file [/Users/rjmilitante/Documents/eclipse-workspace/login-service/bin/com/elsevier/eols/loginservice/CustomUsernamePasswordAuthenticationFilter.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: authenticationManager must be specified 
… 
Caused by: java.lang.IllegalArgumentException: authenticationManager must be specified 

私には分かりません。私は私のSecurityConfigでこのフィルタのauthenticationManagerを設定しようとしています。

@Component 
public class CustomUsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter { 

    public CustomUsernamePasswordAuthenticationFilter(RequestMatcher requiresAuthenticationRequestMatcher) { 
     super(requiresAuthenticationRequestMatcher); 
     // TODO Auto-generated constructor stub 
    } 

    public CustomUsernamePasswordAuthenticationFilter() { 
     super(new AntPathRequestMatcher("/login","POST")); 
     // TODO Auto-generated constructor stub 
    } 

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) 
      throws AuthenticationException { 
     // String dbValue = request.getParameter("dbParam"); 
     // request.getSession().setAttribute("dbValue", dbValue); 
     System.out.println("attempting to authentificate"); 
     while (request.getAttributeNames().hasMoreElements()) { 
      String e = (String) request.getAttributeNames().nextElement(); 
      System.out.println("param name : " + e + " and param value : " + request.getAttribute(e)); 
     } 
     return null; 
    } 
} 

私のセキュリティ設定:

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter{ 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 

    @Bean(name="loginService") 
    public LoginService loginService(){ 
     return new LoginServiceImpl(); 
    } 

    @Bean(name="myAuthenticationManager") 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Bean 
    CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter() throws Exception { 
     CustomUsernamePasswordAuthenticationFilter customUsernamePasswordAuthenticationFilter = new CustomUsernamePasswordAuthenticationFilter(); 
     customUsernamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManagerBean()); 
     return customUsernamePasswordAuthenticationFilter; 
    } 


    @Autowired 
    private myAuthenticationProvider myAuthenticationProvider; 

    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable() 
     .authorizeRequests() 
     .anyRequest().authenticated() 
     .and() 
     /*.addFilterBefore(customUsernamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)*/; 

    } 

    public void configure(AuthenticationManagerBuilder auth) throws Exception { 

     auth.authenticationProvider(myAuthenticationProvider); 
     } 
} 

誰でも見ることができます私のコードは

私のフィルタのように見えますか?何が起きているのか分かりません。

答えて

16

AbstractAuthenticationProcessingFilterクラスのdocumentationは、AuthenticationManagerを設定する必要があります。

私はあなたのCustomUsernamePasswordAuthenticationFilterクラス内に次のコードを追加してみてお勧め:

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

私は実際にエラーを過ぎました。私はちょうど私のカスタムフィルタから@Component注釈を削除しなければなりませんでした。

+0

素晴らしい。どうもありがとう! –

関連する問題