【问题标题】:Custom LDAP authentication using Spring Security 4使用 Spring Security 4 的自定义 LDAP 身份验证
【发布时间】:2015-04-17 18:17:28
【问题描述】:

在我的非 Spring Security 应用程序中,我们使用 LDAP 通过使用他的 ID 和密码连接到 LDAP 服务器来对用户进行身份验证。如果连接成功,则对用户进行身份验证,并从 LDAP 获取他的详细信息。下面是代码:

private void getLdapConnection(UserSignInObject userSignInObject) {
    LdapContextSource ctxSrc = new LdapContextSource();
    ctxSrc.setUrl("ldap://mjkoldc-03.red.com");
    ctxSrc.setUserDn("mj\\" + userSignInObject.getEmail());
    ctxSrc.setPassword(userSignInObject.getPassword());
    ctxSrc.setReferral("follow");
    ctxSrc.afterPropertiesSet();    
    LdapTemplate tmpl = new LdapTemplate(ctxSrc);
    setLdapTemplate(tmpl);
}

@Override
public DefaultUserObject selectUserDetailsFromLdap(
        UserSignInObject userSignInObject) throws Exception {
    DefaultUserObject user = new DefaultUserObject();
    try {
        getLdapConnection(userSignInObject);
        LdapQuery query = query().base("dc=metaljunction,dc=com")
            .attributes("GivenName", "sn", "mail", "MobilePhone")
            .where("ObjectClass").is("user").and("SamAccountName")
            .is(userSignInObject.getEmail());
        user = ldapTemplate.searchForObject(query,
            new ContextMapper<DefaultUserObject>() {
            @Override
            public DefaultUserObject mapFromContext(Object ctx)
                throws NamingException {
                DirContextAdapter context = (DirContextAdapter) ctx;
                DefaultUserObject user = new DefaultUserObject();
                user.setFirstName(context
                    .getStringAttribute("GivenName"));
                user.setLastName(context.getStringAttribute("sn"));
                user.setEmail(context.getStringAttribute("mail"));
                user.setPhone(context
                    .getStringAttribute("MobilePhone"));
                return user;
            }
            });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return user;
}

要求是在 Spring Security 4 中实现相同的逻辑。我想将详细信息保存在 AuthenticationUserDetails 对象中。我该怎么做?我正在使用基于 Java 的配置。这是对用户进行身份验证的唯一方法。

【问题讨论】:

    标签: java spring spring-security ldap spring-ldap


    【解决方案1】:

    您需要实现自己的 AuthenticationProvider(即实现 org.springframework.security.authentication.AuthenticationProvider 的类)并配置 Spring Security 以使用它。 看看这个:Implement custom AuthenticationProvider in Spring Security 2.06

    【讨论】:

      猜你喜欢
      • 2016-08-05
      • 1970-01-01
      • 2011-02-09
      • 2016-09-09
      • 2019-02-11
      • 2014-04-20
      • 2014-12-13
      • 2012-02-18
      • 2016-09-01
      相关资源
      最近更新 更多