当前位置:   article > 正文

SpringBoot:SpEL让复杂权限控制变得很简单_spel在springboot项目

spel在springboot项目

对于在Springboot中,利用自定义注解+切面来实现接口权限的控制这个大家应该都很熟悉,也有大量的博客来介绍整个的实现过程,整体来说思路如下:

  1. 自定义一个权限校验的注解,包含参数value
  2. 配置在对应的接口上
  3. 定义一个切面类,指定切点
  4. 在切入的方法体里写上权限判断的逻辑

乍一看,没毛病,学到了,学到了~,收藏起来。但是呢,等到实际用到的时候就傻眼了,为什么呢?在实际的开发中,你会发现,对于权限校验的需求场景是很多的,比如:

  1. 只要配置了任何角色,就可以访问
  2. 有某个权限就可以访问
  3. 放行所有请求
  4. 只有超级管理员角色才可以访问
  5. 只有登录后才可以访问
  6. 在指定时间段内可以访问
  7. 有某个角色的情况下才可以访问
  8. 同时具有指定的多个角色情况下才可以访问
  9. ……

傻眼了不,按照上面的实现逻辑的话怎么搞?加注解?写各种判断?这时候,其实我们就可以通过SpEL表达式来帮我们处理这个问题。

SpEL表达式

本文前面提到SpEL,那么到底SpEL是啥呢? SpEL的全称为Spring Expression Language,即Spring表达式语言。是Spring3.0提供的。他最强大的功能是可以通过运行期间执行的表达式将值装配到我们的属性或构造函数之中。如果有小伙伴之前没有接触过,不太理解这句话的含义,那么不要紧,继续往下看,通过后续的实践你就能明白他的作用了。

自定义注解

