2015-12-11 20 views
5

İki farklı kaynaktan parola doğrulaması gerektiğinden özel bir kullanıcı adı parola kimlik doğrulama filtresi oluşturmaya çalışıyorum. Spring Boot 1.2.1 ve Java yapılandırmasını kullanıyorum. Dağıtırken karşılaştığım hata, eksik olduğumdan emin değilim. SecurityConfig'imde bu filtrenin authenticationManager'ını ayarlamaya çalışıyorum. KodumSpring Security authenticationmanager belirtilmelidir - özel filtre için

benim filtresi gibi görünür:

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

benim güvenlik yapılandırma:

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

kimse bir bakabilir miyiz? onunla ne olduğundan emin değilim.

cevap

16

sınıfı için documentation sınıfı, bir AuthenticationManager belirtmeniz gerektiğini bildirir.

ben size CustomUsernamePasswordAuthenticationFilter sınıf içinde aşağıdaki kod ekleyerek tavsiye ederiz: Aslında hata geçmiş var

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

. Özel filtremden @Component ek açıklamasını kaldırmam gerekiyordu.

+0

Harika. Çok teşekkürler! –

İlgili konular