springboot jpa实现切面用户日志

配置切面开启

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>5.3.8</version>
		</dependency>

properties添加配置文件:

#配置切面开启
		spring.aop.proxy-target-class=true
		spring.aop.auto=true

创建自定义拦截器SystemControllerLog:

package com.aeh.annotation;
		import java.lang.annotation.*;
		/**
		 * Title: SystemControllerLog
		 * @date 2018年8月31日
		 * @version V1.0
		 * Description:  自定义注解,拦截controller
		 */
		@Target({ElementType.PARAMETER, ElementType.METHOD})//作用在参数和方法上
		@Retention(RetentionPolicy.RUNTIME)//运行时注解
		@Documented//表明这个注解应该被 javadoc工具记录
		public @interface SystemControllerLog {
			String description() default "";
		}

创建自定义拦截器SystemServiceLog :

package com.aeh.basic.annotations;

import java.lang.annotation.*;

/**
 * Title: SystemControllerLog
 * @date 2018年8月31日
 * @version V1.0
 * Description:  自定义注解,拦截service
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemServiceLog {
    String description() default "";
}

SystemLogAspect :

package com.aeh.basic.annotations;

import com.aeh.basic.model.Action;
import com.aeh.basic.model.User;
import com.aeh.basic.service.IActionService;
import com.aeh.basic.utils.IPUtils;
import com.aeh.basic.utils.JsonUtils;
import com.aeh.basic.utils.LoginInfo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;


@Aspect
@Component
public class SystemLogAspect {
    //注入Service用于把日志保存数据库,实际项目入库采用队列做异步
    @Resource
    private IActionService actionService;
    //本地异常日志记录对象
    private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);
    //Service层切点
    @Pointcut("@annotation(com.aeh.basic.annotations.SystemServiceLog)")
    public void serviceAspect(){
    }

    //Controller层切点
    @Pointcut("@annotation(com.aeh.basic.annotations.SystemControllerLog)")
    public void controllerAspect(){
    }

    /**
     * @Description  前置通知  用于拦截Controller层记录用户的操作
     * @date 2018年9月3日 10:38
     */

    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        //读取session中的用户
        LoginInfo logininfo = new LoginInfo();
        User user = logininfo.getUser(request);
        String ip = IPUtils.getLocalIP();

        try {
            //*========控制台输出=========*//
            System.out.println("==============前置通知开始==============");
            System.out.println("请求方法" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName()));
            System.out.println("方法描述:" + getControllerMethodDescription(joinPoint));
            System.out.println("请求人:"+user.getUsername());
