SpringBoot中自定义注解及切面Aspect

1. 自定义注解

package com.yuhuofei.annotation;

import java.lang.annotation.*;

/**
 * @author yuhuofei
 * @version 1.0
 * @description 登录态校验注解
 * @date 2022/9/29 14:50
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginStatusCheck {

    /**
     * 描述信息
     */
    String value() default "";
}

2. 定义切面

package com.yuhuofei.annotation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author yuhuofei
 * @version 1.0
 * @description 登录态校验切面
 * @date 2022/9/29 14:50
 */
@Component
@Slf4j
@Aspect
public class LoginStatusCheckAspect {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Pointcut("@annotation(com.yuhuofei.annotation.LoginStatusCheck)")
    public void checkLoginStatus() {
    }

    @Around("checkLoginStatus()")
    public Object handle(ProceedingJoinPoint joinPoint) throws Throwable {

        //获取所有参数
        Object[] args = joinPoint.getArgs();
        log.info("获取的参数是:{}", args);
        //获取userName属性
        Class<?> aClass = args[0].getClass();
        Field field = aClass.getDeclaredField("userName");
        String name = field.getName();
        log.info("属性是:{}", name);
        //获取userName属性的值
        PropertyDescriptor pd = new PropertyDescriptor(field.getName(), aClass);
        Method getMethod = pd.getReadMethod();
        Object invoke = getMethod.invoke(args[0]);
        log.info("userName的值是:{}", invoke);
        //校验登录态
        String methodName = joinPoint.getSignature().getName();
        UserInfoDO userInfoDo = userInfoMapper.selectOne(new QueryWrapper<UserInfoDO>()
                .eq("delete_flag", false)
                .eq("user_name", invoke.toString()));
        if (null == userInfoDo) {
            throw new Exception("登录失效,请重新登录!");
        }
        return joinPoint.proceed();
    }
}

3. 使 用

package com.yuhuofei.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import com.yuhuofei.annotation.LoginStatusCheck;

/**
 * @author yuhuofei
 * @version 1.0
 * @description
 * @date 2022/9/29 15:10
 */
@Slf4j
@RestController
@RequestMapping("/data")
public class DataController {

    @Autowired
    private DataService dataService;

    @LoginStatusCheck(value = "数据列表接口")
    @PostMapping("/data-list")
    public String queryData(){
        log.info("调用数据列表接口");
        //调用接口,处理业务逻辑
        result = dataService.hanList();
        return result;
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值