在WAS服务器上使用J2C别名有什么用?

时间:2016-03-07 12:42:54

标签: spring ldap websphere ibm-was

这是我的LdapTemplate类 public LdapTemplate getLdapTemplete(String ldapID)     {

    if (ldapID.equalsIgnoreCase(Constants.LDAP1))
    {

        if (ldapTemplate1 == null)
        {
            try
            {
                PasswordCredential passwordCredential = j2cAliasUtility.getAliasDetails(ldapID);
                String managerDN = passwordCredential.getUserName();
                String managerPwd = new String(passwordCredential.getPassword());

                log.info("managerDN :"+managerDN+":: password : "+managerPwd);

                LdapContextSource lcs = new LdapContextSource();
                lcs.setUrl(ldapUrl1);
                lcs.setUserDn(managerDN);
                lcs.setPassword(managerPwd);
                lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
                lcs.afterPropertiesSet();
                ldapTemplate1 = new LdapTemplate(lcs);

                log.info("ldap1 configured");
                return ldapTemplate1;
            }
            catch (Exception e)
            {
                log.error("ldapContextCreater / getLdapTemplete / ldap2");
                log.error("Error in getting ldap context", e);
            }
        }

        return ldapTemplate1;
    }

这是我的J2CAliasUtility类 - 我不知道这个方法做了什么以及它返回了什么?

public PasswordCredential getAliasDetails(String aliasName) throws Exception
    {
        PasswordCredential result = null;
        try
        {
            // ----------WAS 6 change -------------
            Map map = new HashMap();
            map.put(com.ibm.wsspi.security.auth.callback.Constants.MAPPING_ALIAS, aliasName); //{com.ibm.mapping.authDataAlias=ldap1}
            CallbackHandler cbh = (WSMappingCallbackHandlerFactory.getInstance()).getCallbackHandler(map, null);
            LoginContext lc = new LoginContext("DefaultPrincipalMapping", cbh);
            lc.login();
            javax.security.auth.Subject subject = lc.getSubject();
            java.util.Set creds = subject.getPrivateCredentials();
            result = (PasswordCredential) creds.toArray()[0];
        }
        catch (Exception e)
        {
            log.info("APPLICATION ERROR: cannot load credentials for j2calias = " + aliasName);
            log.error(" "+e);
            throw new RuntimeException("Unable to get credentials");
        }
        return result;
    }

2 个答案:

答案 0 :(得分:0)

您的类“J2CAliasUtility”似乎从JAAS(Java身份验证和授权服务)身份验证别名中检索用户名和密码,在这种情况下显然是从LDAP查找的。可以在WebSphere Application Server中按照herehere所述配置auth别名。您的代码使用WebSphere安全API从给定别名中检索用户标识和密码。有关程序化登录和JAAS的更多详细信息,请参阅此IBM KnowledgeCenter topic及其相关主题。

答案 1 :(得分:0)

J2C别名是对适配器访问数据库所使用的密码进行加密的功能。适配器可以使用它来连接数据库,而不必使用存储在适配器属性中的用户ID和密码。

J2C别名消除了将密码以明文形式存储在适配器配置属性中的可能,其他人可能会看到它。

相关问题