【发布时间】:2015-10-08 20:03:58
【问题描述】:
我正在尝试使用类似于org.springframework.cache.annotation.Cacheable 的东西:
自定义注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckEntity {
String message() default "Check entity msg";
String key() default "";
}
方面:
@Component
@Aspect
public class CheckEntityAspect {
@Before("execution(* *.*(..)) && @annotation(checkEntity)")
public void checkEntity(JoinPoint joinPoint, CheckEntitty checkEntity) {
System.out.println("running entity check: " + joinPoint.getSignature().getName());
}
}
服务:
@Service
@Transactional
public class EntityServiceImpl implements EntityService {
@CheckEntity(key = "#id")
public Entity getEntity(Long id) {
return new Entity(id);
}
}
我的 IDE (IntelliJ) 没有看到 key = "#id" 用法有什么特别之处,而 Cacheable 的类似用法则以不同于纯文本的颜色显示。我提到 IDE 部分只是作为提示,以防万一它有帮助,看起来 IDE 提前知道这些注释,或者它只是实现了一些在我的示例中不存在的连接。
checkEntity.key 中的值是“#id”而不是预期的数字。
我尝试使用ExpressionParser,但可能不正确。
在 checkEntity 注释中获取参数值的唯一方法是访问参数数组,这不是我想要的,因为此注释也可以在具有多个参数的方法中使用。
有什么想法吗?
【问题讨论】:
-
没有 IDE 能够为您提供它对
@Cacheable的上下文感知支持,因为您的方面是量身定制的。我能问一下您试图为您的 Aspect 提供什么类型的功能吗?您是否尝试检查实体是否已存在? -
这是为了检查这个id(比如departmentId)是否存在于loggedIn用户可以访问的部门中,否则抛出一个accessdenied异常
-
Spring Security 或 Apache Shiro 不会提供这样的功能而不必滚动您自己的实现吗?
-
我不这么认为,这是基于用户数据的额外安全检查。您可以为呼叫定义角色级别,但我认为您不能根据被调用方法的关系(例如,使用 param deparmentId)和 loginInUser 的其他详细信息(例如,部门 ID 列表可以有)另外定义访问权限访问)
标签: spring annotations aspectj spring-el