栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

这是我的解决方案,对我有用:编辑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;        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470469.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号