英文:
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){}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论