java怎么拦截某个对象
326
2022-10-30
Spring AOP如何在注解上使用SPEL表达式注入对象
目录在注解上使用SPEL表达式注入对象场景描述具体案例补充Spring属性注入方式之SPEL表达式
在注解上使用SPEL表达式注入对象
场景描述
在平时开发中,我们经常通过定义一些注解,进行轻量级开发。
今天主要研究的内容是关于如何在注解上通过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
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对象,用于设置上下文对象。
StandardEvaluationCohttp://ntext 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
return allPermissions.stream().anyMatch(item -> PatternMatchUtils.simpleMatch(permission, item));
Spring属性注入方式之SPEL表达式
spel表达式的好处就是,我们可以使用定义好的某个类中的方法来产生值,然后注入给所需要的对象使用
比较适用于比较复杂的bean注入
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~