⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autologonfilter.java

📁 Chinaxp 论坛源代码
💻 JAVA
字号:
package org.redsoft.forum.filters;import org.redsoft.forum.ForumConstants;import org.redsoft.forum.dao.DAOFactory;import org.redsoft.forum.exception.AccountNotFoundException;import org.redsoft.forum.exception.DAOException;import org.redsoft.forum.security.SimpleCallbackHandler;import javax.security.auth.Subject;import javax.security.auth.login.LoginContext;import javax.security.auth.login.LoginException;import javax.servlet.*;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import java.io.IOException;/** * <p>Filter that perform user auto logon * * Find username in cookies,and then perform auto logon * * @@author <a href="mailto:chjxm@msn.com">cinc</a> * * @@version $Id: AutoLogonFilter.java,v 1.1.1.1 2004/02/04 03:52:12 mustang Exp $ */public class AutoLogonFilter implements Filter  {    /**     * Perform user auto logon     *     * Check session for user, if available, quit     * Otherwise, check cookie for user, if available, proform auto login     *      and save user to session     *     * @param request The servlet request we are processing     * @param response The servlet response we are creating     * @param chain The filter chain we are processing     *     * @exception IOException if an input/output error occurs     * @exception ServletException if a servlet error occurs     */    public void doFilter(ServletRequest request, ServletResponse response,                         FilterChain chain)	throws IOException, ServletException {        HttpServletRequest hreq = (HttpServletRequest)request;        // find user info in session        Subject subject            =(Subject)hreq.getSession().getAttribute( ForumConstants.USER_KEY );        // if subject is null, user is not loged in, check cookies        if (subject == null){            // check cookie            Cookie[] cookies = hreq.getCookies();            //outputCookies(cookies);            // find user in cookies            String userName = findUserInCookie( cookies );            //System.out.println("username = " + userName );            // if userName is not null, perform auto logon            if ( userName != null ){                String password = null;                try {                    password = DAOFactory.getInstance().getAccountDAO().findByUserName( userName ).getPassword();                    LoginContext lc = new LoginContext("XForumLogin", new SimpleCallbackHandler( userName, password ) );                    lc.login();                    subject = lc.getSubject();                    // save user info to session                    hreq.getSession().setAttribute( ForumConstants.USER_KEY, subject );                    System.out.println("Save user subject to session");                } catch (DAOException e) {                    System.out.println("DAOException, call developer");                    e.printStackTrace();                } catch (AccountNotFoundException e) {                    System.out.println("Account not found with userName : " + userName );                } catch (LoginException e) {                    System.out.println("LoginException, call developer");                    e.printStackTrace();                }            }        }        // Pass control on to the next filter        chain.doFilter(request, response);    }    /**     * Search for user in cookies     *     * @param cookies   the cookies     * @return          if found, return username, otherwise return null     */    private String findUserInCookie( Cookie[] cookies ){        if (cookies == null){            return null;        }        for (int i = 0; i < cookies.length; i++) {            Cookie cookie = cookies[i];            if (cookie.getName().equals( ForumConstants.USER_KEY )){                return cookie.getValue();            }        }        return null;    }    /**     * Output all cookies, for debug     * @param cookies     */    private void outputCookies( Cookie[] cookies ) {        if (cookies != null){            System.out.println("Cookies:---------------------");            for (int i = 0; i < cookies.length; i++) {                Cookie c = cookies[i];                String name = c.getName();                String value = c.getValue();                System.out.println(name + " = " + value);            }            System.out.println("Cookies:---------------------");        }    }    /**     * Place this filter into service.     * Read acl configuration from file     *     * @param filterConfig The filter configuration object     */    public void init(FilterConfig filterConfig) throws ServletException {    }    /**     * Take this filter out of service.     */    public void destroy() {    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -