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

📄 qrsxrequestprocessor.java

📁 java网上考试系统
💻 JAVA
字号:
/*
 * @(#)QrsxRequestProcessor.java Dec 16, 2006
 * Copyright 2006 qingdaosoftware, Inc. All rights reserved
 */
package com.qrsx.exam.struts;

import java.io.IOException;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;

import com.qrsx.exam.Constants;
import com.qrsx.exam.cache.Cache;
import com.qrsx.exam.cache.CacheFactory;
import com.qrsx.exam.config.ConfigConstants;
import com.qrsx.exam.config.ExamConfigUtil;
import com.qrsx.exam.exception.ExamSystemException;
import com.qrsx.exam.model.Action;
import com.qrsx.exam.model.Function;
import com.qrsx.exam.model.Role;
import com.qrsx.exam.model.User;
import com.qrsx.exam.util.StringUtils;

/**
 * 
 * 功能:
 * 
 * <p>
 * <a href="com.qrsx.exam.struts.QrsxRequestProcessor.java"> <i>View Source </i>
 * </a></br>
 * 
 * Company : QingdaoSoftware<br>
 * Author : <a href="mailto:wxt1013@163.com">WangXitao</a></br> Version : 1.0<br>
 * Date : Dec 16, 2006<br>
 */
public class QrsxRequestProcessor extends RequestProcessor {
    protected final Log log = LogFactory.getLog(RequestProcessor.class);

    /**
     * 用户认证方法
     */
    @Override
    protected boolean processRoles(HttpServletRequest request,
            HttpServletResponse response, ActionMapping mapping)
            throws IOException, ServletException {
        // 得到映射的路径
        String path = mapping.getPath();
        // 得到用户所要调用的Action方法的名字
        String method = request.getParameter(mapping.getParameter());
        if (StringUtils.isEmpty(method)) {
            method = Constants.DEFAULT_METHOD;
        }

        // 取得不需要校验权限的Action方法
        String[] roles = mapping.getRoleNames();
        if (roles != null && roles.length > 0) {
            // 进行方法的判断
            for (String role : roles) {
                if (method.equals(role)) {
                    request.setAttribute(Constants.REQUEST_CHECK_FLAG, true);
                    return true;
                }
            }
        }

        // 得到Session对象和用户对象
        HttpSession session = request.getSession();
        User u = (User) session.getAttribute(Constants.SESSION_USER);

        // 如果用于对象不存在,那么说明用户没有登录
        if (u == null) {
            // 用户没有执行的权限,跳转到错误页面
            processLocale(request, response);
            RequestDispatcher rd = request
                    .getRequestDispatcher("/errors/noauthority.jsp");
            rd.forward(request, response);
            return false;
        }

        // 判断用户是否为超级用户
        String superusers = ExamConfigUtil
                .getSysConfigValue(ConfigConstants.SUPER_USER);
        String[] users = StringUtils.splitString(superusers,
                ConfigConstants.USER_DELIM);
        if (StringUtils.contains(users, u.getName())) {
            request.setAttribute(Constants.REQUEST_CHECK_FLAG, true);
            return true;
        }

        // 得到用户的角色信息
        Cache cache = CacheFactory.getCache();
        Role role = (Role) cache.get(u.getUserType());
        if (role == null) {
            throw new ExamSystemException("Couldn't find the role!");
        }

        // 进行用户执行功能的判断
        Set<Function> functions = role.getFunctions();
        for (Function function : functions) {
            Set<Action> actions = function.getActions();
            for (Action action : actions) {
                if (path.equals(action.getPath())
                        && method.equals(action.getParameter())) {
                    request.setAttribute(Constants.REQUEST_CHECK_FLAG, true);
                    return true;
                }
            }
        }

        // 用户没有执行的权限,跳转到错误页面
        processLocale(request, response);
        RequestDispatcher rd = request
                .getRequestDispatcher("/errors/noauthority.jsp");
        rd.forward(request, response);
        return false;
    }

    @Override
    protected void processLocale(HttpServletRequest request,
            HttpServletResponse response) {
        super.processLocale(request, response);
        try {
            request.setCharacterEncoding("utf-8");
        } catch (Exception ex) {
        }
    }
}

⌨️ 快捷键说明

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