【问题标题】:How to get user information from ldap?如何从ldap获取用户信息?
【发布时间】:2018-05-21 16:36:37
【问题描述】:

我正在使用 apache shiro 进行 ldap 身份验证:

Factory<SecurityManager> ldapFactory = new IniSecurityManagerFactory("classpath:active.ini");
SecurityManager sManager = ldapFactory.getInstance();
SecurityUtils.setSecurityManager(sManager);

Subject currentUser = SecurityUtils.getSubject();

        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("user", "password");
            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) {
                logger.info("Authentication Exception");
            }
        }

        logger.info("User [" + currentUser.getPrincipal() +"] logged succesfully");
        currentUser.logout();

致电currentUser.getPrincipal(),我只有登录用户的电子邮件地址。
如何获取更多用户信息,例如姓氏、名字等?

【问题讨论】:

    标签: java ldap shiro


    【解决方案1】:

    getPrincipal() 只返回主题的标识符。尝试改用getPrincipals()

    对象 getPrincipal()
    返回此主题的应用程序范围的唯一标识 主体,如果此主题是匿名的,则为 null。

    PrincipalCollection getPrincipals()
    返回此主题的主体 (标识属性)以 PrincipalCollection 或 null 的形式 如果这个主题是匿名的

    Apache Shiro API (JavaDoc)

    【讨论】:

    • 我已经试过了,和getPrincipal()一样。我确实迭代了getPrincipals(),但结果与getPrincipal() 相同。
    【解决方案2】:

    您可以扩展 LDAP 领域。并向 Principal/PrincipalCollection 添加更多上下文(或返回自定义对象或您的 Principal)

    这是一个old thread,但我发现自己每隔一段时间就会挖掘它并涵盖这个主题(它提到了 JSecurity,这是 Shiro 的原始项目名称)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 2020-02-17
      • 2013-04-17
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多