//            System.out.println("请求ip:"+ip);

            //*========数据库日志=========*//
            Action action = new Action();
            action.setActionDes(getControllerMethodDescription(joinPoint));
            action.setActionType("0");
            action.setActionIp(ip);
            action.setUserId(user.getId().toString());
            action.setActionTime(new Date());
            action.setUserName(user.getUsername());
            //保存数据库
            actionService.save(action);

        }catch (Exception e){
            //记录本地异常日志
            logger.error("==前置通知异常==");
            logger.error("异常信息:{}",e.getMessage());
        }
    }

    /**
     * @Description  异常通知 用于拦截service层记录异常日志
     * @date 2018年9月3日 下午5:43
     */
    @AfterThrowing(pointcut = "serviceAspect()",throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint,Throwable e){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();

        LoginInfo logininfo = new LoginInfo();
        User user = logininfo.getUser(request);
        String ip = IPUtils.getLocalIP();
        //获取用户请求方法的参数并序列化为JSON格式字符串
        String params = "";
        if (joinPoint.getArgs()!=null&&joinPoint.getArgs().length>0){
            for (int i = 0; i < joinPoint.getArgs().length; i++) {
                params+= JsonUtils.objectToJson(joinPoint.getArgs()[i])+";";
            }
        }
        try{
            /*========控制台输出=========*/
            System.out.println("=====异常通知开始=====");
            System.out.println("异常代码:" + e.getClass().getName());
            System.out.println("异常信息:" + e.getMessage());
            System.out.println("异常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
            System.out.println("方法描述:" + getServiceMethodDescription(joinPoint));
            System.out.println("请求人:" + user.getUsername());
            System.out.println("请求IP:" + ip);
            System.out.println("请求参数:" + params);
            /*==========数据库日志=========*/
            Action action = new Action();
            action.setActionDes(getServiceMethodDescription(joinPoint));
            action.setActionType("1");
            action.setUserId(user.getId().toString());
            action.setActionIp(ip);
            action.setActionTime(new Date());
            action.setUserName(user.getUsername());
            //保存到数据库
            actionService.save(action);
        }catch (Exception ex){
            //记录本地异常日志
            logger.error("==异常通知异常==");
            logger.error("异常信息:{}", ex.getMessage());
        }
    }


    /**
     * @Description  获取注解中对方法的描述信息 用于service层注解
     * @date 2018年9月3日 下午5:05
     */
    public static String getServiceMethodDescription(JoinPoint joinPoint)throws Exception{
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String description = "";
        for (Method method:methods) {
            if (method.getName().equals(methodName)){
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length==arguments.length){
                    description = method.getAnnotation(SystemServiceLog.class).description();
                    break;
                }
            }
        }
        return description;
    }



    /**
     * @Description  获取注解中对方法的描述信息 用于Controller层注解
     * @date 2018年9月3日 上午12:01
     */
    public static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();//目标方法名
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String description = "";
        for (Method method:methods) {
            if (method.getName().equals(methodName)){
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length==arguments.length){
                    description = method.getAnnotation(SystemControllerLog.class).description();
                    break;
                }
            }
        }
        return description;
    }
}

创建日志表:

/*
		 Navicat Premium Data Transfer

		 Source Server         : 192.168.0.124--mysql
		 Source Server Type    : MySQL
		 Source Server Version : 50635
		 Source Host           : 192.168.0.124:3306
		 Source Schema         : boot_basic

		 Target Server Type    : MySQL
		 Target Server Version : 50635
		 File Encoding         : 65001

		 Date: 21/06/2021 11:56:57
		*/

		SET NAMES utf8mb4;
		SET FOREIGN_KEY_CHECKS = 0;

		-- ----------------------------
		-- Table structure for a_user_operation
		-- ----------------------------
		DROP TABLE IF EXISTS `a_user_operation`;
		CREATE TABLE `a_user_operation`  (
		  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
		  `action_des` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
		  `action_type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
		  `action_ip` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
		  `user_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
		  `action_time` datetime(0) NULL DEFAULT NULL,
		  PRIMARY KEY (`id`) USING BTREE
		) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

		SET FOREIGN_KEY_CHECKS = 1;

创建实体:

package com.aeh.basic.model;

		import javax.persistence.*;
		import java.util.Date;

		@Entity
		@Table(name="a_user_operation")
		public class Action {
			@Id
			@GeneratedValue(strategy = GenerationType.AUTO)
			private Integer id;
			private String ActionDes;
			private String ActionType;
			private String ActionIp;
			private String UserId;
			@Column(name="action_time")
			private Date ActionTime;

			public Integer getId() {
				return id;
			}

			public void setId(Integer id) {
				this.id = id;
			}

			public String getActionDes() {
				return ActionDes;
			}

			public void setActionDes(String actionDes) {
				ActionDes = actionDes;
			}

			public String getActionType() {
				return ActionType;
			}

			public void setActionType(String actionType) {
				ActionType = actionType;
			}

			public String getActionIp() {
				return ActionIp;
			}

			public void setActionIp(String actionIp) {
				ActionIp = actionIp;
			}

			public String getUserId() {
				return UserId;
			}

			public void setUserId(String userId) {
				UserId = userId;
			}

			public Date getActionTime() {
				return ActionTime;
			}

			public void setActionTime(Date actionTime) {
				ActionTime = actionTime;
			}
		}

创建service:

