zoukankan      html  css  js  c++  java
  • 使用spring AOP获得session的思路

    由于Spring 的AOP面向切面编程,与Servlet容器没有任何关联,所以想要获得Session会话比较麻烦。
    当然Struts2同样不依赖Servlet容器,可以在Spring AOP中可以使用com.opensymphony.xwork2.ActionContext,就可以获得Session。
    但是在Servlet中或struts1中,可以通过ThreadLocal方式将session保存,Spring AOP中获得Session对象。
    具体思路和代码:
     
    这个是保存request和session的类
    /**
     * 
     * @author 株洲健坤北大青鸟 周钢
     *
     */
    public class SysContent {  
         private static ThreadLocal<HttpServletRequest> requestLocal= new ThreadLocal<HttpServletRequest>();  
         private static ThreadLocal<HttpServletResponse> responseLocal= new ThreadLocal<HttpServletResponse>();  
           
        public static HttpServletRequest getRequest() {  
            return (HttpServletRequest)requestLocal.get();  
        }  
        public static void setRequest(HttpServletRequest request) {  
            requestLocal.set(request);  
        }  
        public static HttpServletResponse getResponse() {  
            return (HttpServletResponse)responseLocal.get();  
        }  
        public static void setResponse(HttpServletResponse response) {  
            responseLocal.set(response);  
        }  
        public static HttpSession getSession() {  
            return (HttpSession)((HttpServletRequest)requestLocal.get()).getSession();  
        }  
    }
     
     
    这个是配置的过滤器
     
    /**
     * 
     * @author 株洲健坤北大青鸟 周钢
     *
     */
    public class GetContent implements Filter {  
        @Override  
        public void destroy() {  
            // TODO Auto-generated method stub  
        }  
        @Override  
        public void doFilter(ServletRequest arg0, ServletResponse arg1,  
                FilterChain arg2) throws IOException, ServletException {  
            SysContent.setRequest((HttpServletRequest) arg0);  
            SysContent.setResponse((HttpServletResponse) arg1);  
            arg2.doFilter(arg0, arg1);  
        }  
        @Override  
        public void init(FilterConfig arg0) throws ServletException {  
            // TODO Auto-generated method stub  
        }  
    }  
     
     
     
    使用AOP进行环绕通知切入所有com.aptech.service包下的所有类的方法
     
    /**
     * @author 株洲健坤北大青鸟 周钢
     */
    @Aspect
    public Class AopTest{
    @Around(value="execution(* com.aptech.service.*.*(..))")
    public void aroundTest(ProceedingJoinPoint pj) throws Exception {  
              HttpServletRequest request = SysContent.getRequest();  
              HttpServletResponse response = SysContent.getResponse();  
              HttpSession session = SysContent.getSession();  
              //其他操作
              if(true){
               pj.proceed();
              }
              throw new Exception("您没有该权限");
        } 
    }
  • 相关阅读:
    20145312 《信息安全系统设计基础》第13周学习总结
    20145312《信息安全系统设计基础》实验五 网络通信
    20145312 《信息安全系统设计基础》第12周学习总结
    20145312 GDB调试汇编堆栈过程分析
    20145312《信息安全系统设计基础》实验四 驱动程序设计
    20145312 《信息安全系统设计基础》第11周学习总结
    20145312 《信息安全系统设计基础》实验三 实时系统的移植
    20145312 《信息安全系统设计基础》第10周学习总结
    20145312 《信息安全系统设计基础》第9周学习总结
    20145209&20145309信息安全系统设计基础实验报告 (4)
  • 原文地址:https://www.cnblogs.com/netcorner/p/4346190.html
Copyright © 2011-2022 走看看