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 ...
随机推荐
- Linux操作系统--定时任务
最近在学习Linux操作系统.学到了关于定时任务的章节,作为一个总结写下这篇文章.在Linux中,我们可以将耗时大的任务如复制大文件,压缩.解压缩大文件等放进定时任务中(深夜执行,因为工作时间访问量大 ...
- pip install PIL The _imagingft C module is not installed
需要先删除PIL再进行安装 sudo pip uninstall -y PIL 删除PIL相关文件夹:/usr/local/bin/pil , usr/lib/python2.7/dist-packa ...
- vmware station-ubuntu18.04 共享剪贴板
辞职在家休息,买了台新电脑,装个虚拟机,安装visual studio, android studio, qt, everything, noptepad++,hbuilder,ditto,xx-ne ...
- SQLServer之CHECK约束
CHECK约束添加规则 1.CHECK 约束用于限制列中的值的范围. 2.Check约束通过逻辑表达式来判断数据的有效性,用来限制输入一列或多列的值的范围,在列中更新数据时,所要输入的内容必须满足Ch ...
- LeetCode算法题-Maximum Product of Three Numbers(Java实现)
这是悦乐书的第275次更新,第291篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第143题(顺位题号是628).给定一个整数数组,从其中找出三个数,使得乘积最大.例如: ...
- win 10 Hbuilder1.2.1连接Genymotion 调试Android 软件
这里记录一下 Hbuilder1.2.1连接Genymotion 调试Android 软件 的过程: 步骤一:把Genymotion 的 adb.exe 路径配置到 Hbuilder 中 在 菜单栏 ...
- .net 调用java service 代理类方法
通过Svcutil.exe 工具生成代理类调用 1.找到如下地址“C:\Windows\System32\cmd.exe” 命令行工具,右键以管理员身份运行(视系统是否为win7 而定) 2 ...
- k8s--如何使用Namespaces
Namespaces 使用示例 Viewing namespaces Creating a new namespace Deleting a namespace Subdividing your cl ...
- Eclipse安装Gradle插件
1.window下安装Gradle见https://www.cnblogs.com/felixzh/p/9203271.html2.eclipse中依次打开help>Install new so ...
- SQL NULL 函数
SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 "Products" 表: P_Id ProductName UnitPrice ...