package com.aeh.basic.service;

		import com.aeh.basic.model.User;
		import org.springframework.data.jpa.repository.JpaRepository;
		import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
		import org.springframework.data.jpa.repository.Query;

		import java.util.List;

		/**
		 * Created by  zxn
		 */
		public interface IActionService extends BaseRepository<User, Integer>,JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {
			
		}

添加获取ip的工具类:

package com.aeh.basic.utils;


		import javax.servlet.http.HttpServletRequest;
		import java.io.IOException;
		import java.net.Inet4Address;
		import java.net.InetAddress;
		import java.net.NetworkInterface;
		import java.net.UnknownHostException;
		import java.util.ArrayList;
		import java.util.Collections;
		import java.util.Enumeration;
		import java.util.List;
		import java.util.concurrent.BrokenBarrierException;
		import java.util.concurrent.CyclicBarrier;


		public class IPUtils {
			/**
			 * 获取所在内网所有IP
			 * @param args
			 * @throws InterruptedException
			 */
			public static void gainAllIp() throws InterruptedException {
				final List<String> innerIp = Collections.synchronizedList(new ArrayList<String>());
				final CyclicBarrier barrier = new CyclicBarrier(255, new IpShow(innerIp));
				String hostAddress = getLocalIP();
				int pos = hostAddress.lastIndexOf(".");
				String wd = hostAddress.substring(0, pos + 1);
				for (int i = 1; i <= 255; i++) {
					String ip = wd + i;
					PingIpThread thread = new IPUtils.PingIpThread(ip, barrier, innerIp);
					thread.start();
				}
			}

			public static class IpShow implements Runnable {
				private List<String> innerIp;
				public IpShow(List<String> innerIp) {
					this.innerIp = innerIp;
				}
				@Override
				public void run() {
					for (String ip : innerIp) {
						System.out.println(ip);
					}
				}
			}

			public static class PingIpThread extends Thread {
				private String ip;
				private CyclicBarrier barrier;
				private List<String> list;
				public PingIpThread(String ip, CyclicBarrier barrier, List<String> list) {
					this.ip = ip;
					this.barrier = barrier;
					this.list = list;
				}
				public void run() {
					try {
						if (InetAddress.getByName(ip).isReachable(5000))
							list.add(ip);
						barrier.await();
					} catch (UnknownHostException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
		//			} catch (InterruptedException e) {
		//				e.printStackTrace();
		//			}
					catch (InterruptedException e) {
						e.printStackTrace();
					} catch (BrokenBarrierException e) {
						e.printStackTrace();
					}
				}
			}

			public static String  getLocalIP() {
				try {
					return InetAddress.getLocalHost().getHostAddress();
				} catch (UnknownHostException e) {
					e.printStackTrace();
					return null;
				}
			}
			public static String getLocalHostName() {
				try {
					return InetAddress.getLocalHost().getHostName();
				} catch (UnknownHostException e) {
					e.printStackTrace();
					return null;
				}
			}
			public static String getCanonicalHostName() {
				try {
					return InetAddress.getLocalHost().getCanonicalHostName();
				} catch (UnknownHostException e) {
					e.printStackTrace();
					return null;
				}
			}
			public static String getIPFromDomainName(String domainName) {
				try {
					return InetAddress.getByName(domainName).getHostAddress();
				} catch (UnknownHostException e) {
					e.printStackTrace();
					return null;
				}
			}
			public static String getDomainNameFromIP(String ip) {
				try {
					return InetAddress.getByAddress(getIPBytes(ip)).getCanonicalHostName();
				} catch (UnknownHostException e) {
					e.printStackTrace();
					return null;
				}
			}
			private static byte[] getIPBytes(String ip) {
				byte[] ipBytes = new byte[4];
				String[] ipStr = ip.split("[.]");

				for (int i = 0; i < 4; i++) {
					int m = Integer.parseInt(ipStr[i]);
					byte b= (byte)(m & 0xff);
					ipBytes[i] = b;
				}
				return ipBytes;
			}

			public static void main(String[] args) throws InterruptedException {
				gainAllIp();
			}
		}

jsonuntils:

package com.aeh.basic.utils;

		import net.sf.json.JSONArray;
		import net.sf.json.JSONObject;

		import java.beans.IntrospectionException;
		import java.beans.Introspector;
		import java.beans.PropertyDescriptor;
		import java.io.BufferedReader;
		import java.io.InputStream;
		import java.io.InputStreamReader;
		import java.net.URL;
		import java.util.*;

		public class JsonUtils {
			/**
			 * 一个String字符串转换为json格式
			 *
			 * @param s
			 * @return
			 * @descript
			 * @author Jacky
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static String stringToJson(String s) {
				if (s == null) {
					return nullToJson();
				}
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < s.length(); i++) {
					char ch = s.charAt(i);
					switch (ch) {
						case '"':
							sb.append("\\\"");
							break;
						case '\\':
							sb.append("\\\\");
							break;
						case '\b':
							sb.append("\\b");
							break;
						case '\f':
							sb.append("\\f");
							break;
						case '\n':
							sb.append("\\n");
							break;
						case '\r':
							sb.append("\\r");
							break;
						case '\t':
							sb.append("\\t");
							break;
						case '/':
							sb.append("\\/");
							break;
						default:
							if (ch >= '\u0000' && ch <= '\u001F') {
								String ss = Integer.toHexString(ch);
								sb.append("\\u");
								for (int k = 0; k < 4 - ss.length(); k++) {
									sb.append('0');
								}
								sb.append(ss.toUpperCase());
							} else {
								sb.append(ch);
							}
					}
				}
				return sb.toString();
			}

			public static String nullToJson() {
				return "";
			}

			/**
			 * 一个obj对象转换为json格式
			 *
			 * @param obj
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static String objectToJson(Object obj) {
				StringBuilder json = new StringBuilder();
				if (obj == null) {
					json.append("\"\"");
				} else if (obj instanceof Number) {
					json.append(numberToJson((Number) obj));
				} else if (obj instanceof Boolean) {
					json.append(booleanToJson((Boolean) obj));
				} else if (obj instanceof String) {
					json.append("\"").append(stringToJson(obj.toString())).append("\"");
				} else if (obj instanceof Object[]) {
					json.append(arrayToJson((Object[]) obj));
				} else if (obj instanceof List) {
					json.append(listToJson((List<?>) obj));
				} else if (obj instanceof Map) {
					json.append(mapToJson((Map<?, ?>) obj));
				} else if (obj instanceof Set) {
					json.append(setToJson((Set<?>) obj));
				} else {
					json.append(beanToJson(obj));
				}
				return json.toString();
			}

			public static String numberToJson(Number number) {
				return number.toString();
			}

			public static String booleanToJson(Boolean bool) {
				return bool.toString();
			}

			/**
			 * 一个bean对象转换为json格式
			 *
			 * @param bean
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static String beanToJson(Object bean) {
				StringBuilder json = new StringBuilder();
				json.append("{");
				PropertyDescriptor[] props = null;
				try {
					props = Introspector.getBeanInfo(bean.getClass(), Object.class)
							.getPropertyDescriptors();
				} catch (IntrospectionException e) {
				}
				if (props != null) {
					for (int i = 0; i < props.length; i++) {
						try {
							String name = objectToJson(props[i].getName());
							String value = objectToJson(props[i].getReadMethod()
									.invoke(bean));
							json.append(name);
							json.append(":");
							json.append(value);
							json.append(",");
						} catch (Exception e) {
						}
					}
					json.setCharAt(json.length() - 1, '}');
				} else {
					json.append("}");
				}
				return json.toString();
			}

			/**
			 * @param list
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static String listToJson(List<?> list) {
				StringBuilder json = new StringBuilder();
				json.append("[");
				if (list != null && list.size() > 0) {
					for (Object obj : list) {
						json.append(objectToJson(obj));
						json.append(",");
					}
					json.setCharAt(json.length() - 1, ']');
				} else {
					json.append("]");
				}
				return json.toString();
			}

			/**
			 * 一个数组集合转换为json格式
			 *
			 * @param array
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static String arrayToJson(Object[] array) {
				StringBuilder json = new StringBuilder();
				json.append("[");
				if (array != null && array.length > 0) {
					for (Object obj : array) {
						json.append(objectToJson(obj));
						json.append(",");
					}
					json.setCharAt(json.length() - 1, ']');
				} else {
					json.append("]");
				}
				return json.toString();
			}

			/**
			 * 一个Map集合转换为json格式
			 *
			 * @param map
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static String mapToJson(Map<?, ?> map) {
				StringBuilder json = new StringBuilder();
				json.append("{");
				if (map != null && map.size() > 0) {
					for (Object key : map.keySet()) {
						json.append(objectToJson(key));
						json.append(":");
						json.append(objectToJson(map.get(key)));
						json.append(",");
					}
					json.setCharAt(json.length() - 1, '}');
				} else {
					json.append("}");
				}
				return json.toString();
			}

			/**
			 * 一个Set集合转换为json格式
			 *
			 * @param set
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static String setToJson(Set<?> set) {
				StringBuilder json = new StringBuilder();
				json.append("[");
				if (set != null && set.size() > 0) {
					for (Object obj : set) {
						json.append(objectToJson(obj));
						json.append(",");
					}
					json.setCharAt(json.length() - 1, ']');
				} else {
					json.append("]");
				}
				return json.toString();
			}

			/**
			 * json字符串转换为List
			 *
			 * @param jsonStr
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static List<Map<String, Object>> parseJSONList(String jsonStr) {
				JSONArray jsonArr = JSONArray.fromObject(jsonStr);
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				Iterator<JSONObject> it = jsonArr.iterator();
				while (it.hasNext()) {
					JSONObject JSON = it.next();
					list.add(parseJSONMap(JSON.toString()));
				}
				return list;
			}

			/**
			 * json字符串转换为map
			 *
			 * @param jsonStr
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static Map<String, Object> parseJSONMap(String jsonStr) {
				Map<String, Object> map = new HashMap<String, Object>();
				try {
					//最外层解析
					JSONObject json = JSONObject.fromObject(jsonStr);
					for (Object k : json.keySet()) {
						Object v = json.get(k);
						//如果内层还是数组的话,继续解析
						if (v instanceof JSONArray) {
							List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
							Iterator<JSONObject> it = ((JSONArray) v).iterator();
							while (it.hasNext()) {
								JSONObject JSON = it.next();
								list.add(parseJSONMap(JSON.toString()));
							}
							map.put(k.toString(), list);
						} else {
							map.put(k.toString(), v);
						}
					}
				} catch (Exception e) {
					map.put("exception", jsonStr);
				}
				return map;
			}

			/**
			 * 根据一个url地址.获取json数据.转换为List
			 *
			 * @param url
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static List<Map<String, Object>> getListByUrl(String url) {
				try {
					//通过HTTP获取JSON数据
					InputStream in = new URL(url).openStream();
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					StringBuilder sb = new StringBuilder();
					String line;
					while ((line = reader.readLine()) != null) {
						sb.append(line);
					}
					return parseJSONList(sb.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}

			/**
			 * 根据一个url地址.获取json数据.转换为MAP
			 *
			 * @param url
			 * @return
			 * @descript
			 * @author lijianning
			 * @date 2015年6月15日
			 * @version 1.0v
			 */
			public static Map<String, Object> getMapByUrl(String url) {
				try {
					//通过HTTP获取JSON数据
					InputStream in = new URL(url).openStream();
					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
					StringBuilder sb = new StringBuilder();
					String line;
					while ((line = reader.readLine()) != null) {
						sb.append(line);
					}
					return parseJSONMap(sb.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		}

怎么用

在这里插入图片描述

大佬勿喷,欢迎提意见建议评论!!!!这些工具可直接用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值