【发布时间】:2018-07-31 17:44:09
【问题描述】:
我有一个控制器:
@Authorised(id = "{personId}")
@RequestMapping(value = {"{personId}"}, method = GET)
public void test(@PathVariable PersonId personId) {
System.out.println(personId); //gets personId
}
注释:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Authorised {
String id() default "";
}
切入点:
@Pointcut("@annotation(Authorised)")
private void AuthorisedMethod() {}
必须获取 {personId} 值的方法不是字符串“{personId}”:
@Before("AuthorisedMethod()")
public void checkIfIsCurrentlyAuthenticated(JoinPoint joinPoint) throws NoSuchMethodException {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getMethod().getName();
Class<?>[] parameterTypes = signature.getMethod().getParameterTypes();
Parameter[] parameters = signature.getMethod().getParameters();
Authorised annotations = joinPoint.getTarget().getClass().getMethod(methodName, parameterTypes).getAnnotation(Authorised.class);
String id = annotations.id();
System.out.println(id); // prints: "{personId}"
// do the chekcing
throw new UnauthenticatedUserException();
}
能否实现以及如何实现?
更新:但是如果方法参数参数编号与切入点 args() 不匹配怎么办?我的意思是,如果特定方法有参数@PathVariable PersonId personId 等等,但 poincut 只需要知道 PersonId personId 怎么办?
就像@statut 说的你必须这样写 args():args(personId,..)
【问题讨论】:
标签: java spring spring-mvc aspectj spring-annotations