【问题标题】:Apache Shiro - authentication with attribute other than cn?Apache Shiro - 使用 cn 以外的属性进行身份验证?
【发布时间】:2014-05-29 11:57:56
【问题描述】:

我正在尝试在我们的项目中使用 Apache Shiro 来通过 Active Directory 领域进行身份验证和授权。当我使用用户 CN 属性进行身份验证时效果很好但是,我想使用另一个唯一属性进行登录。是否可以配置 shiro 这样做?

这是我的 shiro.ini 文件:

    [main]
    shiro.loginUrl = /login.jsp
    activeDirectoryRealm = org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm
    activeDirectoryRealm.systemUsername = admin
    activeDirectoryRealm.systemPassword = secret
    activeDirectoryRealm.searchBase = DC=company,DC=private
    activeDirectoryRealm.url = ldap://url:389

还有登录码:

    public void login(String uname, String pwd, boolean rememberMe) {
            Factory<SecurityManager> ldapFactory = new IniSecurityManagerFactory("classpath:shiro.ini");
            SecurityManager sManager = ldapFactory.getInstance();
            SecurityUtils.setSecurityManager(sManager);
            Subject currentUser = SecurityUtils.getSubject();
            if (!currentUser.isAuthenticated()) {
                uname = "CN=" + uname + ",OU=Users";
                UsernamePasswordToken token = new UsernamePasswordToken(uname, pwd);
                token.setRememberMe(rememberMe);
                try {
                    currentUser.login(token);
                } catch (UnknownAccountException ex) {
                    logger.info("Unknown user");
                } catch (IncorrectCredentialsException ex) {
                    logger.info("Incorrect credentials");
                } catch (LockedAccountException ex) {
                    logger.info("Account is locked");
                } catch (AuthenticationException ex) {
                    ex.printStackTrace();
                }
            }
        }

如果我换行

    uname = "CN=" + uname + ",OU=Users";

到下面

    uname = "myCustomAttribute=" + uname + ",OU=Users";

并尝试使用它登录,我得到了

    javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A9, comment: AcceptSecurityContext error, data 52e, v1db1 ]

感谢您的回答。

【问题讨论】:

    标签: authentication active-directory shiro


    【解决方案1】:

    好的,我已经通过创建自定义 MyJndiLdapRealm 来扩展 JndiLdapRealm 并覆盖方法来做到这一点

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){}
    

    但如果您有更好的解决方案,我仍然欢迎您回答。

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 2017-06-28
      • 1970-01-01
      • 2016-04-08
      • 2014-12-20
      • 2016-04-02
      • 2014-12-06
      • 2013-01-30
      • 2017-07-23
      相关资源
      最近更新 更多