Jboss Resteasy 3@Context HttpServletRequest始终为空

Jboss Resteasy 3@Context HttpServletRequest始终为空,jboss,jax-rs,wildfly,resteasy,undertow,Jboss,Jax Rs,Wildfly,Resteasy,Undertow,我们使用的是Resteasy 2,但我们正在升级到Resteasy 3,HttpServletRequest注入总是null 我们改进的安全拦截器/过滤器如下所示: @Provider @ServerInterceptor @Precedence("SECURITY") public class SecurityInterceptor implements ContainerRequestFilter, ContainerResponseFilter { @Context pr

我们使用的是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;
    }
}
@ApplicationPath("/")
public class RestApplication extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> classes = new HashSet<Class<?>>();

    public RestApplication() {

        // Interceptors
        this.classes.add(SecurityInterceptor.class);

        // Services
        this.classes.add(MyService.class);
    }

    public Set<Class<?>> getClasses() {
        return this.classes;
    }

    public Set<Object> getSingletons() {
        return this.singletons;
    }
}
应用程序类如下所示:

@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;
    }
}
@ApplicationPath("/")
public class RestApplication extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> classes = new HashSet<Class<?>>();

    public RestApplication() {

        // Interceptors
        this.classes.add(SecurityInterceptor.class);

        // Services
        this.classes.add(MyService.class);
    }

    public Set<Class<?>> getClasses() {
        return this.classes;
    }

    public Set<Object> getSingletons() {
        return this.singletons;
    }
}
然而,查看和发布,我没有看到
HttpServletRequest
injectionprovider

这让我相信我可能需要一个额外的插件。这是安装的内容:

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

有什么想法吗?

您可以使用SecurityInterceptor的构造函数来获取以下值:

///...
    private HttpServletRequest request;
    private ServletContext context;

    public SecurityInterceptor(@Context HttpServletRequest request, @Context ServletContext context) {
       this.request = request;
       this.context = context;
    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        // The "servletRequest" won't be null anymore
        if (!isTokenValid(pmContext, method)) {
            requestContext.abortWith(ACCESS_DENIED);
        }
    }

 ///...

这将解决您的问题

基于@peeskillet建议,修改以返回新的类实例而不是单例解决了我的问题

因此,我修改的
javax.ws.rs.core.Application
文件如下所示:

@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;
    }
}
@ApplicationPath("/")
public class RestApplication extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> classes = new HashSet<Class<?>>();

    public RestApplication() {

        // Interceptors
        this.classes.add(SecurityInterceptor.class);

        // Services
        this.classes.add(MyService.class);
    }

    public Set<Class<?>> getClasses() {
        return this.classes;
    }

    public Set<Object> getSingletons() {
        return this.singletons;
    }
}
@ApplicationPath(“/”)
公共类重新启动应用程序扩展了应用程序{
private Set singleton=new HashSet();
私有集>();
公共重新应用程序(){
//拦截器
this.classes.add(SecurityInterceptor.class);
//服务
this.classes.add(MyService.class);
}

public set尝试将它们添加到类中(而不是单例)让resteasy创建它们。@peeskillet感谢您的回复。您的建议奏效了。感谢您的解决方案。我最终选择了@peeskillet推荐的方法,因为我们必须在许多地方进行更改才能接受构造函数中的值。我很乐意提供帮助。我没有看到“new SecurityInterceptor()”,但这也是一个问题。