zoukankan      html  css  js  c++  java
  • Struts2 自定义拦截器

    自定义拦截器(权限管理),包含了对ajax和表单请求的拦截

    package com.interceptor;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    
    public class AuthorityInterceptor extends AbstractInterceptor{
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        @Override
        /* session过期及操作的权限验证拦截器 */
        public String intercept(ActionInvocation invocation) throws Exception {
            // TODO Auto-generated method stub
            // 取得请求相关的ActionContext实例 
            ActionContext context=invocation.getInvocationContext();
            Map session=context.getSession();
            String employ=(String)session.get("employ");
            
            
            if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){
                // session 过期
                 //return Action.LOGIN;
                //对ajax请求的拦截
                return isAjax();
            }else if (employ==null) {
                //没有登陆,将服务器提示设置成一个HttpServletRequest属性  
                context.put("tip","您还没有登录,请登陆系统"); 
                return isAjax();           
                
            }else {
                return invocation.invoke();
            }
            
            
        }// end function
    
        private String isAjax() {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setCharacterEncoding("text/html;charset=utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = null;
            try {
                pw = response.getWriter();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String flag = "";
            if (request.getHeader("X-Requested-With") != null
            && request.getHeader("X-Requested-With").equalsIgnoreCase(     
            "XMLHttpRequest")) {  
                
                flag = "sessionfalse";
                pw.write(flag);
                return null;
             }else{
               return Action.LOGIN;
            }
        }
        
    
    }
  • 相关阅读:
    mongoDB Liunx下安装及配置
    Node.js WEB服务器(1)——编写简单的HTTP服务器
    MongoDB 和 NoSQL简介
    ES6的Promise浅析
    Node.js的模块系统
    Node.js的异步IO和事件轮询
    mybatis 关联表心得
    mustache 模板,用于构造html页面内容
    Python实现冒泡,选择排序
    文件路径太长无法删除 robocopy
  • 原文地址:https://www.cnblogs.com/heyesp/p/4595677.html
Copyright © 2011-2022 走看看