signonfilter.java

来自「jGossip是一个简单而功能强大的Java论坛软件(消息板)」· Java 代码 · 共 179 行

JAVA
179
字号
/*
 * $$Id: SignOnFilter.java,v 1.13 2004/05/30 20:03:56 bel70 Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and
 * limitations under the License.
 *
 * The Original Code is JGossip forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *              Dmitry Belov <bel@jresearch.org>
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on 26-Feb-2003
 */
package org.jresearch.gossip.filters;

import org.jresearch.gossip.IConst;
import org.jresearch.gossip.beans.user.User;
import org.jresearch.gossip.configuration.Configurator;
import org.jresearch.gossip.exception.ConfiguratorException;
import org.jresearch.gossip.log.ForumLog;

import java.io.IOException;

import java.util.HashMap;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * This filter protects some URI and make sure that only signed-on users can
 * access them
 */
public class SignOnFilter implements Filter {
    private String _signon;
    private HashMap _protectedUris = new HashMap();
    private HashMap _publicUris = new HashMap();
    private FilterConfig _config;

    /**
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    public void init(FilterConfig config) throws ServletException {
        _config = config;

        /* SignOn action */
        _signon = config.getInitParameter("signon.action");

        /* Protected Uri */
        String uri = config.getInitParameter("protected.uri");
        StringTokenizer tok = new StringTokenizer(uri, ",");

        while (tok.hasMoreTokens()) {
            String url = tok.nextToken().trim();
            _protectedUris.put(url, url);
        }

        /* Public Uri */
        uri = config.getServletContext().getInitParameter("public.uri");
        tok = new StringTokenizer(uri, ",");

        while (tok.hasMoreTokens()) {
            String url = tok.nextToken().trim();
            _publicUris.put(url, url);
        }
    }

    /**
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
     *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;

        /* Uri */
        String uri = req.getRequestURI();

        int i = uri.lastIndexOf("/");

        if (i >= 0) {
            uri = uri.substring(i + 1);
        }

        if (isPublic(uri)) {
            req.getSession().getServletContext().log("SignOnFilter: uri is  public - check of signon is skipped ");
            chain.doFilter(request, response);
        } else {
            /* check if signon is required */
            boolean isSignedIn = checkUser(req.getSession());

            if (isProtected(uri) && !isSignedIn) {
                request.setAttribute(IConst.REQUEST.REDIRECT_URL,
                    "/" + uri +
                    ((req.getQueryString() != null)
                    ? ("?" + req.getQueryString()) : ""));
                _config.getServletContext().getRequestDispatcher(getSignOnUrl())
                       .forward(request, response);
                ForumLog.getInstance().getForumLogger().warn(request.getRemoteAddr() +
                    " try to access " + req.getRequestURI());
            } else {
                chain.doFilter(request, response);
            }
        }
    }

    private String getSignOnUrl() throws ServletException {
        try {
            if (IConst.VALUES.FALSE.equals(Configurator.getInstance().get(IConst.CONFIG.ENABLE_FORUM_SIGN_ON))) {
                return Configurator.getInstance().get(IConst.CONFIG.EXT_LOGON_ACTION_URL);
            }
        } catch (ConfiguratorException e) {
            throw new ServletException(e);
        }

        return _signon;
    }

    private boolean checkUser(HttpSession session) {
        User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);

        if (user.getStatus() > 0) {
            return true;
        }

        return false;
    }

    /**
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy() {
        _protectedUris.clear();
    }

    /**
     * DOCUMENT ME!
     *
     * @param uri DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean isProtected(String uri) {
        return (_protectedUris.get(uri) != null);
    }

    /**
     * DOCUMENT ME!
     *
     * @param uri DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean isPublic(String uri) {
        return (_publicUris.get(uri) != null);
    }
}

⌨️ 快捷键说明

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