Helidon MP 支持 AOP 吗?

问题描述 投票:0回答:2

像Spring框架一样,我想在执行方法之前创建一个Pointcut来执行一些逻辑。在 Helidon MP 中可以这样做吗?

@Pointcut("execution(public * *(..))")
private void anyPublicOperation(String input) {}
aop spring-aop helidon
2个回答
2
投票

Helidon MP,像所有 MicroProfile 实现一样,以 CDI 为中心,它为此提供了 拦截器装饰器


0
投票

我已经用

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){}
© www.soinside.com 2019 - 2024. All rights reserved.