提高网站建设管理水平,深圳网站开发兼职,加强网站建设和信息公开,网站优化包括对什么优化Spring之Aop切面---日志收集#xff08;环绕处理、前置处理方式#xff09;--使用/教程/实例 简介系统登录日志类LoginLogEntity .java 一、环绕处理方式1、自定义注解类LoginLogAop.class2、切面处理类LogoutLogAspect.java 二、前置处理方式#xff1a;1、自定义注解类Log… Spring之Aop切面---日志收集环绕处理、前置处理方式--使用/教程/实例 简介系统登录日志类LoginLogEntity .java 一、环绕处理方式1、自定义注解类LoginLogAop.class2、切面处理类LogoutLogAspect.java 二、前置处理方式1、自定义注解类LogoutLogAop.class2、切面处理类LogoutLogAspect.java 三、Proceedingjoinpoint简述 简介
本文章介绍采用两种不同方式处理----系统登录、系统退出登录两种场景日志。
环绕处理系统登录日志前置处理系统退出登录日志
系统登录日志类LoginLogEntity .java
package com.fy.test.entity;import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;import java.io.Serializable;
import java.time.LocalDateTime;import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;/*** ClassName: LoginLogEntity * Description: * Author fy* Date 2023/07/10 9:00*/
Data
Accessors(chain true)
TableName(t_login_log)
public class LoginLogEntity implements Serializable {private static final long serialVersionUID 1L;/*** 主键*/TableId(value id, type IdType.ASSIGN_ID)private String id;/*** 操作系统*/private String opOs;/*** 浏览器类型*/private String opBrowser;/*** 登录IP地址*/private String opIp;/*** 登录时间*/private LocalDateTime opDate;/*** 登录用户ID*/private String userId;/*** 登录用户名称*/private String userName;/*** 错误类型*/private String exCode;/*** 错误信息*/private String exMsg;/*** 登录状态*/private boolean status;/*** 描述*/private String desc;
}
一、环绕处理方式
1、自定义注解类LoginLogAop.class
package com.fy.test.log.annotation;import java.lang.annotation.*;/*** ClassName: LoginLogAop* Description: * Author fy* Date 2023/07/10 9:05*/
Target({ElementType.PARAMETER, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
Documented
public interface LoginLogAop {/*** 描述*/String desc() default ;}
2、切面处理类LogoutLogAspect.java
package com.fy.test.log.aspect;import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.fy.test.log.annotation.LoginLogAop;
import com.fy.test.utils.RequestHolder;
import com.fy.test.utils.SecurityUtil;
import com.fy.test.service.dto.LoginLogDto;
import com.fy.test.service.feign.LogServiceFeign;
import com.fy.test.service.vo.UserVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;/*** ClassName: LoginLogAspect* Description:* Author fy* Date 2023/07/10 9:05*/
Slf4j
Aspect
public class LoginLogAspect {Autowiredprivate LogServiceFeign logServiceFeign;/*** 配置织入点*/Pointcut(annotation(com.fy.test.common.log.annotation.LoginLogAop))public void logPointCut() {}/*** 通知方法会将目标方法封装起来* 注意环绕方式选择ProceedingJoinPoint* Proceedingjoinpoint 继承了JoinPoint在JoinPoint的基础上暴露出 proceed() 这个方法是AOP代理链执行的方法。* JoinPoint仅能获取相关参数无法执行连接点。* 暴露出proceed()这个方法就能支持 aop:around 这种切面而其他的几种切面只需要用到JoinPoint这跟切面类型有关* 就能控制走代理链还是走自己拦截的其他逻辑。 * * param joinPoint 切点*/Around(value logPointCut())public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {Object result joinPoint.proceed();LoginLogDto logDto getLog();logDto.setStatus(true);handleLog(joinPoint, logDto);return result;}/*** 通知方法会在目标方法抛出异常后执行** param joinPoint* param e*/AfterThrowing(value logPointCut(), throwing e)public void doAfterThrowing(JoinPoint joinPoint, Exception e) {LoginLogDto logDto getLog();logDto.setExCode(e.getClass().getSimpleName()).setExMsg(e.getMessage());logDto.setStatus(false);handleLog(joinPoint, logDto);}private LoginLogDto getLog() {HttpServletRequest request RequestHolder.getHttpServletRequest();UserAgent userAgent UserAgentUtil.parse(request.getHeader(User-Agent));LoginLogDto loginLog new LoginLogDto();loginLog.setOpIp(ServletUtil.getClientIP(request)).setOpOs(userAgent.getOs().getName()).setOpBrowser(userAgent.getBrowser().getName()).setUserId(SecurityUtil.getUserId()).setUserName(SecurityUtil.getUserName()).setOpDate(LocalDateTime.now());return loginLog;}protected void handleLog(final JoinPoint joinPoint, LoginLogDto loginLogDto) {// 获得注解LoginLogAop logAop getAnnotationLog(joinPoint);if (null logAop) {return;}loginLogDto.setDescription(logAop.description());MapString, Object requestParams getRequestParams(joinPoint);if (requestParams.containsKey(userVo)) {UserVo userVo JSONObject.parseObject(JSON.toJSONString(requestParams.get(userVo)), UserVo.class);if (null ! userVo StringUtils.isBlank(loginLogDto.getUserName())) {loginLogDto.setUserName(userVo.getUsername());}}// 保存数据库logServiceFeign.saveLoginLog(loginLogDto);}/*** 是否存在注解如果存在就获取*/private LoginLogAop getAnnotationLog(JoinPoint joinPoint) {Signature signature joinPoint.getSignature();MethodSignature methodSignature (MethodSignature) signature;Method method methodSignature.getMethod();if (method ! null) {return method.getAnnotation(LoginLogAop.class);}return null;}/*** 获取入参*/private MapString, Object getRequestParams(JoinPoint joinPoint) {MapString, Object requestParams new HashMap();// 参数名String[] paramNames ((MethodSignature) joinPoint.getSignature()).getParameterNames();// 参数值Object[] paramValues joinPoint.getArgs();for (int i 0; i paramNames.length; i) {Object value paramValues[i];// 如果是文件对象if (value instanceof MultipartFile) {MultipartFile file (MultipartFile) value;// 获取文件名value file.getOriginalFilename();}requestParams.put(paramNames[i], value);}return requestParams;}
}二、前置处理方式
1、自定义注解类LogoutLogAop.class
package com.fy.test.log.annotation;import java.lang.annotation.*;/*** ClassName: LogoutLogAop* Description: * Author fy* Date 2023/07/10 9:10*/
Target({ElementType.PARAMETER, ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
Documented
public interface LogoutLogAop {/*** 描述*/String desc() default ;}
2、切面处理类LogoutLogAspect.java
package com.fy.test.log.aspect;import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.fy.test.log.annotation.LoginLogAop;
import com.fy.test.log.annotation.LogoutLogAop;
import com.fy.test.utils.RequestHolder;
import com.fy.test.utils.SecurityUtil;
import com.fy.test.service.dto.LoginLogDto;
import com.fy.test.service.feign.LogServiceFeign;
import com.fy.test.service.vo.UserVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;/*** ClassName: LogoutLogAspect* Description:* Author fy* Date 2023/07/10 9:10*/
Slf4j
Aspect
public class LogoutLogAspect {Autowiredprivate LogServiceFeign logServiceFeign;/*** 配置织入点*/Pointcut(annotation(com.fy.test.log.annotation.LogoutLogAop))public void logPointCut() {}/*** 通知方法会将目标方法封装起来** param joinPoint 切点*/Before(logPointCut())public void doBefore(JoinPoint joinPoint) throws Throwable {try {System.out.println(前置处理开始);LoginLogDto logDto getLog();logDto.setStatus(true);handleLog(joinPoint, logDto);} catch (Exception e) {//记录本地异常日志log.error(前置通知异常);log.error(异常信息{}, e.getMessage());}}/*** 通知方法会在目标方法抛出异常后执行** param joinPoint* param e*/AfterThrowing(value logPointCut(), throwing e)public void doAfterThrowing(JoinPoint joinPoint, Exception e) {LoginLogDto logDto getLog();logDto.setExCode(e.getClass().getSimpleName()).setExMsg(e.getMessage());logDto.setStatus(false);handleLog(joinPoint, logDto);}private LoginLogDto getLog() {HttpServletRequest request RequestHolder.getHttpServletRequest();UserAgent userAgent UserAgentUtil.parse(request.getHeader(User-Agent));LoginLogDto loginLogDto new LoginLogDto();loginLogDto.setOpIp(ServletUtil.getClientIP(request)).setOpOs(userAgent.getOs().getName()).setOpBrowser(userAgent.getBrowser().getName()).setUserId(SecurityUtil.getUserId()).setUserName(SecurityUtil.getUserName()).setOpDate(LocalDateTime.now());return loginLogDto;}protected void handleLog(final JoinPoint joinPoint, LoginLogDto loginLogDto) {// 获得注解LogoutLogAop logAop getAnnotationLog(joinPoint);if (null logAop) {return;}loginLogDto.setDesc(logAop.desc());// 保存数据库logServiceFeign.saveLoginLog(loginLogDto);}/*** 是否存在注解如果存在就获取*/private LogoutLogAop getAnnotationLog(JoinPoint joinPoint) {Signature signature joinPoint.getSignature();MethodSignature methodSignature (MethodSignature) signature;Method method methodSignature.getMethod();if (method ! null) {return method.getAnnotation(LogoutLogAop.class);}return null;}/*** 获取入参*/private MapString, Object getRequestParams(JoinPoint joinPoint) {MapString, Object requestParams new HashMap();// 参数名String[] paramNames ((MethodSignature) joinPoint.getSignature()).getParameterNames();// 参数值Object[] paramValues joinPoint.getArgs();for (int i 0; i paramNames.length; i) {Object value paramValues[i];// 如果是文件对象if (value instanceof MultipartFile) {MultipartFile file (MultipartFile) value;// 获取文件名value file.getOriginalFilename();}requestParams.put(paramNames[i], value);}return requestParams;}
}三、Proceedingjoinpoint简述
Proceedingjoinpoint 继承了JoinPoint在JoinPoint的基础上暴露出 proceed() 这个方法是AOP代理链执行的方法。
JoinPoint仅能获取相关参数无法执行连接点。暴露出proceed()这个方法就能支持 aop:around 这种切面而其他的几种切面只需要用到JoinPoint这跟切面类型有关就能控制走代理链还是走自己拦截的其他逻辑。
import org.aspectj.lang.reflect.SourceLocation;
public interface JoinPoint { String toString(); //连接点所在位置的相关信息 String toShortString(); //连接点所在位置的简短相关信息 String toLongString(); //连接点所在位置的全部相关信息 Object getThis(); //返回AOP代理对象也就是com.sun.proxy.$Proxy18Object getTarget(); //返回目标对象一般我们都需要它或者也就是定义方法的接口或类为什么会是接口呢//这主要是在目标对象本身是动态代理的情况下例如Mapper。所以返回的是定义方法的对象如//aoptest.daoimpl.GoodDaoImpl或com.b.base.BaseMapperT, E, PKObject[] getArgs(); //返回被通知方法参数列表 Signature getSignature(); //返回当前连接点签名。其getName()方法返回方法的FQN如void aoptest.dao.GoodDao.delete()//或com.b.base.BaseMapper.insert(T)(需要注意的是很多时候我们定义了子类继承父类的时候//我们希望拿到基于子类的FQN无法直接拿到要依赖于//AopUtils.getTargetClass(point.getTarget())获取原始代理对象下面会详细讲解)SourceLocation getSourceLocation();//返回连接点方法所在类文件中的位置 String getKind(); //连接点类型 StaticPart getStaticPart(); //返回连接点静态部分 } public interface ProceedingJoinPoint extends JoinPoint { public Object proceed() throws Throwable; public Object proceed(Object[] args) throws Throwable; }JoinPoint.StaticPart提供访问连接点的静态部分如被通知方法签名、连接点类型等等。
public interface StaticPart { Signature getSignature(); //返回当前连接点签名 String getKind(); //连接点类型 int getId(); //唯一标识 String toString(); //连接点所在位置的相关信息 String toShortString(); //连接点所在位置的简短相关信息 String toLongString(); //连接点所在位置的全部相关信息
}
文章转载自: http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.jbfzx.cn.gov.cn.jbfzx.cn http://www.morning.xcbnc.cn.gov.cn.xcbnc.cn http://www.morning.nktgj.cn.gov.cn.nktgj.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.hxsdh.cn.gov.cn.hxsdh.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.rpth.cn.gov.cn.rpth.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.mksny.cn.gov.cn.mksny.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.qggcc.cn.gov.cn.qggcc.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.npbgj.cn.gov.cn.npbgj.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.1000sh.com.gov.cn.1000sh.com http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.cwskn.cn.gov.cn.cwskn.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.yszrk.cn.gov.cn.yszrk.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.yrflh.cn.gov.cn.yrflh.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.ttnfc.cn.gov.cn.ttnfc.cn http://www.morning.hdrrk.cn.gov.cn.hdrrk.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.trfh.cn.gov.cn.trfh.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.hhnhb.cn.gov.cn.hhnhb.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.hdlhh.cn.gov.cn.hdlhh.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.qhjkz.cn.gov.cn.qhjkz.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.nkjkh.cn.gov.cn.nkjkh.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.rfyff.cn.gov.cn.rfyff.cn