SpringAOP的自定义注解实践
springaop属于spring的重要属性,在java中有相当广泛的用途,大家一般都接触过aop实现事务的管理,在xml里配好声明式事务,然后直接在service上直接加上相应注解即可,
今天我们来实现下SpringAOP的自定义注解,用来在前置通知中做下权限校验,有利于我们代码的解藕,提高复用性,增加代码B格;
话不多说,上代码,首先定义一个自定义注解
这里的参数我们并没有在使用,先随意定义个type,以后想使用时可以再赋值
再来看切面
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.jiubao.common.core.cache.redis.JedisUtil;
import com.jiubao.common.core.exception.MyException;
import com.jiubao.livenet.cache.JiuBaoCache;
import com.jiubao.livenet.constant.ServiceConstants;
import com.jiubao.livenet.enums.Limitsenum;
import com.jiubao.livenet.service.CustomerService;
/**
* 注解切面
* @author Administrator
*
*/
@Aspect
@Component
public class AuthorityAspect {
private static final Logger logger = LoggerFactory.getLogger(AuthorityAspect.class);
@Resource
private CustomerService customerService;
//Controller层切点
@Pointcut("execution (* com.jiubao.livenet.controller.*.*(..))")
public void controllerAspect() {}
/**
* 前置通知
* 只拦截带有Authority注解的controller方法
* @param joinPoint 切点
* @return
*/
//@org.springframework.stereotype.Controller *) 表示拦截controller方法 @annotation(Authority) 表示只拦截带此注解的方法
@Before("within(@org.springframework.stereotype.Controller *) && @annotation(Authority)")
public void doBefore(JoinPoint joinPoint) throws MyException {
logger.info("==========执行商户权限controller前置通知===============");
Object[] args = joinPoint.getArgs();
HttpServletRequest request=null;
for (int i = 0; i < args.length; i++) {
Object object = args[i];
if(object instanceof HttpServletRequest) {
request=(HttpServletRequest) args[i];
break;
}
}
Map<String, Object> paramMap = new HashMap<String, Object>();
if(request ==null) {
return;
}
String url = request.getRequestURI().toString(); //请求的url,注意,只有方法参数里有HttpServletRequest request才能获取到request
/**
* 权限控制
*/
JedisUtil jedis = JiuBaoCache.getJedisUtil();
String userId = jedis.getUserId(request);
paramMap.put("uid", userId); //登录人UID
boolean isAccess=false;
List<Map<String, String>> userAuthlist= customerService.getUserAuth(paramMap); //查询登录人的商户角色列表
for (int i = 0; i < userAuthlist.size(); i++) {
if(userAuthlist.get(i).get("role")!=null) {
String[] limitsArray = Limitsenum.limitsArray(Integer.valueOf(userAuthlist.get(i).get("role"))); //使用枚举得到此角色对应的URL数组
for (int a = 0; a < limitsArray.length; a++) {
if(url.contains(limitsArray[a])) { //判断是否有,如果有则跳出
isAccess=true;
break;
}
}
if(isAccess) {
break;
}
}
}
if (!isAccess){
throw new MyException(-1,ServiceConstants.NO_ACCESS); //无权限访问,如果没有权限直接扔出个自定义异常,在那里会用response返回给前端提示
}
}
//全部拦截,这样的before就是全局拦截,会拦截所有方法
@Before("controllerAspect()")
public void doBefore2(JoinPoint joinPoint) {
System.out.println("==========执行全部拦截controller前置通知===============");
}
@AfterReturning(returning="rvt",pointcut="controllerAspect()") //打印所有方法的返回值
public Object AfterExec(JoinPoint joinPoint,Object rvt){ //pointcut是对应的注解类 rvt就是方法运行完之后要返回的值
logger.info("返回值为:" + rvt);
return rvt;
}
}
再看看调用过程
直接加在controller里想要限制的方法上即可
上面就是全部逻辑了,还有after和Around注解在此省略,这里要开始测试了,我几次都测不通,发现根本无效,而且
我配置文件里也有 <aop:aspectj-autoproxy proxy-target-class="true" />,不知道怎么回事,后来才发现
如果你的配置文件是这样的:
你除了在spring-mybaits.xml里要有这个外,还需要在spring-mvc.xml里有,好吧,都要加上才有效,而且别忘了头部的声明哦
顺便鄙视一下springmvc,配置文件就是麻烦
在此就完成了所有代码了,亲测是有效的
需提醒是的,当一个切面里有多个同样的注解时,会按链式分先后执行,比如上面第1个before,所有配了@Authority的方法会执行,接着执行第2个before,
而没有配@Authority的方法会直接执行第2个before,第1个由于不满足条件不会执行,这样方便我们在一个切面里写多种灵活的逻辑.
springaop由于配置灵活,复用性强,无性能损耗,在封装代码上很好用,比拦截器更方法,推荐使用
再推荐几个这方面比较好的文章:
1.https://www.cnblogs.com/jianjianyang/p/4910851.html
2.https://www.cnblogs.com/sjlian/p/7325602.html
3.https://www.cnblogs.com/mouseIT/p/5033746.html
SpringAOP的自定义注解实践的更多相关文章
- SpringBoot+SpringAOP+Java自定义注解+mybatis实现切库读写分离
一.定义我们自己的切库注解类 自定义注解有几点需要注意: 1)@Target 是作用的目标,接口.方法.类.字段.包等等,具体看:ElementType 2)@Retention 是注解存在的范围,R ...
- springAop整合自定义注解做方法日志配置(源码在附件)
package com.aop.log.anno; import java.lang.annotation.ElementType; import java.lang.annotation.Reten ...
- java自定义注解注解方法、类、属性等等【转】
http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...
- JAVA自定义注解SpringAOP
原文:https://my.oschina.net/wangnian/blog/801348 前言:Annotation(注解)是JDK5.0及以后版本引入的,它的作用就是负责注解其他注解.现在开发过 ...
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)
转载:http://itindex.net/detail/50710-springaop-controller-service 从业近二,三年了,第一次写博客,平时做做脚手架或者架构一些基础框架然后给 ...
- Spring Boot系列——AOP配自定义注解的最佳实践
AOP(Aspect Oriented Programming),即面向切面编程,是Spring框架的大杀器之一. 首先,我声明下,我不是来系统介绍什么是AOP,更不是照本宣科讲解什么是连接点.切面. ...
- spring --解析自定义注解SpringAOP(配合@Aspect)
1:首先,声明自定义注解 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface DtT ...
- 使用自定义注解和springAOP捕获Service层异常,并处理自定义异常
一 自定义异常 /** * 自定义参数为null异常 */ public class NoParamsException extends Exception { //用详细信息指定一个异常 publi ...
- ssm+redis 如何更简洁的利用自定义注解+AOP实现redis缓存
基于 ssm + maven + redis 使用自定义注解 利用aop基于AspectJ方式 实现redis缓存 如何能更简洁的利用aop实现redis缓存,话不多说,上demo 需求: 数据查询时 ...
随机推荐
- Docker Toolbox更换镜像源并下载centos7
- 为订阅内虚拟机批量安装并配置 Microsoft Anti-Malware 扩展
本文提供了对订阅内的 Windows 经典部署虚拟机和资源管理器部署虚拟机执行批量安装并配置 Microsoft Anti-Malware 扩展的 PowerShell 脚本. 关于安装 Window ...
- 数据库常用SQL用法
查找某列数据包含某一字符串: SELECT * FROM table WHERE column LIKE '%string%' 查找某列数据以某些字符串开头: SELECT * FROM table ...
- ETCD TLS 配置的坑
一.环境准备 环境总共 3 台虚拟机,系统为centos7,1个 master,2 个 etcd 节点,master 同时也作为 node 负载 pod,在分发证书等阶段将在另外一台主机上执行,该主机 ...
- 省事之通用Makefile模版
现在编译方案都偏爱使用cmake解决问题,这两条做unity插件,还是用Makefile,居然忘得光光,好记性不如烂笔头. 后面,翻箱倒柜找到以前为炼金术写的Makefiel,发现还真是挺好用,贴出来 ...
- 41、Thead线程 System.Thread与互斥体Mutex
Thead线程 System.Thread 使用Thread类可以创建和控制线程.下面的代码是创建和启动一个新线程的简单例子.Thread 类的构造函数重载为接受ThreadStart和Paramet ...
- python选课系统
程序名称: 选课系统 角色:学校.学员.课程.讲师 要求: 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海 ...
- OS开发小记:iOS富文本框架DTCoreText在UITableView上的使用
要在页面中显示自己的布局,比如文字的字体和颜色.图文并排的样式,我们要用iOS SDK的原生UI在app本地搭建,如果一个页面需要在服务器端获取数据的话,我们也要在本地搭建好固定的布局,解析服务器传回 ...
- Spring4 SpringMVC Hibernate4 Freemaker 整合样例
更正改动(2014-05-30 13:47:22):有的IDE中web.xml会报这个错: cvc-complex-type.2.4.a: Invalid content was found star ...
- Guava包学习---Maps
Maps包方法列表: 还是泛型创建Map: public static <K, V> HashMap<K, V> newHashMap() { return new HashM ...