【问题标题】:Apache shiro LDAP multiple OUsApache shiro LDAP multiple OUs
【发布时间】:2012-03-05 15:05:26
【问题描述】:

我正在尝试配置 apache shiro 以使用我们的 LDAP 服务器进行身份验证。我对LDAP不是很熟悉,请原谅我的无知!

在 shiro.ini 中使用以下选项可以正常工作(用户已通过身份验证):

ldapRealm.userDnTemplate = uid={0},ou=users,dc=mycompany,dc=com

但是,我们公司有多个组织单位 (ou)。如何使 ou 参数采用多个值?我可以用这个吗

ldapRealm.userDnTemplate = uid={0},ou=*,dc=mycompany,dc=com

我只想尝试所有组织单位,直到登录成功。

像这样添加具有不同 ou 的额外 ldap 领域怎么样:

#ldapRealm1 ldapRealm1 = org.apache.shiro.realm.ldap.JndiLdapRealm ldapRealm1.userDnTemplate = uid={0},ou=users1,dc=mycompany,dc=com ldapRealm1.contextFactory.url = ldap://test.com:389 #ldapRealm2 ldapRealm2 = org.apache.shiro.realm.ldap.JndiLdapRealm ldapRealm2.userDnTemplate = uid={0},ou=users2,dc=mycompany,dc=com ldapRealm2.contextFactory.url = ldap://test.com:389 #ldapRealm3 ldapRealm3 = org.apache.shiro.realm.ldap.JndiLdapRealm ldapRealm3.userDnTemplate = uid={0},ou=users3,dc=mycompany,dc=com ldapRealm3.contextFactory.url = ldap://test.com:389

这行得通吗?

还可以在登录页面中添加一个下拉列表以允许用户选择他们的组织单位并将其作为参数传递给 ldapRealm 吗?我应该如何处理?

TIA, 塞拉芬

【问题讨论】:

    标签: java ldap shiro


    【解决方案1】:

    多个领域可以正常工作。您只需要创建一个 AuthenticationToken 的子接口,它还指定您要定位的组织单位。

    然后您可以创建 LdapRealm 的子类并更改 supports() 方法以返回 true IFF AuthenticationToken 反映了目标组织单位。例如:

    LdapAuthenticationToken extends AuthenticationToken {
        String getOrganizationalUnit();
    }
    
    LdapUsernamePasswordToken extends UsernamePasswordToken 
        implements LdapAuthenticationToken  {
        private String organizationalUnit; //add getter/setter
    }
    
    MyLdapRealm extends LdapRealm {
        private String organizationalUnit; //add getter/setter
        @Override
        public boolean supports(AuthenticationToken token) {
            return token != null && token instanceof LdapAuthenticationToken &&
                this.organizationalUnit.equals(
                    ((LdapAuthenticationToken)token).getOrganizationalUnit());
        }
    
        @Override
        protected AuthenticationInfo doGetAuthenticatinoInfo(AuthenticationToken token) {
            LdapAuthenticationToken ldapToken = (LdapAuthenticationToken)token;
            //get the OU here, and do whatever you want with it.
        }
    }
    

    如果您有多个领域,请注意每个领域可能都有自己的 LDAP 连接池,它可能不如单个共享连接池高效。

    如果您想拥有一个连接池,则需要使用一个领域并基于 OrganizationalUnit 手动制定查询。 LdapAuthenticationToken 在这种情况下也会很有帮助。

    【讨论】:

      猜你喜欢
      • 2017-02-01
      • 2014-10-20
      • 2015-01-04
      • 2018-07-31
      • 2014-07-26
      • 2016-12-30
      • 2017-09-20
      • 2016-05-07
      • 2017-01-04
      相关资源
      最近更新 更多