AOP 在 Helidon MP 中受支持吗?

huangapple go评论73阅读模式
英文:

Is AOP supported in Helidon MP?

问题

在Helidon MP中,类似于Spring框架,我想创建一个Pointcut,以在执行方法之前执行一些逻辑。这在Helidon MP中是否可能?

@Pointcut("execution(public * *(..))")
private void anyPublicOperation(String input) {}
英文:

Like Spring framework, I want to create a Pointcut to execute some logic before executing the method. Is it possible to do that in Helidon MP?

@Pointcut("execution(public * *(..))")
private void anyPublicOperation(String input) {}

答案1

得分: 1

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

英文:

Helidon MP, like all MicroProfile implementations, is centered around CDI, which offers, for this purpose, interceptors and decorators.

答案2

得分: 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)) {
                // 验证访问令牌
                authService.validateAccessToken(Objects.toString(method.invoke(p)));
            }
        }

        return invocationContext.proceed();
    }
}

然后使用它:

 @SoapSecure
 public boolean test(String token){}
英文:

I have already done that using Interceptor. Thanks!
Here is the example:

  • Creating a custom annotation with @InterceptorBinding
@InterceptorBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SoapSecure {
  String tokenParam() default "token";
}
  • Creating the interceptor
@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();
    }
}

Then use it:

 @SoapSecure
 public boolean test(String token){}

huangapple
  • 本文由 发表于 2023年2月8日 12:32:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定