struts2利用拦截器(Interceptor)判断用户是否登录

在struts2中,很重要的一个知识点就是拦截器,拦截器在action前后都会执行,主要是针对action的。因此我想到用拦截器来判断用户是否登录,当然这不是一个很好判断用户是否登录的方法,但是我希望可以通过这个例子,来增强对拦截器的认识。
1.自定义一个拦截器


public class ValidateInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        // TODO Auto-generated method stub
        //这个result就是在struts中配置的result
        String result = "";
        //请求,调用的方法
        String method = invocation.getProxy().getMethod();
        // 如果是登录,注册,退出,则不用判断,直接进入下一个操纵      if (method.equals("login") || method.equals("regist")
                || method.equals("userNameExist")) {
            result = invocation.invoke();//进入下一步的请求处理       } else {
            //如果是其他请求,则判断用户是否登录,获取session中的登录用户
            Map<String, Object> map = invocation.getInvocationContext()
                    .getSession();
            UserInfo user = (UserInfo) map.get("user");
            if (user == null) {
                System.out.println("请先登录!");
                result = "noLogin";
            } else {
            //如果已经登录,则对当前用户操作进行记录
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                String now = sdf.format(new Date());
                BeanFactory factory = WebApplicationContextUtils
                        .getRequiredWebApplicationContext(ServletActionContext
                                .getServletContext());
                LogServices logServices = (LogServices) factory
                        .getBean("LogServices");
                // LogServices logServices=new LogServices();
                LogInfo logInfo = new LogInfo();
                logInfo.setLogOptOwner(user.getUserInfoName());
                logInfo.setLogFunctionName(method);
                logInfo.setLogOptTime(now);
                logServices.addlog(logInfo);
                result = invocation.invoke();//进入下一步的请求处理   
            }
        }
        return result;
    }
}

2.在struts.xml中

<interceptors>
            <interceptor name="ValidateInterceptor" class="interceptor.ValidateInterceptor">
            </interceptor>

            <interceptor-stack name="mystack">
                <interceptor-ref name="ValidateInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>

        </interceptors>

        <default-interceptor-ref name="mystack"></default-interceptor-ref>

        <global-results>
            <result name="noLogin">/nologin.jsp</result>
        </global-results>

3.Job记录操作

public class LogJob extends QuartzJobBean {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    private LogServices logServices;

    @Override
    protected void executeInternal(JobExecutionContext arg0)
            throws JobExecutionException {
        // TODO Auto-generated method stub
        logServices = (LogServices) arg0.getJobDetail().getJobDataMap()
                .get("logServices");

        quartzQuery();

    }

    public void quartzQuery() {
    //打印到本地
        String now = sdf.format(new Date());
        System.out.println("\t\t***********************************");
        List<LogInfo> list = logServices.getAlllogs();
        if (list.size() > 0) {
            System.out.println("\t\t用户\t操作\t时间");
            for (LogInfo logInfo : list) {
                System.out.println("\t\t" + logInfo.getLogOptOwner().trim()
                        + "\t" + logInfo.getLogFunctionName().trim() + "\t"
                        + logInfo.getLogOptTime());
            }
        }
        System.out.println("\t\t***********************************");
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_36027342/article/details/79774722