通过AOP+自定义注解实现日志记录+接口权限校验

创作背景

今天系统的学习了aop,写一个demo来练手强化和熟悉一下aop。

实现原理

通过自定义注解+aop切面编程实现。

代码实现


@Target({ElementType.METHOD}) //指定只能在方法上写
@Retention(RetentionPolicy.RUNTIME) //设定运行范围
public @interface CheckAuth {
    String value();
}
@RestController
@RequestMapping("/book")
public class BookController {
    
    @CheckAuth(value = "admin") //添加自定义注解并定义需要管理员权限
    @GetMapping("/selectAllBooks")
    public String select() {
        return "查询成功";
    }
}
@Data
public class User {
    private int id;
    private String name;
    private String auth;
}
@Service
public class UserServiceImpl implements UserService {
    @Override
    public User getLoginUser() {
        User user = new User();
        user.setId(1);
        user.setAuth("admin");
        user.setName("xiaotao");
        return user;
    }
}
@Aspect
@Component
@Slf4j
public class BookAdvice {
    @Resource
    private UserService userService;
    //环绕通知
    @Around("@annotation(checkAuth)")
    public Object around(ProceedingJoinPoint pjp, CheckAuth checkAuth) throws Throwable {
        log.info("===============> log begin <=================");
        //1.获取request对象
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        //2.获取请求的详情详细
        String requestURI = request.getRequestURI();
        String method = request.getMethod();

        log.info("requestTime: {}", LocalDateTime.now());
        log.info("requiredAuth: {}", checkAuth.value());
        log.info("requestURI: {}", requestURI);
        log.info("method: {}", method);
        //打印所有请求头
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {//通过Enumeration类中的hasMoreElements()判断是否还有参数名
            String parameterName = (String) headerNames.nextElement(); //获取当前参数名
            //再通过request.getParameter("")的方法来获取对应参数名的值
            log.info("Request Header: {}: {}", parameterName, request.getHeader(parameterName));
        }
        //打印请请求体
        Enumeration<String> e = request.getParameterNames();
        while (e.hasMoreElements()) {
            String parameterName = (String) e.nextElement(); 
            
            log.info("Request parameters:  {}: {}", parameterName, request.getParameter(parameterName));
        }

        //3.判断权限是否符合
        User loginUser = userService.getLoginUser();
        if (loginUser != null) {
            String auth = checkAuth.value();
            if (loginUser.getAuth().equals(auth)) {
                log.info("权限校验完成,权限符合");
                log.info("===============> log end <=================");
                return pjp.proceed();
            }
        }
        log.error("权限不足");
        log.info("===============> log end <=================");
        //4.不符合条件的均为权限不足
        throw  new RuntimeException("权限不足");
    }
    

}

最终效果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喂不饱的小陶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值