最后一个方法:核心的日志记录方法

package com.migu.cm.aspect;

import com.alibaba.fastjson.JSON;

import com.migu.cm.domain.UserOperationLog;

import com.migu.cm.service.UserOperationLogService;

import com.migu.cm.utils.Slf4jLogUtil;

import com.migu.cm.utils.ThreadLocalUtil;

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.annotation.Order;

import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.security.core.userdetails.UserDetails;

import org.springframework.stereotype.Component;

import org.springframework.util.StringUtils;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.net.URLDecoder;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.UUID;

@Aspect

@Order(1)

@Component

public class WebLogAspect {

/**
* 站位符:requestUrl>methodType>DeclaringTypeName.SignatureName>requestParams
*/
private final static String BEFORE_MATCH_STANCE_LOG = "{} {} {} {}"; /**
* 站位符:requestUrl>methodType>DeclaringTypeName.SignatureName>requestParams>responseInfo>cost
*/
private final static String AFTER_MATCH_STANCE_LOG = "{} {} {} {} {} {}"; @Autowired
private UserOperationLogService userOperationLogService; @Pointcut("execution(public * com.migu.cm.web..*.*(..))")
public void webLog() {
} @Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
ThreadLocalUtil.set("startTime", System.currentTimeMillis());
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
ThreadLocalUtil.set("loggerTag", request.getHeader("loggerTag") == null ? UUID.randomUUID().toString() : request.getHeader("loggerTag"));
StringBuffer requestUrl = new StringBuffer(request.getRequestURL());
String requestParam = getRequestParam(joinPoint, request);
recordHandlers();
if (!StringUtils.isEmpty(request.getQueryString())) {
requestUrl.append("?").append(URLDecoder.decode(request.getQueryString(), "utf-8"));
}
Slf4jLogUtil.SimpleLogUtil.infoToController(BEFORE_MATCH_STANCE_LOG, requestUrl.toString()
, request.getMethod(), joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()
, requestParam); // 用户操作记录(对数据增,删,改)
logInterception(request);
} /**
* 记录每次请求的操作者记录
*/

private void recordHandlers() {

//打印请求用户名

try {

if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof UserDetails) {

UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

if (userDetails != null) {

StringBuilder strBuilder = new StringBuilder();

strBuilder.append("username:").append(userDetails.getUsername());

Slf4jLogUtil.SimpleLogUtil.info(strBuilder.toString());

}

}

} catch (Exception e) {

Slf4jLogUtil.SimpleLogUtil.error("doBefore error", e);

}

}

@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(JoinPoint joinPoint, Object ret) throws Throwable {
// 处理完请求,返回内容
Object[] object = joinPoint.getArgs();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
StringBuffer requestUrl = new StringBuffer(request.getRequestURL());
if (!StringUtils.isEmpty(request.getQueryString())) {
requestUrl.append("?").append(URLDecoder.decode(request.getQueryString(), "utf-8"));
}
String requestParam = getRequestParam(joinPoint, request);
String responseParam = getResponseParam(ret);
Slf4jLogUtil.SimpleLogUtil.infoToController(AFTER_MATCH_STANCE_LOG, requestUrl.toString()
, request.getMethod(), joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()
, requestParam, responseParam
, (System.currentTimeMillis() - (long) ThreadLocalUtil.get("startTime")));
ThreadLocalUtil.remove();
} /**
* 根据请求方式获取requestParam
*
* @param joinPoint
* @param request
* @return
*/
private String getRequestParam(JoinPoint joinPoint, HttpServletRequest request) {
String params = "[]";
if (request.getContentType() != null) {
switch (request.getContentType()) {
case "application/x-www-form-urlencoded":
params = StringUtils.isEmpty(request.getParameterMap()) ? "[]" : JSON.toJSONString(request.getParameterMap());
break;
case "application/json":
params = JSON.toJSONString(joinPoint.getArgs());
break;
case "application/x-www-form-urlencoded; charset=UTF-8":
params = StringUtils.isEmpty(request.getParameterMap()) ? "[]" : JSON.toJSONString(request.getParameterMap());
break;
default:
params = "[]";
break;
}
}
return params;
} /**
* 根据实际业务处理输出的responseParam
*
* @param ret
* @return
*/
private String getResponseParam(Object ret) {
if (ret instanceof String) {
Map returnUrl = new HashMap(16);
returnUrl.put("returnAddress", ret);
return JSON.toJSONString(returnUrl);
}
return JSON.toJSONString(ret);
} private final String UNKNOWN = "unknown"; /***
* 用户操作记录,(对数据库增,删,改)
* @param request
*/

