由于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("您没有该权限");
    } 
}

使用spring AOP获得session的思路的更多相关文章

  1. Spring aop与HibernateTemplate——session管理(每事务一次 Session)

    一.HibernateTemplate与Spring aop简介 参见http://bbs.csdn.net/topics/340207475中网友blueram的发言.(感谢blueram) 二.在 ...

  2. 按照自己的思路去研究Spring AOP源码【1】

    目录 一个例子 Spring AOP 原理 从@EnableAspectJAutoProxy注解入手 什么时候会创建代理对象? 方法执行时怎么实现拦截的? 总结 问题 参考 一个例子 // 定义一个切 ...

  3. 按照自己的思路研究Spring AOP源码【2】

    目录 问题的提出 哪一步导致了顺序的改变 AbstractAdvisorAutoProxyCreator.sortAdvisors()方法 总结 问题的提出 上面这篇文章介绍了Spring AOP源码 ...

  4. spring aop获取目标对象的方法对象(包括方法上的注解)

    这两天在学习权限控制模块.以前看过传智播客黎活明老师的巴巴运动网视频教程,里面就讲到权限控制的解决方案,当时也只是看看视频,没有动手实践,虽说看过几遍,可是对于系统中的权限控制还是很迷茫,所以借着这次 ...

  5. spring aop实现日志收集

    概述 使用spring aop 来实现日志的统一收集功能 详细 代码下载:http://www.demodashi.com/demo/10185.html 使用spring aop 来实现日志的统一收 ...

  6. 转:spring aop 拦截业务方法,实现权限控制

    难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要.思索很久没有办法,后来在网上看到了 ...

  7. Spring AOP实例——异常处理和记录程序执行时间

    实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...

  8. 从零开始学 Java - Spring AOP 实现用户权限验证

    每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...

  9. s2sh框架搭建(基于spring aop)

    对于spring aop 是如何管理事务的,请看一下:http://bbs.csdn.net/topics/290021423 1.applicationContext.xml <?xml ve ...

随机推荐

  1. LINUX 高可用群集之 ~Corosync~

    Corosync:它属于OpenAIS(开放式应用接口规范)中的一个项目corosync一版本中本身不具 备投票功能,到了corosync 2.0之后引入了votequorum子系统也具备了投票功能了 ...

  2. External components provide true shutdown for boost converter

    The step-up switching-converter circuit in Figure 1 presents a familiar problem: If you shut down bo ...

  3. FreeRTOS Customisation -- FreeRTOSConfig.h

    http://www.freertos.org/a00110.html FreeRTOS is customised using a configuration file called FreeRTO ...

  4. 使用vue脚手架工具搭建vue-webpack项目

    对于Vue.js来说,如果你想要快速开始,那么只需要在你的html中引入一个<script>标签,加上CDN的地址即可.但是,这并不算是一个完整的vue实际应用.在实际应用中,我们必须要一 ...

  5. 使用 HAProxy, PHP, Redis 和 MySQL 轻松构建每周上亿请求Web站点

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. C/C++之回调函数

    转自:http://www.cnblogs.com/chenyuming507950417/archive/2012/01/02/2310114.html 今天讨论下C/C++中的回调函数. 在理解“ ...

  7. mysql -- 用索引应避免空值

    由于数据库的复杂性,以讹传讹的空间非常大,快赶上中医养生了.避免使用 NULL 的理由,在高性能MySQL里有提到一段.建议大家多读些书,少看网上的奇技淫巧.特意把书翻出来摘录了下以供参考: 要尽量避 ...

  8. 使用富文本OHAttributedLabel

    OHAttributedLabel 富文本标签 https://github.com/AliSoftware/OHAttributedLabel 以下是我渲染出来的效果 OHAttributedLab ...

  9. 深度学习Deep learning

    In the last chapter we learned that deep neural networks are often much harder to train than shallow ...

  10. 从头開始学 RecyclerView(六) LayoutManager

    前言 在前面的文章中.每一个演示样例,都使用了LayoutManager,毕竟它是RecyclerView必不可少的一部分. LayoutManager,顾名思义,就是『布局管理器』. 使用例如以下代 ...