如何设置aopMethodInterceptor
使用泽西岛资源?
这是我尝试过的,如下this https://hk2.java.net/2.2.0/aop-example.html文档:
第 1 步 - 拦截服务
public class MyInterceptionService implements InterceptionService
{
private final Provider<AuthFilter> authFilterProvider;
@Inject
public HK2MethodInterceptionService(Provider<AuthFilter> authFilterProvider)
{
this.authFilterProvider = authFilterProvider;
}
/**
* Match any class.
*/
@Override
public Filter getDescriptorFilter()
{
return BuilderHelper.allFilter();
}
/**
* Intercept all Jersey resource methods for security.
*/
@Override
@Nullable
public List<MethodInterceptor> getMethodInterceptors(final Method method)
{
// don't intercept methods with PermitAll
if (method.isAnnotationPresent(PermitAll.class))
{
return null;
}
return Collections.singletonList(new MethodInterceptor()
{
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable
{
if (!authFilterProvider.get().isAllowed(method))
{
throw new ForbiddenException();
}
return methodInvocation.proceed();
}
});
}
/**
* No constructor interception.
*/
@Override
@Nullable
public List<ConstructorInterceptor> getConstructorInterceptors(Constructor<?> constructor)
{
return null;
}
}
第 2 步 - 注册服务
public class MyResourceConfig extends ResourceConfig
{
public MyResourceConfig()
{
packages("package.with.my.resources");
// UPDATE: answer is remove this line
register(MyInterceptionService.class);
register(new AbstractBinder()
{
@Override
protected void configure()
{
bind(AuthFilter.class).to(AuthFilter.class).in(Singleton.class);
// UPDATE: answer is add the following line
// bind(MyInterceptionService.class).to(InterceptionService.class).in(Singleton.class);
}
});
}
}
然而,这似乎不起作用,因为我的资源方法都没有被拦截。这可能是因为我使用@ManagedAsync
用我所有的资源?有任何想法吗?
另外,请不要建议ContainerRequestFilter
. See 这个问题 https://stackoverflow.com/q/20556200/1415732为什么我不能使用它来处理安全性。
我认为您可能不想调用 register(MyInterceptionService.class) 而是添加到您的 configure() 语句中:
bind(MyInterceptionService.class).to(InterceptionService.class).in(Singleton.class)
我不确定它是否有效,因为我自己没有尝试过,所以你的结果可能会有所不同,哈哈
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)