2017-01-03 132 views
1

多個身份驗證提供我與應用此安全設置:如何正確配置在春季啓動應用程序

問題是,我daoAutentication進行兩次,我想定。在日誌中我可以看到:

2017-01-03 10:29:18.106 DEBUG 2154 --- [[ACTIVE] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)'] .r.o.MyApplication$ApplicationSecurity : providers: [org.spring[email protected]4c46fcec, [email protected], org.spring[email protected]60516c4c] 

我不知道爲什麼有2個DaoAuthenticationProvider。當我編輯我這樣的配置:

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(kaasAuthenticationProvider()); 
    // auth.authenticationProvider(daoAuthenticationProvider()); 
} 

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

然後它工作正常。只有一個DaoAuthenticationProvider。問題是,我不知道爲什麼這個作品,所以我現在不想使用它,直到我也明白這一點的安全機制

UPDATE:

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider { 

    private Logger log = LoggerFactory.getLogger(CustomAuthenticationProvider.class); 

    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
     log.debug("Authentication: {}.", authentication); 
     ... 
     return new CustomAuthenticationToken(securityToken, authorities, 
       new CustomUser(login, "", true, true, true, true, authorities)); 
    } 
} 
+1

安置自己的CustomAuthenticationProvider代碼。 – Apollo

+0

爲什麼你需要這個提供商?這是一個複雜的,我不能粘貼代碼。我在這裏更新了骷髏 – hudi

回答

1

OK我發現這個問題。 This線程幫助我很多

我自動裝配的AuthenticationManager:

@Autowired 
private AuthenticationManager authenticationManager; 

沒有委託在紗線之上:

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

這原因是:

class InitializeUserDetailsManagerConfigurer 
     extends GlobalAuthenticationConfigurerAdapter { 
    @Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     if (auth.isConfigured()) { 
      return; 
     } 
     ... 
} 

之前執行
@Autowired 
public void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(customAuthenticationProvider()); 
    auth.authenticationProvider(daoAuthenticationProvider()); 
} 

的原因是什麼,爲什麼有一個額外的DaoAuthenticationProvider的時候