2015-02-10 64 views
4

我的问题是,我想有两个身份验证提供春天启动的安全性 - 多个身份验证提供

BEFORE: 我有我的UserDetailServiceImpl,我验证了用户对DB(不知道供应商是)用户根据数据库中的数据得到了作用

NOW: 我已经使用ActiveDirectoryLdapAuthentication提供如下

@Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     super.configure(auth); 
     auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService); 
    } 

    @Bean 
    public AuthenticationManager authenticationManager() { 
     return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider())); 
    } 
    @Bean 
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() { 
     MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN); 
     provider.setConvertSubErrorCodesToExceptions(true); 
     provider.setUseAuthenticationRequestCredentials(true); 

     return provider; 
    } 

我做了它的工作,所以我能够进行身份验证。

问题:

  1. 我现在无法再与数据库用户登录,现在只有LDAP。
  2. UserDetailsS​​ervice未使用,因此用户拥有哪个角色?
  3. 有没有办法如何添加一些默认角色到LDAP认证用户?

那么如何启用这两个提供商? 如何向用户添加角色,通过LDAP auth.provider进行身份验证?

我也很欣赏“大图片”描述了这里发生了什么(引擎盖下)。这里使用的每个类的作用是什么,它真的不清楚它是如何工作的(AuthenticationManager,AuthenticationManagerBuilder,AuthenticationProvider等)。也许它只是被自动配置隐藏等等,但是即使直接看Spring Security也不会让我任何智慧。

感谢任何提示

UPDATE 此代码似乎是正确

工作
@Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService); 
    } 

    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() { 
     MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN); 
     provider.setConvertSubErrorCodesToExceptions(true); 
     provider.setUseAuthenticationRequestCredentials(true); 
     provider.setAuthoritiesMapper(new SimpleAuthorityMapper()); 
     return provider; 
    } 

回答

1

问题太多!

这两个提供程序都已启用,因为您将它们都添加到AuthenticationManagerBuilder。但是你将它们添加到同一个过滤器链中,并且两者都接受相同种类的Authentication作为输入,所以它们中的一个总是掩盖另一个。

标准LdapAuthenticationProviderauthoritiesMapper,你可以用它来从目录条目当局映射到你的用户(通常是做了使用目录组的方块,例如见sample)。我想如果你的目录不包含组,你可以让所有的用户拥有相同的权限或者一些自定义的映射器。

AuthenticationManager类型和AuthenticationProvider一脸狐疑冗余(也可能是有害的,因为它们是全球性的,所配置的AuthenticationManagerBuilder单个过滤器链)的0​​。我怀疑你完全需要AuthenticationManager方法,另一个不需要公开或@Bean(可能)。

+0

这是否意味着我无法一次对两个提供者进行身份验证?在数据库中拥有超级用户,如果LDAP关闭等能够登录。AuthoritiesMapper可能会有所帮助,我们也可能在AD中有一些组,我会检查。明天会尝试你的建议。现在感谢。 – Zveratko 2015-02-10 20:40:04

+0

如果没有'@Bean public AuthenticationManager authenticationManager()'它完全停止工作: -/ – Zveratko 2015-02-11 07:24:35

+0

有两个AuthenticationManagerBuilder被创建,我认为应该只有一个。我不明白。 – Zveratko 2015-02-11 08:35:03