2015-12-11 12 views
5

के लिए मैं एक कस्टम उपयोगकर्ता नाम पासवर्ड प्रमाणीकरण फ़िल्टर बनाने की कोशिश कर रहा हूं क्योंकि मुझे दो अलग-अलग स्रोतों से पासवर्ड सत्यापित करने की आवश्यकता है। मैं वसंत बूट 1.2.1 और जावा विन्यास का उपयोग कर रहा हूँ। तैनाती करते समय मुझे मिली त्रुटिस्प्रिंग सुरक्षा प्रमाणीकरण प्रबंधक निर्दिष्ट किया जाना चाहिए - कस्टम फ़िल्टर

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 

मुझे यकीन नहीं है कि मैं क्या खो रहा हूं। मैं अपने सुरक्षा कॉन्फ़िगर में इस फ़िल्टर के लिए प्रमाणीकरण प्रबंधक सेट करने का प्रयास कर रहा हूं। मेरे कोड

मेरी फिल्टर की तरह दिखता है:

@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; 
    } 
} 

मेरी सुरक्षा config:

@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

documentationAbstractAuthenticationProcessingFilter कक्षा के लिए कहता है कि आपको AuthenticationManager सेट करना होगा।

मैं तुम्हें अपने CustomUsernamePasswordAuthenticationFilter वर्ग के अंदर निम्नलिखित कोड जोड़ने की कोशिश कर सुझाव देते हैं:

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

मैं वास्तव में त्रुटि पिछले मिला। मुझे बस अपने कस्टम फ़िल्टर से @Component एनोटेशन को हटाना पड़ा।

+0

कमाल। आपका बहुत बहुत धन्यवाद! –

संबंधित मुद्दे