切面打印日志,自定义注解

//首先需要在pom.xml中添加依赖
        <!-- aop依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 权限限制
 * 自定义注解  @PermissionLimit
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PermissionLimit {

	/**
	 * 初始化注解上的描述
	 */
	String value() default "";
	
	/**
	 * 登录拦截 (默认拦截)
	 */
	boolean limit() default true;

	/**
	 * 要求管理员权限
	 *
	 * @return
	 */
	boolean adminuser() default false;
}
import com.example.study.annotation.PermissionLimit;
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.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Aspect
@Component
public class LogAspect {

    /**
     * 配置切点
     */
    
    @Pointcut("@annotation(com.example.study.annotation.PermissionLimit)") //指向项目中PermissionLimit类的路径
    public void pointcut() { }

    /**
     * 配置切面
     * @param point
     * @return
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) {
        Object result = null;
        try {
            // 执行方法
            result = point.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        // 注解上的描述
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        PermissionLimit logAnnotation = method.getAnnotation(PermissionLimit.class);
        if (logAnnotation != null) {
            System.out.println(logAnnotation.value());
        }
        return result;
    }
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.study.annotation.PermissionLimit;

@RestController
public class UserController {

	//测试类
    @RequestMapping("test")
    public String test() {
        try {
            System.out.println("123");
        } catch (Exception e) {
            logger.info("邮件发送失败:", e);
        }
        return "over";
    }

    @PermissionLimit("邮件发送456")
    @RequestMapping("test1")
    public String test1() {
        try {
            System.out.println("456");
        } catch (Exception e) {
            logger.info("邮件发送失败:", e);
        }
        return "over";
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值