自定义注解配合切面做日志记录

自定义注解配合切面可以很好的做到日志记录,权限控制等信息.

一.具体自定义注解代码如下:

package com.example.common;

import java.lang.annotation.*;


@Target(ElementType.METHOD)        
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Authorization {

    String message() default "";

    String bussCode() default "";
}

元注解说明:

1. @Target

标识注解使用的范围

2.@Retention

注解用于指明修饰的注解的生存周期,保留到哪个阶段.

3.@Documented 注解表明这个注释是由 javadoc记录的,如果一个类型声明被注释了文档化,它的注释成为公共API的一部分。

二.切面使用代码如下:

package com.example.common;


import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
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.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
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.reflect.Method;
import java.util.HashMap;
import java.util.Map;


@Component
@Aspect
@Slf4j
public class AuthorizationAspect {

    @Pointcut("@annotation(com.example.common.Authorization)")
    public void authPointcut() {

    }

    @Around("authPointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        Authorization authCode = method.getAnnotation(Authorization.class);
        if (authCode != null) {
            //根据方法上是否有改注解进行业务逻辑处理
            
        }

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String requestMode = request.getMethod();
        //请求方式-->类名.方法名
        String classMethodName = requestMode + "-->" + signature.getDeclaringTypeName() + "." + signature.getName();
        //客户端ip
        String ipAddress = ServletUtil.getClientIP(request, null);
        //请求路径
        String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");
        //请求参数
        Map<String, Object> fieldsName = getFieldsName(point);
        
        log.info("classMethodName:{}",classMethodName);
        log.info("ipAddress:{}",ipAddress);
        log.info("requestUrl:{}",requestUrl);
        log.info("requestParam:{}",fieldsName);
        //这里可以保存日志
        return point.proceed();
    }


    

    /**
     * 获取参数列表
     *
     * @param joinPoint
     */
    private Map<String, Object> getFieldsName(ProceedingJoinPoint joinPoint) {
        // 参数值
        Object[] args = joinPoint.getArgs();
        ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String[] parameterNames = pnd.getParameterNames(method);
        Map<String, Object> paramMap = new HashMap<>(32);
        for (int i = 0; i < parameterNames.length; i++) {
            paramMap.put(parameterNames[i], args[i]);
        }
        return paramMap;
    }


}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值