gpt4 book ai didi

java - 使用 Guice AOP 在 Jersey 中进行方法拦截

转载 作者:搜寻专家 更新时间:2023-10-31 20:04:48 24 4
gpt4 key购买 nike

是否可以使用 Guice AOP 拦截 Jersey 资源上的注释方法?

我有一个成功配置的 Guice 集成,在依赖注入(inject)方面与 Jersey 一起工作,没有任何问题,但是我配置的拦截器根本没有拦截我的注释方法。

web.xml

<listener>
<listener-class>my.package.GuiceConfig</listener-class>
</listener>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

GuiceConfig 配置模块

public class GuiceConfig extends GuiceServletContextListener {

@Override
protected Injector getInjector() {
return Guice.createInjector(new JerseyServletModule() {

@Override
protected void configureServlets() {

bindInterceptor(Matchers.any(),
Matchers.annotatedWith(RequiredAuthority.class),
new AuthorisationInterceptor());

Map<String, String> params = new HashMap<String, String>();
params.put(JSP_TEMPLATES_BASE_PATH, "/WEB-INF/jsp");
params.put(FEATURE_FILTER_FORWARD_ON_404, "true");
params.put(PROPERTY_PACKAGES, "my.service.package");

filter("/*").through(GuiceContainer.class, params);
}
});
}
}

RequiredAuthority 注释

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiredAuthority {
String value();
}

AuthorisationInterceptor 方面

public class AuthorisationInterceptor implements MethodInterceptor {

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

// Allow invocation to process or throw an appropriate exception
}
}

TempResource JAX-RS 资源类

@Path("/temp")
public class TempResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
@RequiredAuthority("PERMISSION")
public String getTemp() {

// Return resource normally
}
}

最佳答案

看起来 configureServlets() 没有调用:

bind(TempResource.class);

关于java - 使用 Guice AOP 在 Jersey 中进行方法拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10982583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com