我是靠谱客的博主 英俊黑猫,这篇文章主要介绍利用AOP实现简单的用户认证,现在分享给大家,希望可以做个参考。

前言:

最近博主刚好看了一些关于aop的知识,想到现在公司中的一些项目所有接口上都会手动认证用户,就想到好像可以直接写个切面就行了。

切面类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/** * @author :Curtain * @date :Created in 2020/12/22 10:25 * @description:切面类 * 创建一个AOP切面类,只要在类上加个 @Aspect 注解即可。 * @Aspect 注解用来描述一个切面类,定义切面类的时候需要打上这个注解。 * @Component 注解将该类交给 Spring来管理 * BaseController是公司自己写的一个类,我们会用到它里面获取用户信息的方法所以要继承 */ @Aspect @Component public class YhrzAdvice extends BaseController { //定义一个切点,表示被@ApiOperation注解的地方,因为我们用到了swagger,所以所有接口上面都有 //这个注解,或者也可以用excution路径的方式标记到Controller层的接口 @Pointcut("@annotation(io.swagger.annotation.ApiOperation)") private void yhrz(){} @Around("yhrz()") public Object yhrzCheck(ProceedingJoinPoint joinPoint) throws Throwable { //用户认证逻辑,根据自己的情况编写 ShiroUser user = getUser(); if (user == null || user.getId() == null) { return (ResultGenerator.genFailResult("用户未找到")); } return joinPoint.proceed();//让当前程序正常运行 } }

 

最后

以上就是英俊黑猫最近收集整理的关于利用AOP实现简单的用户认证的全部内容,更多相关利用AOP实现简单内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部