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