2017-01-03 128 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的时候