背景:在SpringMVC框架中,对Controller层中的需要相关用户权限的方法,加入Session中用户或管理员的验证。
NeedSession.Java -注解类
/** * 用户Session注解,只能用于方法<br/> * 默认为value = SessionType.USER * * @author Xiadi * @since 2013-9-10 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface NeedSession { /** * Session中用户的类型<br/> * 默认 USER * * @return */ SessionType value() default SessionType.USER; }
SessionType.java -枚举类
/** * Web上下文,保存所有request与response * */ public class SysContent { private static ThreadLocal<httpservletrequest> requestLocal = new ThreadLocal<httpservletrequest>(); private static ThreadLocal<httpservletresponse> responseLocal = new ThreadLocal<httpservletresponse>(); public static HttpServletRequest getRequest() { return requestLocal.get(); } public static void setRequest(HttpServletRequest request) { requestLocal.set(request); } public static HttpServletResponse getResponse() { return responseLocal.get(); } public static void setResponse(HttpServletResponse response) { responseLocal.set(response); } public static HttpSession getSession() { return requestLocal.get().getSession(); } } </httpservletresponse></httpservletresponse></httpservletrequest></httpservletrequest>
SessionValidateFilter.java -过滤器
/** * Session过滤器,将所有的请求保存 * */ public class SessionValidateFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { SysContent.setRequest((HttpServletRequest) request); SysContent.setResponse((HttpServletResponse) response); chain.doFilter(request, response); } @Override public void destroy() { } }
SessionAOP.java -AOP切面业务类
/** * Session AOP切面 * */ @Component @Aspect public class SessionAOP { @Around(value = "@annotation(com.eaglec.plat.hj.aop.NeedSession)") public Object aroundManager(ProceedingJoinPoint pj) throws Exception { HttpServletRequest request = SysContent.getRequest(); HttpServletResponse response = SysContent.getResponse(); HttpSession session = SysContent.getSession(); String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; SessionType type = this.getSessionType(pj); if (type == null) { throw new Exception("The value of NeedSession is must."); } Object uobj = session.getAttribute("user"); Object mobj = session.getAttribute("manager"); boolean isUser = type == SessionType.USER && uobj != null; boolean isManager = type == SessionType.MANAGER && mobj != null; boolean isUserOrManager = type == SessionType.OR&& (mobj != null || uobj != null); try { if (isUser || isManager || isUserOrManager) { return pj.proceed(); } else { // 会话过期或是session中没用户 if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase( //ajax处理 "XMLHttpRequest")) { response.addHeader("sessionstatus", "timeout"); // 解决EasyUi问题 response.getWriter().print("{\"rows\":[],\"success\":false,\"total\":0}"); }else{//http跳转处理 response.sendRedirect(basePath + "error/nosession"); } } } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private SessionType getSessionType(ProceedingJoinPoint pj) { // 获取切入的 Method Class actionClass = pj.getTarget().getClass(); Method method = actionClass .getMethod(joinPoint.getSignature().getName()); boolean flag = method.isAnnotationPresent(NeedSession.class); if (flag) { NeedSession annotation = method.getAnnotation(NeedSession.class); return annotation.value(); } return null; } }
web.xml
<!-- spring session aop --> <filter> <filter-name>sessionValidate</filter-name> <filter-class>com.eaglec.plat.hj.aop.SessionValidateFilter</filter-class> </filter> <filter-mapping> <filter-name>sessionValidate</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
servlet-context.xml
- <aop:aspectj-autoproxy/>
相关推荐
Spring 权限 这里是一个 spring aop 实现的一个权限 包括权限设计 这个不是很能懂 包括注释
NULL 博文链接:https://panmingzhi0815.iteye.com/blog/1139504
传统的应用程序实现 J2EE容器实现 AOP下的应用程序权限控制实现
使用SpringAop使用Oracle数据权限控制
本篇文章主要介绍了Java之Spring AOP 实现用户权限验证,用户登录、权限管理这些是必不可少的业务逻辑,具有一定的参考价值,有兴趣的可以了解一下。
本篇文章主要介绍了spring aop实现用户权限管理的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
用spring aop 编写的权限验证实例代码,下载后将解压后的文件直接导入到eclipse中,直接运行测试方法即可。
可扩展性强:使用 Spring AOP 进行方法耗时监测,可以方便的对其他的切面进行扩展,例如:日志、异常处理、权限控制等切面。 4. 提高系统性能:通过对系统中的方法进行耗时监测,可以及时发现系统中的性能瓶颈,...
主要介绍了Spring AOP实现权限检查的功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
一个简单的采用自定义注解结合SpringAop实现方法执行的权限管理,这个demo中并没有涉及到与数据库的交互和业务代码,用户权限在登陆时采用简单的手动初始化。该demo用的jdk1.7编译,Spring4.0版本,只想通过这个demo...
Spring Boot AOP权限控制模块开发,利用AOP功能完成属于自己的权限模块开发
AOP 采取横向抽取机制,取代了传统纵向继承体系的重复性代码,其应用主要体现在事务处理、日志管理、权限控制、异常处理等方面。 目前最流行的 AOP 框架有两个,分别为 Spring AOP 和 AspectJ。Spring AOP 使用纯 ...
SpringAOP与SpringMVC拦截器两种方式实现权限管控,前台xml读取配置根据登录用户判断button是否enable
springAOP指的是在spring中的AOP,什么是AOP,相对于java中的面向对象(oop),在面向对象中一些公共的行为,像日志记录,权限验证等如果都使用面向对象来做,会在每个业务方法中都写上重复的代码,造成代码的冗余。...
基于spring_aop_权限管理系统原型
1、编写切面类,包含权限审核方法和日志记录方法,这两个方法将来会织入到...5、编写案例,运用Spring AOP技术,要求包含前置通知、后置通知、环绕通知、返回通知、异常返回通知。请掌握这五种通知的特点,及应用场景
内含有mybatis 拦截器实现的分页代码,spring 的事务和aop 测试、和反射工具类
aop权限管理实例真实项目完整实例,有完整代码和配置及说明,spring版本是2.5,有需要的可以下载。上传只为分享
Spring中的AOP可以控制权限,在我们做项目的时候,用Struts的过滤方法也可以实现。 当然这个文件只是提供测试,我们主要是学习这个AOP的思想。大家下载后可以再摸索......