当然,万变不离其宗,自定义注解我们还是需要滴。这里呢,我们仅需要定义一个value属性用于接收表达式即可。

  1. @Target({ElementType.METHOD, ElementType.TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. public @interface PreAuth {
  4. /**
  5. *
  6. *
  7. * permissionAll()-----只要配置了角色就可以访问
  8. * hasPermission("MENU.QUERY")-----有MENU.QUERY操作权限的角色可以访问
  9. * permitAll()-----放行所有请求
  10. * denyAll()-----只有超级管理员角色才可访问
  11. * hasAuth()-----只有登录后才可访问
  12. * hasTimeAuth(1,,10)-----只有在1-10点间访问
  13. * hasRole(‘管理员’)-----具有管理员角色的人才能访问
  14. * hasAllRole(‘管理员’,'总工程师')-----同时具有管理员、总工程师角色的人才能访问
  15. *
  16. * Spring el
  17. * 文档地址:https://docs.spring.io/spring/docs/5.1.6.RELEASE/spring-framework-reference/core.html#expressions
  18. */
  19. String value();
  20. }

定义切面

注解定义好了,我们就需要定义切面了。这里要考虑一个点。我们希望的是如果方法上有注解,则对方法进行限制,若方法上无注解,单是类上有注解,那么类上的权限注解对该类下所有的接口生效。因此,我们切点的话要用@within注解。代码如下:

  1. @Around(
  2. "@annotation(PreAuth注解路径) || " +
  3. "@within(PreAuth注解路径)"
  4. )
  5. public Object preAuth(ProceedingJoinPoint point) throws Throwable {
  6. if (handleAuth(point)) {
  7. return point.proceed();
  8. }
  9. throw new SecureException(ResultCode.REQ_REJECT);
  10. }
  11. private boolean handleAuth(ProceedingJoinPoint point) {
  12. //TODO 逻辑判断,返回true or false
  13. }

权限校验

关键点来了。这里我们要引入SpEL。

首先,引入SpEL:

private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser();

然后,从注解上获取我们需要的表达式。

  1. MethodSignature ms = point.getSignature() instanceof MethodSignature? (MethodSignature) point.getSignature():null;
  2. Method method = ms.getMethod();
  3. // 读取权限注解,优先方法上,没有则读取类
  4. PreAuth preAuth = ClassUtil.getAnnotation(method, PreAuth.class);
  5. // 判断表达式
  6. String condition = preAuth.value();
  7. if (StringUtil.isNotBlank(condition)) {
  8. //TODU 表达式解析
  9. }

 表达式解析

  1. private boolean handleAuth(ProceedingJoinPoint point) {
  2. MethodSignature ms = point.getSignature() instanceof MethodSignature? (MethodSignature) point.getSignature():null;
  3. Method method = ms.getMethod();
  4. // 读取权限注解,优先方法上,没有则读取类
  5. PreAuth preAuth = ClassUtil.getAnnotation(method, PreAuth.class);
  6. // 判断表达式
  7. String condition = preAuth.value();
  8. if (StringUtil.isNotBlank(condition)) {
  9. Expression expression = EXPRESSION_PARSER.parseExpression(condition);
  10. // 方法参数值
  11. Object[] args = point.getArgs();
  12. StandardEvaluationContext context = getEvaluationContext(method, args);
  13. //获取解析计算的结果
  14. return expression.getValue(context, Boolean.class);
  15. }
  16. return false;
  17. }
  18. /**
  19. * 获取方法上的参数
  20. *
  21. * @param method 方法
  22. * @param args 变量
  23. * @return {SimpleEvaluationContext}
  24. */
  25. private StandardEvaluationContext getEvaluationContext(Method method, Object[] args) {
  26. // 初始化Sp el表达式上下文,并设置 AuthFun
  27. StandardEvaluationContext context = new StandardEvaluationContext(new AuthFun());
  28. // 设置表达式支持spring bean
  29. context.setBeanResolver(new BeanFactoryResolver(applicationContext));
  30. for (int i = 0; i < args.length; i++) {
  31. // 读取方法参数
  32. MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
  33. // 设置方法 参数名和值 为spel变量
  34. context.setVariable(methodParam.getParameterName(), args[i]);
  35. }
  36. return context;
  37. }

自定义解析方法

看完上面的解析处理是不是很蒙蔽,只看到了获取表达式,获取参数,设置参数,然后expression.getValue就完事了。

有的同学会问,你权限校验的逻辑呢?别急,关键点在这:StandardEvaluationContext context = new StandardEvaluationContext(new AuthFun());在上文代码中找到了吧。

这个AuthFun就是我们进行权限校验的对象。所以呢,我们还得在定义一下这个对象。进行具体的权限校验逻辑处理,这里定的每一个方法都可以作为表达式在权限注解中使用。代码如下:

  1. public class AuthFun {
  2. /**
  3. * 判断角色是否具有接口权限
  4. *
  5. * @return {boolean}
  6. */
  7. public boolean permissionAll() {
  8. //TODO
  9. }
  10. /**
  11. * 判断角色是否具有接口权限
  12. *
  13. * @param permission 权限编号,对应菜单的MENU_CODE
  14. * @return {boolean}
  15. */
  16. public boolean hasPermission(String permission) {
  17. //TODO
  18. }
  19. /**
  20. * 放行所有请求
  21. *
  22. * @return {boolean}
  23. */
  24. public boolean permitAll() {
  25. return true;
  26. }
  27. /**
  28. * 只有超管角色才可访问
  29. *
  30. * @return {boolean}
  31. */
  32. public boolean denyAll() {
  33. return hasRole(RoleConstant.ADMIN);
  34. }
  35. /**
  36. * 是否已授权
  37. *
  38. * @return {boolean}
  39. */
  40. public boolean hasAuth() {
  41. if(Func.isEmpty(AuthUtil.getUser())){
  42. // TODO 返回异常提醒
  43. }else{
  44. return true;
  45. }
  46. }
  47. /**
  48. * 是否有时间授权
  49. *
  50. * @param start 开始时间
  51. * @param end 结束时间
  52. * @return {boolean}
  53. */
  54. public boolean hasTimeAuth(Integer start, Integer end) {
  55. Integer hour = DateUtil.hour();
  56. return hour >= start && hour <= end;
  57. }
  58. /**
  59. * 判断是否有该角色权限
  60. *
  61. * @param role 单角色
  62. * @return {boolean}
  63. */
  64. public boolean hasRole(String role) {
  65. return hasAnyRole(role);
  66. }
  67. /**
  68. * 判断是否具有所有角色权限
  69. *
  70. * @param role 角色集合
  71. * @return {boolean}
  72. */
  73. public boolean hasAllRole(String... role) {
  74. for (String r : role) {
  75. if (!hasRole(r)) {
  76. return false;
  77. }
  78. }
  79. return true;
  80. }
  81. /**
  82. * 判断是否有该角色权限
  83. *
  84. * @param role 角色集合
  85. * @return {boolean}
  86. */
  87. public boolean hasAnyRole(String... role) {
  88. //获取当前登录用户
  89. BladeUser user = AuthUtil.getUser();
  90. if (user == null) {
  91. return false;
  92. }
  93. String userRole = user.getRoleName();
  94. if (StringUtil.isBlank(userRole)) {
  95. return false;
  96. }
  97. String[] roles = Func.toStrArray(userRole);
  98. for (String r : role) {
  99. if (CollectionUtil.contains(roles, r)) {
  100. return true;
  101. }
  102. }
  103. return false;
  104. }
  105. }

实际使用

在使用的时候,我们只需要在类上或者接口上,加上@PreAuth的直接,value值写的时候要注意一下,value应该是我们在AuthFun类中定义的方法和参数,如我们定义了解析方法hasAllRole(String... role),那么在注解中,我们就可以这样写@PreAuth("hasAllRole('角色1','角色2')"),需要注意的是,参数要用单引号包括。

  1. @PreAuth("hasPermission('LM_QUERY,LM_QUERY_ALL')")
  2. public T 接口名称....

原理

根据上面的实际使用,可以看到。SpEL表达式解析将我们注解中的"hasAllRole('角色1','角色2')"这样的字符串,给动态解析为了hasAllRole(参数1,参数1),并调用我们注册类中同名的方法。

总结

通过SpEL的使用,让我们的权限配置校验更加灵活。当出现新的场景时,我们仅需要在自定的表达式解析类中增加对应场景的解析方法即可。相对于之前的实现方式,这不得不说是更好的一个选择。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/774475
推荐阅读
相关标签
  

闽ICP备14008679号