authentication - Apache Shiro - 使用 cn 以外的属性进行身份验证?

标签 authentication active-directory shiro

我正在尝试在我们的项目中使用 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 ]

感谢您的回答。

最佳答案

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

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){}

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

关于authentication - Apache Shiro - 使用 cn 以外的属性进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23078484/

相关文章:

java - GWT/GAE(Java) : when combined with GAE, app.yaml/web.xml 用户身份验证和登录/注销实际上不起作用

java - 如何在 Grails 或 Java 应用程序中轻松实现 "who is online"?

java - Apache Shiro 需要哪些数据库表?

java - Apache Shiro - Maven 项目中的主题始终为空

php - 拉维尔 5.2 : Display a session flash message after user login/logout and registration

authentication - 密码保护 REST 服务?

JSF 身份验证 : cannot intercept error messages

c# - Graph API 从 Azure AD 获取用户

c# - 密码过期时如何在事件目录中更改密码

c# - 将大量用户添加到 Active Directory 中的组