像Spring框架一样,我想在执行方法之前创建一个Pointcut来执行一些逻辑。在 Helidon MP 中可以这样做吗?
@Pointcut("execution(public * *(..))")
private void anyPublicOperation(String input) {}
Helidon MP,像所有 MicroProfile 实现一样,以 CDI 为中心,它为此提供了 拦截器 和 装饰器。
我已经用
Interceptor
完成了。谢谢!
这是例子:
@InterceptorBinding
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SoapSecure {
String tokenParam() default "token";
}
@Priority(1)
@Interceptor
@SoapSecure
@Slf4j
public class SoapAuthenticationInterceptor {
@Inject
private AuthService authService;
@AroundInvoke
public Object validateToken(InvocationContext invocationContext) throws Exception {
Method method = invocationContext.getMethod();
log.info("Validate the token from SOAP APIs: " + method.getName());
String tokenParam = method
.getAnnotation(SoapSecure.class)
.tokenParam();
Parameter[] params = method.getParameters();
String accessToken = null;
for (Parameter p : params) {
if (p.getName().equals(tokenParam)) {
// validate the access token
authService.validateAccessToken(Objects.toString(method.invoke(p)));
}
}
return invocationContext.proceed();
}
}
然后使用它:
@SoapSecure
public boolean test(String token){}