springboot aop 自定义注解
枚举类:
/**
* Created by tzq on 2018/5/21.
*/
public enum MyAnnoEnum {
SELECT("select","查询"),
INSERT("insert","添加"),
UPDATE("update","修改"),
DELECT("delect","删除"); private MyAnnoEnum(String code, String name) {
this.code = code;
this.name = name;
}
public static String getNameByCode(String code) {
for (MyAnnoEnum enm : MyAnnoEnum.values()) {
if (enm.getCode() == code) {
return enm.getName();
}
}
return null;
}
private String code;
private String name; public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
pom.xml
<!--aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
MyAnno类:自定义注解类
import cn.supercop.idss.enm.MyAnnoEnum; import java.lang.annotation.*; /**
* 自定义注解类
* Created by tzq on 2018/5/19.
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnno { MyAnnoEnum value(); }
HtppHandlerMethodAspect : aop 类
import ch.qos.logback.core.net.SyslogOutputStream;
import cn.supercop.idss.config.MyAnno;
import cn.supercop.idss.domain.system.AuditLog;
import cn.supercop.idss.enm.MyAnnoEnum;
import cn.supercop.idss.service.AuditLogService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*; @Aspect //定义一个切面
@Component
public class HtppHandlerMethodAspect { private Logger log = LoggerFactory.getLogger(HtppHandlerMethodAspect.class);
private static final String[] IGNORE_URI = {"api/login"};//放行集合不进行aop
@Autowired
private AuditLogService auditLogService; /**
* 过滤指定url不aop
* @return true or false
*/
public boolean filter_URL(){
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
boolean flag = false;
String url = request.getRequestURL().toString();
log.info(">>>: " + url);
for (String s : IGNORE_URI) {
if (url.contains(s)) {
log.info("访问路径包含在放行集合中");
flag = true;
break;
}
}
return flag ;
} /**
* 所有的 controller 层进行aop扫描
* @param joinPoint
* @throws Exception
*/
@Before("execution(* cn.supercop.idss.controller..*(..))")
public void executioncontroller(JoinPoint joinPoint) throws Exception {
System.out.println("****************************controller 开始****************************");
if(!filter_URL()){
analysis(joinPoint);
}
System.out.println("****************************controller 结束****************************"); } /**
* 所有的mapper层进行aop扫描,
* @param joinPoint
* @throws Exception @Before("execution(* cn.supercop.idss.mapper..*(..)) ")
public void executemapper(JoinPoint joinPoint) throws Exception {
System.out.println("****************************mapper 开始****************************");
if(!"cn.supercop.idss.mapper.AuditLogMapper.insert".equals(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()))
analysis(joinPoint);
System.out.println("****************************mapper 结束****************************");
}
*/
public void analysis(JoinPoint joinPoint) throws Exception {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
System.out.println("URL : " + request.getRequestURL().toString());//请求路径
System.out.println("HTTP_METHOD : " + request.getMethod());//请求方法类型
System.out.println("IP : " + request.getRemoteAddr());//
//aop 切点的方法
System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));
//获取目标方法的参数信息
Object[] obj = joinPoint.getArgs();
for (Object o : obj) {
System.out.println(o);
} Enumeration<String> enumeration = request.getParameterNames();
Map<String, String> parameterMap = new HashMap();
while (enumeration.hasMoreElements()) {
String parameter = enumeration.nextElement();
parameterMap.put(parameter, request.getParameter(parameter));
}
String str = JSON.toJSONString(parameterMap);
if (obj.length > 0) {
System.out.println("请求的参数信息为:" + str);
}
String action = "缺少类型";//动作
//取自定义注解
Method method=((MethodSignature)joinPoint.getSignature()).getMethod();
Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
method.getParameterTypes());
Annotation an = method.getAnnotation(MyAnno.class);
if(an instanceof MyAnno){
MyAnno myAnno = (MyAnno) an;
action = myAnno.value().getName();
System.out.println("value: " + myAnno.value().getName());
}
//取用户名
Subject subject = SecurityUtils.getSubject();
String username = subject.getPrincipal().toString();
if(MyAnnoEnum.SELECT.getName() != action){
AuditLog auditLog = new AuditLog();
auditLog.setOccurredTime(new Date());//发生时间
auditLog.setAction(action);//动作
auditLog.setObjectName(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());//操作对像名称
auditLog.setDescription(str);//描述
auditLog.setSource(request.getRequestURL().toString());// '源头';
auditLog.setClientIp(this.getRemortIP(request));//客户端IP
auditLog.setUserId(new Long(1));//用户ID
auditLog.setUserType("内部");//用户类型
auditLog.setStatus((short) 1);//执行状态
auditLog.setCreatedBy(username);//创建人
auditLog.setModifiedBy(username);//创建人
auditLogService.addAuditLog(auditLog);
} } public String getRemortIP(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
} }
主要注解的类:
如Controller:
/**
* 查询蓝牙访问列表
*/
@MyAnno(MyAnnoEnum.SELECT)
@RequestMapping("/selectBluetoothAccess")
@ResponseBody
public Result<Object> selectBluetoothAccess(BluetoothAccess bluetoothAccess) {
如mapper or Dao :
public interface WifiAccessMapper {
Long count(@Param("personId") String personId);
//查询
@MyAnno(MyAnnoEnum.SELECT)
List<WifiAccess> select (WifiAccess wifiAccess);
}
总结:
自定义一个注解,注解引用一个枚举来约束注解后的值,定义来insert/update/select/deltet, 定义在dao层或者Controller层都可以实现,可根据业务需要实现,
在aop HtppHandlerMethodAspect类,根据execution表达式来切,在类中定义来一个常量数组,来过滤一些一个表达式中需要过滤的信息,analysis方法解析参数;
springboot aop 自定义注解的更多相关文章
- springboot aop 自定义注解方式实现完善日志记录(完整源码)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...
- springboot+aop+自定义注解,打造通用的全局异常处理和参数校验切面(通用版)
一.引入相应的maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
- SpringBoot系列(十三)统一日志处理,logback+slf4j AOP+自定义注解,走起!
往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)we ...
- redis分布式锁-spring boot aop+自定义注解实现分布式锁
接这这一篇redis分布式锁-java实现末尾,实现aop+自定义注解 实现分布式锁 1.为什么需要 声明式的分布式锁 编程式分布式锁每次实现都要单独实现,但业务量大功能复杂时,使用编程式分布式锁无疑 ...
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- ssm+redis整合(通过aop自定义注解方式)
此方案借助aop自定义注解来创建redis缓存机制. 1.创建自定义注解类 package com.tp.soft.common.util; import java.lang.annotation.E ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
随机推荐
- 委托(3).net 2.0中的委托
由于.net 2.0引入了匿名方法,所以创建委托的方式可以更简化了. .net 2.0中的委托 有了匿名方法,上一篇的例子可以简化为: namespace DelegateDemo { //声明委托 ...
- JavaScript(转载自 计科学院 慕课网)
什么是脚本语言? ①脚本语言介于HTML和C,C++,Java,C#等编程语言之间 ②脚本语言与编程语言有相似地方,其函数与编程语言类似,也有变量.与编程语言之间最大的区别是编程语言的语法和规则更为严 ...
- Object.keys 及表单清空
Object.keys 返回一个所有元素为字符串的数组,其元素来自于从给定的object上面可直接枚举的属性.这些属性的顺序与手动遍历该对象属性时的一致. // simple array var ar ...
- vue安装element-ui和px2rem的细节
1.按需引入element-ui vue脚手架搭建完成之后,可以到element-ui官网进行npm 安装: npm i element-ui -S 如果是完整引入可以按照官网一步一步做即可完成:这里 ...
- Xamarin 学习笔记 - Layout(布局)
本文翻译自CodeProject文章:https://www.codeproject.com/Articles/1227733/Xamarin-Notes-Xamarin-Forms-Layouts ...
- HA总结:AWS 网络连接
单数据中心 网络HA总结 参考:https://d1.awsstatic-china.com/aws-answers/AWS_Single_Data_Center_HA_Network_Connect ...
- iOS----------对单元格取余
if (indexPath.row % 2 == 0) { cell.backgroundColor = [UIColor magentaColor]; }else{ cell.backgroundC ...
- (转)hibernateTools工具安装及使用总结(eclipse 3.6)
最近项目采用flex+spring+hibernate的框架开发,之前虽说有多年的Java开发经验了,但是一直使用的JDBC或者 ibatis,hibernate的使用还是大姑娘上轿头一回,网上都介绍 ...
- 推荐一款MongoDB的客户端管理工具--nosqlbooster
今天给大家推荐一款MongoDB的客户端工具--nosqlbooster,这个也是我工作中一直使用的连接管理MongoDB的工具.这个工具还有个曾用名--mongobooster.nosqlboost ...
- C# List集合去重使用lambda表达式
name age sex Lucy 22 woman Lily 23 woman Tom 24 man Lucy 22 woman Lily 23 woman LiLei 25 man List< ...