SpringBoot使用AOP+注解实现简单的权限验证的方法
作者:wqh8522 时间:2022-07-29 00:59:09
SpringAOP的介绍:传送门
demo介绍
主要通过自定义注解,使用SpringAOP的环绕通知拦截请求,判断该方法是否有自定义注解,然后判断该用户是否有该权限。这里做的比较简单,只有两个权限:一个普通用户、一个管理员。
项目搭建
这里是基于SpringBoot的,对于SpringBoot项目的搭建就不说了。在项目中添加AOP的依赖:<!--more--->
<!--AOP包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
自定义注解及解析
在方法上添加该注解,说明该方法需要管理员权限才能访问。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Permission {
String authorities() default "ADMIN";
}
解析类:通过AOP的环绕通知获取方法上的注解,判断是否有Permission注解,返回注解的值。
public class AnnotationParse {
/***
* 解析权限注解
* @return 返回注解的authorities值
* @throws Exception
*/
public static String privilegeParse(Method method) throws Exception {
//获取该方法
if(method.isAnnotationPresent(Permission.class)){
Permission annotation = method.getAnnotation(Permission.class);
return annotation.authorities();
}
return null;
}
}
SpringAOP环绕通知
@Aspect
@Component
public class ControllerAspect {
private final static Logger logger = LoggerFactory.getLogger(ControllerAspect.class);
@Autowired
private UserService userService;
/**
* 定义切点
*/
@Pointcut("execution(public * com.wqh.blog.controller.*.*(..))")
public void privilege(){}
/**
* 权限环绕通知
* @param joinPoint
* @throws Throwable
*/
@ResponseBody
@Around("privilege()")
public Object isAccessMethod(ProceedingJoinPoint joinPoint) throws Throwable {
//获取访问目标方法
MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
//得到方法的访问权限
final String methodAccess = AnnotationParse.privilegeParse(targetMethod);
//如果该方法上没有权限注解,直接调用目标方法
if(StringUtils.isBlank(methodAccess)){
return joinPoint.proceed();
}else {
//获取当前用户的权限,这里是自定义的发那个发
User currentUser = userService.getCurrentUser();
logger.info("访问用户,{}",currentUser.toString());
if(currentUser == null){
throw new LoginException(ResultEnum.LOGIN_ERROR);
}
if(methodAccess.equals(currentUser.getRole().toString())){
return joinPoint.proceed();
}else {
throw new BusinessException(ResultEnum.ROLE_ERROR);
}
}
}
}
使用
只需要在需要验证的方法上添加自定义注解: @Permission既可
来源:https://segmentfault.com/a/1190000012845239
标签:SpringBoot,aop,注解,权限验证
0
投稿
猜你喜欢
C# 16进制与字符串、字节数组之间的转换
2021-07-13 08:08:10
详解从零开始---用C#制作扫雷游戏
2022-06-14 13:33:12
java8新特性将List中按指定属性排序过滤重复数据的方法
2023-06-16 17:57:42
java数据类型和运算符的深入讲解
2021-09-14 07:06:08
C# 读写自定义的Config文件的实现方法
2022-09-08 23:22:35
C#自定义函数NetxtString生成随机字符串
2022-06-18 20:43:39
SpringBoot分离打Jar包的两种配置方式
2023-01-30 09:06:59
mybatis-plus排除非表中字段的操作
2022-04-22 03:48:41
Hadoop组件简介
2023-08-20 14:07:00
Android自定义 WebView浏览器
2023-07-21 00:31:34
windows如何使用bat脚本后台启动/停止和重启jar包服务
2022-02-27 12:28:36
idea2020.1无法自动加载maven依赖的jar包问题及解决方法
2021-06-13 17:40:36
SpringBoot SSO轻松实现(附demo)
2022-04-05 02:24:33
arthas jprofiler做复杂链路的调用分析
2022-01-15 12:01:25
JAVA线程池原理实例详解
2021-10-01 17:59:01
Maven项目修改JDK版本全过程
2021-07-19 12:13:29
C#端口扫描器的编写方法
2023-12-17 17:47:24
解决Spring Batch框架job任务只跑一次的问题
2023-01-07 00:13:53
解决java文件流处理异常 mark/reset not supported问题
2022-10-05 14:28:08
Android Volley图片加载功能详解
2023-11-19 08:01:41