分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

切面保存web访问记录

发布时间:2023-09-06 01:50责任编辑:苏小强关键词:暂无标签
package com.hn.xf.device.api.rest.aspect;import com.hn.xf.device.api.rest.authorization.manager.TokenManager;import com.hn.xf.device.api.rest.authorization.model.TokenModel;import com.hn.xf.device.api.rest.config.Constants;import com.hn.xf.device.service.log.service.AppLogService;import lombok.extern.slf4j.Slf4j;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.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import java.util.Arrays;/** * @Created with IntelliJ IDEA. * @author: fengshufang * @Date: 2018/1/22 * @Description APP日志 */@Aspect@Component@Slf4jpublic class WebLogAspect { ???@Autowired ???AppLogService appLogService; ???@Autowired ???private TokenManager manager; ???@Pointcut("execution(public * com.hn.xf.device.api.rest.controller..*(..))") ???public void webLog() { ???} ???@Before("webLog()") ???public void doBefore(JoinPoint joinPoint) throws Throwable { ???????// 接收到请求,记录请求内容 ???????ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ???????HttpServletRequest request = attributes.getRequest(); ???????String authorization = request.getHeader(Constants.AUTHORIZATION); ???????String user=""; ???????String cpid = request.getHeader(Constants.CPID); ???????if (cpid == null ) { ???????????//验证token ???????????TokenModel model = manager.getToken(authorization); ???????????if(model!=null) ???????????{ ???????????????user=model.getUserId().toString(); ???????????} ???????} ???????else { ???????????user=cpid; ???????} ???????if (Arrays.toString(joinPoint.getArgs()).toString().length() < 500) { ???????????appLogService.add(user, joinPoint.getSignature().getName(), request.getRequestURL().toString() + request.getRemoteAddr() + Arrays.toString(joinPoint.getArgs()).toString()); ???????} else { ???????????String tempData = Arrays.toString(joinPoint.getArgs()).toString().substring(0,499); ???????????appLogService.add(user, joinPoint.getSignature().getName(), request.getRequestURL().toString() + request.getRemoteAddr() + tempData); ???????} ???} ???@AfterReturning(returning = "ret", pointcut = "webLog()") ???public void doAfterReturning(Object ret) throws Throwable { ???????// 处理完请求,返回内容 ???????//log.info("RESPONSE : " + ret); ???}}

  

切面保存web访问记录

原文地址:https://www.cnblogs.com/anakin/p/8907324.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved