当前位置: 首页 > 面试题库 >

使用JSF 2,1和Apache Tomcat使用LDAP登录

农永宁
2023-03-14
问题内容

不仅仅是一个问题,我需要使用jsf执行身份验证。我开发了一个登录名,该登录名接收存储在MySQL中的用户名和密码。从Active
Directory登录后,它应该使用AD的用户名和密码,我想应该与MySQL的用户名和密码相同。

然后,要进入系统,您将不再看到登录名,而是直接看到主页或主页。

希望您的帮助和预先感谢。

问候。


问题答案:

这是我的解决方案,对我有用:编辑faces-config.xml

<lifecycle>
        <phase-listener>
            com.xxx.admin.security.Login
        </phase-listener>
    </lifecycle>

类登录:

    public class Login implements PhaseListener {
    private static final String USER_LOGIN_OUTCOME = "login";
     @Override
        public void afterPhase(PhaseEvent event) {
            FacesContext context = event.getFacesContext();
            if (userExists(context)) {
                // 1. Update last login
                // 2. may be expired ???
                ExternalContext extContext = context.getExternalContext();
                try {
                    ETT_UserDTL tmpUser = (ETT_UserDTL) extContext.getSessionMap().get(User.USER_SESSION_KEY);
                    if (!Authenticator.authenticateUser(tmpUser, context)) {
                        // send the user to the login view
                        reLogin(context);
                    } else {
                        ;
                    }
                    // allow processing of the requested view
                } catch (Exception ex) {
                    SystemLogger.getLogger().error(ex);
                    ClientMessage.logErr(ex.toString());
                    reLogin(context);
                }
            } else {
                // send the user to the login view
                reLogin(context);
            }
        }
    private boolean userExists(FacesContext context) {
    // Need re-check authenticator here.
    // Check user exist
    ExternalContext extContext = context.getExternalContext();
    return (extContext.getSessionMap().containsKey(User.USER_SESSION_KEY));
}
private void reLogin(FacesContext context) {
        // send the user to the login view
        if (requestingSecureView(context)) {
            context.responseComplete();
            context.getApplication().
                    getNavigationHandler().handleNavigation(context,
                    null,
                    USER_LOGIN_OUTCOME);
        } else {
            ;
        }
    }
    }

LDAP验证:

public class LDAPAuthentication {

    static String ATTRIBUTE_FOR_USER = "sAMAccountName";

    @SuppressWarnings("unchecked")
    public Attributes authenticateUser(String username, String password, String strDomain, String strHost, String dn) throws NamingException {

        String searchFilter = "(&(objectClass=user)(" + ATTRIBUTE_FOR_USER + "=" + username + "))";
        // Create the search controls

        SearchControls searchCtls = new SearchControls();
        // searchCtls.setReturningAttributes(returnedAtts);
        // Specify the search scope
        searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE);
        String searchBase = dn;
        Hashtable environment = new Hashtable();
        environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        // Using starndard Port, check your instalation
        environment.put(Context.PROVIDER_URL, "ldap://" + strHost);
        environment.put(Context.SECURITY_AUTHENTICATION, "simple");

        environment.put(Context.SECURITY_PRINCIPAL, username + "@" + strDomain);
        environment.put(Context.SECURITY_CREDENTIALS, password);

        LdapContext ctxGC = null;
        try {
            ctxGC = new InitialLdapContext(environment, null);
            // Search for objects in the GC using the filter
            NamingEnumeration answer = ctxGC.search(searchBase, searchFilter, searchCtls);
            while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult) answer.next();
                Attributes attrs = sr.getAttributes();
                if (attrs != null) {
                    return attrs;
                }
            }
        } catch (Exception e) {
            SystemLogger.getLogger().error(e);
        }
        return null;
    }
}

验证方式:

public static boolean authenticateLDAPUser(String strUser, String strPass, String strDomain, String strHost) throws NamingException, Exception {
        LDAPAuthentication ldap = new LDAPAuthentication();
        Attributes att = ldap.authenticateUser(strUser, strPass, strDomain, strHost, "");
        if (att != null) {
            try {
                ETT_UserDTL tmpUser = (ETT_UserDTL) DataUtil.performAction(DATA_UserGUI.class, "getInfByUserName", strUser);
                tmpUser.setPassword(strPass);
                if (!otherAuthenticate(tmpUser)) {
                    Authenticator.removeUser();
                    return false;
                } else {
                    ;
                }
                pushUser(tmpUser);
                return true;
            } catch (TelsoftException ex) {
                SystemLogger.getLogger().error(ex);
                return false;
            }
        } else {
            updateLoginFail();
            return false;
        }
    }


 类似资料:
  • LDAP 基本概念 https://segmentfault.com/a/1190000002607140 http://www.itdadao.com/articles/c15a1348510p0.html http://blog.csdn.net/reblue520/article/details/51804162 LDAP 服务器端安装 环境:CentOS 7.3 x64(为了方便,已经禁用

  • 一旦用户点击登录页面,我需要根据Spring Security中提供的LDAP组中的角色自动登录。 如果用户在LDAP中提到的组中有任何角色,那么它必须在登录后重定向到相应的页面。(即我的示例中的page1)。 我已经连续搜索了2天,为任何在线文档或一个例子,但徒劳无功。我所能找到的就是使用jdbcDataSource或在控制器中硬编码用户名和密码,然后在登录时或通过Spring使用web.xml

  • 我已经按照你在这篇博客中提到的步骤进行了操作。对于基本dn,他们给出了类似于'base dn-dc=,dc='的内容,但我给出了类似于'ecompany.local'的内容。因为我不是ldap管理员,所以我给了主体作为我的ldap id和我的ldap密码。当我单击test connection时,弹出窗口显示“Liferay已成功连接到LDAP服务器”。 重新启动了服务器。 点击登录。输入ldap

  • 我有一个在Java设计的应用程序,我愿意使用用户名和密码登录AWS。我知道如何使用这里定义的默认安全凭据,但它对我的应用程序需求无效。有没有办法使用用户名和密码使用JavaAPI登录我的AWS帐户?

  • 本文向大家介绍python使用tornado实现登录和登出,包括了python使用tornado实现登录和登出的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了tornado实现登录和登出的具体代码,供大家参考,具体内容如下 main.py如下: index.html login.html 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。