【问题标题】:glassfish ldap authorization after spnego authenticationspnego 身份验证后的 glassfish ldap 授权
【发布时间】:2012-09-06 06:36:29
【问题描述】:

我们使用 Apache mod_proxy_http 和 SPNEGO 成功实现了 SSO。在我的 Java EE 6 Web 应用程序中,我使用 request.getRemoteUser() 获取经过身份验证的用户。

现在最好的授权方式是什么。我们的目标是通过 LDAP 在我们的 Microsoft AD 中检查用户的特定角色成员身份。使用 glassfish 3.1.2 实现这一目标的最佳方法是什么?

【问题讨论】:

  • 你能做到吗?如果是,介意在此处添加答案吗?

标签: glassfish ldap java-ee-6 spnego jaspic


【解决方案1】:

这取决于您的用例。 如果您只想知道 AD 中的角色,您可以在 LDAP 上进行手动搜索。

例子:

import javax.naming.*;
import javax.naming.directory.*;

public class test {

    public String ldapUri = "ldap://localhost";
    public String usersContainer = "cn=users,dc=example,dc=com"; // base DN for search
    public String username = "user";
    public String password = "pass";

    public static void main(String args[]){

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, ldapUri);
    env.put( Context.SECURITY_PRINCIPAL, username );
    env.put( Context.SECURITY_CREDENTIALS, password );
    env.put(Context.REFERRAL, "follow");

    try {
        DirContext ctx = new InitialDirContext(env);
        SearchControls ctls = new SearchControls();

        String name = "your_username"; // full DN name
        NamingEnumeration answer = ctx.search(usersContainer, "(member=" + name + ")",ctls );    

        while(answer.hasMore()) {
                SearchResult rslt = (SearchResult)answer.next();
                Attributes attrs = rslt.getAttributes();
                System.out.println(attrs.get("cn"));
            }

        ctx.close();


    } catch (NamingException e) {
        e.printStackTrace();
    }    
    }
 }

请注意,用户名必须是用户的完整 DN,例如CN=user1,CN=users,DC=company,DC=com

如果您想在应用程序中使用这些组进行角色分配,事情就有点棘手了。要实现这一点,您可能需要自定义 ServerAuthModule(例如 here),或者您必须修改 SPNEGO 部分以根据来自 AD/LDAP 的数据分配角色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2021-02-24
    • 1970-01-01
    • 2011-09-27
    • 2017-05-28
    相关资源
    最近更新 更多