目录:
1、简单介绍SpEL用法
2、写注解
3、写AOP
4、通过SPEL获取接口入参5、通过SPEL修改接口入参
6、使用7、场景
正文:
1、简单介绍SpEL
Spring Expression Language(缩写为SpEL)是一种强大的表达式语言。在Spring产品组合中,它是表达式计算的基础。它支持在运行时查询和操作对象图,它可以与基于XML和基于注解的Spring配置还有bean定义一起使用。由于它能够在运行时动态分配值,因此可以为我们节省大量Java代码。
SpEL官方文档
2、写注解
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CleanerTimeCheck {
String cleanerCode() default "";
String orderTime() ;
}
3、写AOP
4、通过SpEL获取接口参数
@Aspect
@Component
@Order(2)
@Log4j2
public class CleanerTimeCheckAop {
@Autowired
private IOrOrderService orOrderService;
private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Pointcut("@annotation(cleanerTimeCheck)")
public void initLogAnno(CleanerTimeCheck cleanerTimeCheck) {
}
@Before("initLogAnno(cleanerTimeCheck)")
public void doBefore(JoinPoint point, CleanerTimeCheck cleanerTimeCheck) {
//通过SpEL获取接口参数对象属性值
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext(point.getArgs());
standardEvaluationContext = setContextVariables(standardEvaluationContext, point);
String cleanerCode = getElValue(cleanerTimeCheck.cleanerCode(),standardEvaluationContext);
String orderTime = getElValue(cleanerTimeCheck.orderTime(),standardEvaluationContext);
业务代码,省略...
}
private StandardEvaluationContext setContextVariables(StandardEvaluationContext standardEvaluationContext,
JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer
= new LocalVariableTableParameterNameDiscoverer();
String[] parametersName = parameterNameDiscoverer.getParameterNames(targetMethod);
if (args == null || args.length <= 0) {
return standardEvaluationContext;
}
for (int i = 0; i < args.length; i++) {
standardEvaluationContext.setVariable(parametersName[i], args[i]);
}
return standardEvaluationContext;
}
/**
* 通过key SEL表达式获取值
*
*/
private String getElValue(String key, StandardEvaluationContext context) {
if (StringUtils.isBlank(key)) {
return "";
}
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(key);
String value = exp.getValue(context, String.class);
return value;
}
}
5、通过SpEL修改接口参数
@Aspect
@Component
@Order(2)
@Log4j2
public class CleanerTimeCheckAop {
@Autowired
private IOrOrderService orOrderService;
private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Pointcut("@annotation(cleanerTimeCheck)")
public void initLogAnno(CleanerTimeCheck cleanerTimeCheck) {
}
@Before("initLogAnno(cleanerTimeCheck)")
public void doBefore(JoinPoint point, CleanerTimeCheck cleanerTimeCheck) {
//通过SpEL获取接口参数对象属性值
StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext(point.getArgs());
standardEvaluationContext = setContextVariables(standardEvaluationContext, point);
业务代码,省略...
setElValue(cleanerTimeCheck.cleanerCode(),"新业务值",standardEvaluationContext);
}
private StandardEvaluationContext setContextVariables(StandardEvaluationContext standardEvaluationContext,
JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method targetMethod = methodSignature.getMethod();
LocalVariableTableParameterNameDiscoverer parameterNameDiscoverer
= new LocalVariableTableParameterNameDiscoverer();
String[] parametersName = parameterNameDiscoverer.getParameterNames(targetMethod);
if (args == null || args.length <= 0) {
return standardEvaluationContext;
}
for (int i = 0; i < args.length; i++) {
standardEvaluationContext.setVariable(parametersName[i], args[i]);
}
return standardEvaluationContext;
}
/**
* 通过key SEL表达式获取值
*
*/
private void setElValue(String key,String newValue, StandardEvaluationContext context) {
if (StringUtils.isBlank(key)) {
return "";
}
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(key);
exp.setValue(context, newValue);
}
}
6、使用
@CleanerTimeCheck(cleanerCode = "#payPrepayVO.cleanerCode",orderTime = "#payPrepayVO.serviceTime")
public Result<PayPrepayDTO> prepay(@RequestBody @Validated OrderPrepayVO payPrepayVO, @LoginUser String customCode) throws InterruptedException {
业务代码,省略...
}
7、场景
xx后台管理系统有站点(线下门店)查询条件,防止当前用户恶意选中了非自己管控站点范围的站点,导致其它站点的借阅信息被查询出来,这种无法使用权限框架进行过滤,权限框架无法对数据进行过滤,只能对接口或者菜单权限进行限定,使用SPEL+AOP的机制可以在接口请求参数还没映射到用于接收请求体中的参数的request类的时候,对数据进行权限过滤(配合站点系统的用户权限接口,请求体为用户的userCode,响应参数是该用户对应角色管理的站点列表),这样即可实现这个功能。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/105969.html