【发布时间】:2018-06-26 23:01:33
【问题描述】:
我们使用的是 Resteasy 2,但我们正在升级到 Resteasy 3,HttpServletRequest 注入始终是 null。
我们修改后的安全拦截器/过滤器如下所示:
@Provider
@ServerInterceptor
@Precedence("SECURITY")
public class SecurityInterceptor implements ContainerRequestFilter, ContainerResponseFilter {
@Context
private HttpServletRequest servletRequest;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// Need access to "servletRequest" but it is always null
if (!isTokenValid(pmContext, method)) {
requestContext.abortWith(ACCESS_DENIED);
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
// post processing
}
}
应用程序类看起来像:
@ApplicationPath("/")
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public RestApplication() {
// Interceptors
this.singletons.add(new SecurityInterceptor());
// Services
this.singletons.add(new MyService());
}
public Set<Class<?>> getClasses() {
return this.empty;
}
public Set<Object> getSingletons() {
return this.singletons;
}
}
示例 API:
@Path("/test")
public class MyService extends BaseService {
@Context HttpServletRequest servletRequest;
@GET
@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public Response hello() {
// Need access to HttpServletRequest but it's null
return Response.ok("hello").build();
}
}
但是,查看this 和this 的帖子,我没有看到HttpServletRequest 注入提供者。
这让我相信我可能需要一个额外的插件。这是安装的:
jose-jwt
resteasy-atom-provider
resteasy-cdi
resteasy-crypto
resteasy-jackson2-provider
resteasy-jackson-provider
resteasy-jaxb-provider
resteasy-jaxrs
resteasy-jettison-provider
resteasy-jsapi
resteasy-json-p-provider
resteasy-multipart-provider
resteasy-spring
resteasy-validator-provider-11
resteasy-yaml-provider
有什么想法吗?
【问题讨论】:
-
尝试将它们添加到类中(而不是单例)并让 resteasy 创建它们。
-
@peeskillet 感谢您的回复。你的建议奏效了。
标签: jboss jax-rs wildfly resteasy undertow