網站首頁 編程語言 正文
場景描述
在平時開發中,我們經常通過定義一些注解,進行輕量級開發。今天主要研究的內容是關于如何在注解上通過spel表達式
注入對象,以此調用注入對象的具體業務處理邏輯,然后在通過對表達式的解析,進而獲取該業務邏輯處理的結果,類似于Spring Security
中的@PreAuthorize
, @PreAuthorize
, @PostAuthorize
等注解,本次場景案例以模仿@PreAuthorize
注解進行分析。
具體案例
定義@SpelPreAuthorize注解,對標@PreAuthorize
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SpelPreAuthorize {
String value() default "";
}
定義具體業務邏輯處理類
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component("ps")
public class PermissionService {
public boolean hasPermission(String permission) {
List allPermissions = Arrays.asList("user:save", "user:delete", "user:edit");
return allPermissions.contains(permission);
}
}
定義切面
- 通過
@Before
注解定義前置切面 - 通過注入
spelExpressionParser
解析器,用于解析spel表達式 - 實例化
EvaluationContext
對象(默認實現tandardEvaluationContext
),解析表達式,注入上下文信息,執行具體業務
import com.czf.ebao.data.spel.annoation.SpelPreAuthorize;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class SpelPreAuthorizeAspect {
/**
* 注入spring bean 工廠
*/
@Autowired
private DefaultListableBeanFactory defaultListableBeanFactory;
@Before("@annotation(spelPreAuthorize)")
public void perAuthorize(JoinPoint point, SpelPreAuthorize spelPreAuthorize) {
String permission = spelPreAuthorize.value();
// 實例化spel表達式解析器
SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
// 解析表達式內容
Expression expression = spelExpressionParser.parseExpression(permission);
// 聲明StandardEvaluationContext對象,用于設置上下文對象。
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(defaultListableBeanFactory));
Boolean result = expression.getValue(context, Boolean.class);
if (!result) {
throw new RuntimeException("該用戶無訪問權限");
}
}
}
定義測試類
import com.czf.ebao.data.spel.annoation.SpelPreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/spel")
public class SpelController {
@GetMapping("/hello")
@SpelPreAuthorize("@pms.hasPermission('user:hello')")
public String sayHello() {
return "hello";
}
}
補充
- 通配符匹配
// import org.springframework.util.PatternMatchUtils
List allPermissions = Arrays.asList("user:save", "user:delete", "user:edit");
return allPermissions.stream().anyMatch(item -> PatternMatchUtils.simpleMatch(permission, item));
原文鏈接:https://blog.csdn.net/c17315377559/article/details/121670739
相關推薦
- 2022-11-03 C++高精度算法的使用場景詳解_C 語言
- 2023-01-17 CMakeList中自動編譯protobuf文件過程_C 語言
- 2022-03-15 使用swagger-bootstrap-ui ,訪問的時候 404
- 2022-11-14 C#中對集合排序的三種方式_C#教程
- 2022-04-03 Docker?部署RocketMQ的詳細操作_docker
- 2023-03-27 Python中反轉二維數組的行和列問題_python
- 2022-08-12 Docker與K8s關系介紹不會Docker也可以使用K8s_云和虛擬化
- 2022-12-30 Android入門之在子線程中調用Handler詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支