public void logInterception(HttpServletRequest request ) {

try {

        // TODO 这里处理下, 如果没有2个  / /  就不要拦截了, 造成的index 越界不好
String requestURI = request.getRequestURI();
String path = requestURI.substring(1);
int index = path.indexOf("/");
String sub2 = path.substring(index+1);
int index2 = sub2.indexOf("/");
String sub3 = sub2.substring(0, index2);
if (sub3.indexOf("add") >= 0 || sub3.indexOf("update") >= 0 || sub3.indexOf("cancel") >= 0 || sub3.indexOf("del") >= 0 || sub3.indexOf("toTop") >= 0
|| sub3.indexOf("make") >= 0 || sub3.indexOf("effect") >= 0 || sub3.indexOf("save") >= 0 || sub3.indexOf("insert") >= 0
|| sub3.indexOf("login") >= 0 || sub3.indexOf("edit") >= 0 || sub3.indexOf("approve") >= 0 || sub3.indexOf("modify") >= 0
|| sub3.indexOf("active") >= 0 || sub3.indexOf("doPackage") >= 0 || sub3.indexOf("doDownload") >= 0 || sub3.indexOf("close") >= 0
|| sub3.indexOf("remove") >= 0) {
UserOperationLog userOperationLog = new UserOperationLog();
//日志记录
try {
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof UserDetails) {
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (userDetails != null) {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("username:").append(userDetails.getUsername());
userOperationLog.setUsername(userDetails.getUsername());
Slf4jLogUtil.SimpleLogUtil.info(strBuilder.toString());
}
}
} catch (Exception e) {
Slf4jLogUtil.SimpleLogUtil.error("doBefore error", e);
}
userOperationLog.setPath(requestURI);
//时间
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.getRemoteAddr();
}
userOperationLog.setIp(ip);
userOperationLog.setCreatetime(new Date());
int paramsIndex = request.getQueryString().lastIndexOf("=");
String params = request.getQueryString().substring(0,paramsIndex-2);
userOperationLog.setParams(params);
userOperationLogService.saveInfo(userOperationLog);
}
} catch (Exception e) {
Slf4jLogUtil.SimpleLogUtil.error("用户操作记录获取异常:", e);
}
}

}

Spring AOP使用注解记录用户操作日志的更多相关文章

  1. RabbitMQ实战场景(一):异步记录用户操作日志

    传统的项目开发中业务流程以串行方式,执行了模块1—>模块2–>模块3 而我们知道,这个执行流程其实对于整个程序来讲是有一定的弊端的,主要有几点: (1)整个流程的执行响应等待时间比较长; ...

  2. ssm 项目记录用户操作日志和异常日志

    借助网上参考的内容,写出自己记录操作日志的心得!! 我用的是ssm项目使用aop记录日志:这里用到了aop的切点 和 自定义注解方式: 1.建好数据表: 数据库记录的字段有: 日志id .操作人.操作 ...

  3. springAOP记录用户操作日志

    项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用springAOP来完成比较合适. 注解工具类: @Retention(RetentionPolicy.RUNTIME) @Tar ...

  4. linux 记录用户操作日志

    将以下加入到/etc/profile 最后 history USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]/ ...

  5. 我使用Spring AOP实现了用户操作日志功能

    我使用Spring AOP实现了用户操作日志功能 今天答辩完了,复盘了一下系统,发现还是有一些东西值得拿出来和大家分享一下. 需求分析 系统需要对用户的操作进行记录,方便未来溯源 首先想到的就是在每个 ...

  6. 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录

    在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...

  7. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  8. spring AOP自定义注解 实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  9. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

随机推荐

  1. linux 常用命令及实例

    转载:https://www.cnblogs.com/xiaoyafei/p/9163976.html 在linux中,绝大多数命令都参照 命令 选项 参数 选项:适用于调整命令的功能的 参数:指的是 ...

  2. 《xxx系统》质量属性战术

    <xxx系统>质量属性战术 可用性:重新引入 用户每填写一份表单,表单查看中即时更新所有信息. 易用性:系统主动 对于下拉框的选项较多时,用户可先进行部分输入,系统进行实时检索显示与用户输 ...

  3. web3js 进行转账

    1.准备阶段 部署以太坊geth 安装nodejs npm install web3 npm install npm install ethereumjs-tx 其中, web3是1.0.0.beta ...

  4. Selenium之table操作

    操作内容: 获取table总行数.总列数.获取某单元格的text值,删除一行[如果每行后边提供删除的按钮] HTML代码: <html><head><meta http- ...

  5. 3.13 练习题4:邮件发送(smtp)

    3.13 练习题4:邮件发送(smtp) 前言本篇总结了QQ邮箱和163邮箱发送邮件,邮件包含html中文和附件,可以发给多个收件人,专治各种不行,总之看完这篇麻麻再也不用担心我的邮件收不到了.以下代 ...

  6. 安卓开发学习之Menu

    安卓开发中菜单是一个很重要的组件,从安卓开发文档(http://wear.techbrood.com/guide/index.html)中可以看到,安卓UI设计中的Menu主要分为: A.Option ...

  7. Codeforces1099F. Cookies(线段树+dp+贪心+博弈)

    题目链接:传送门 思路: 分析到处理节点时的吃cookie的顺序了,然鹅不会用线段树维护前缀和.技术门槛QAQ... 很容易想到可以从root开始搜索,每次深入消耗时间2*边权w. 然后对于深入到点u ...

  8. 再谈Retina下1px的解决方案

    https://www.w3cplus.com/css/fix-1px-for-retina.html

  9. 19.python的序列化

    自定制序列化 import json from datetime import datetime class JsonCustomEncoder(json.JSONEncoder): #自定制序列化类 ...

  10. Centos7.4下安装Jumpserver 1.0.0(支持windows组件)

    0)系统环境CentOS 7.4 IP: 192.168.100.10 [root@jumpserver-server ~]# cat /etc/redhat-release CentOS Linux ...