简体   繁体   English

在Jersey过滤器中添加自定义标头不起作用

[英]Adding custom headers in Jersey Filters isn't working

I am new to DropWizard framework and Jersey in general. 一般来说,我是DropWizard框架和Jersey的新手。 So I could be completely off the track with what I am trying to accomplish here. 因此,我在这里可能要完成的工作可能完全偏离了轨道。

I am running into an issue where the parameters I add to my request header through a pre-matching Request filter are not reaching the custom provider for my AuthCredentials object. 我遇到了一个问题,我通过预匹配的请求过滤器添加到我的请求标头中的参数没有到达我的AuthCredentials对象的自定义提供程序。 I am trying to provide a AuthCredentials object for every resource that requires auth from the header parameters . 我正在尝试为每个需要从标头参数进行身份验证的资源提供AuthCredentials对象。 Since one of the services I am going to rely on for providing auth headers isn't ready yet i am faking them out by adding them in the pre-matching request filter myself. 由于我要提供身份验证标头所依赖的一项服​​务尚未准备好,因此我自己通过在匹配前请求过滤器中添加它们来伪造它们。

Here's my code 这是我的代码

 @PreMatching
 @Provider
 class MyFilter implements ContainerRequestFilter{
   @Override
   public void filter(ContainerRequestContext requestContext) throws IOException {
        requestContext.getHeaders().add("param1", "value1");
   }
}


class MyAuthCredentialsFactory implements Factory<AuthCredentials>{
     @Inject
     public MyAuthCredentialsFactory(HttpServletRequest request){
          System.out.println("Param1 header value = " + request.getHeader("param1")); // <---This prints null
     }
}

I am registering these classes in my applications' run method in drop wizard framework as shown below. 我在拖放向导框架的应用程序的run方法中注册这些类,如下所示。

@Override
    public void run(ServerConfigType configuration,
                    Environment environment) {
        environment.jersey().register(new AbstractBinder() {
               @Override
               protected void configure() {
                  bindFactory(MyAuthCredentialsFactory.class).to(AuthCredentials.class).in(RequestScoped.class);               
               }
    });
    environment.jersey().register(MyFilter.class);

My resource uses auth credentials like this 我的资源使用这样的身份验证凭据

@Path("/pathtomyresource")
public class MyApi {
    @Inject AuthCredentials authCredentials;
}

I am using DropWizard 0.8.3, and Jackson 2.5.3. 我正在使用DropWizard 0.8.3和Jackson 2.5.3。

When I print the value of the param1 from the header I am getting a null. 当我从标题打印param1的值时,我得到的是空值。 I looked at the HttpServletRequest's header names in the debugger, and didn't find the param1 in there which explains why the value is null. 我在调试器中查看了HttpServletRequest的标头名称,但没有在其中找到解释为什么该值为null的param1。 I did a lot of research on the internet on this and found that it should work but isn't working for me. 我在互联网上对此进行了大量研究,发现它应该可以工作但对我不起作用。 At this point I am puzzled and I would really appreciate any help with this. 在这一点上,我感到困惑,非常感谢您的帮助。

HttpServletRequest and ContainerRequestContext are not the same thing, and setting something in the latter will not result in it being put in the former. HttpServletRequestContainerRequestContext不是同一件事,在后者中进行设置不会导致将其放入前者中。 You can inject ContainerRequestContext into the Factory though, and just get the property from there 您可以将ContainerRequestContext注入到Factory ,然后从那里获取属性

 @Inject
 public MyAuthCredentialsFactory(ContainerRequestContext request){
 }

Personally what I just do, when I need to access the request context, is instead of implementing Factory , I just extend AbstractContainerRequestValueFactory as seen here . 个人什么,我只是做,当我需要访问请求上下文,是不是实现Factory ,我只是延长AbstractContainerRequestValueFactory所看到这里 Then you can just call getContainerRequest() , to get the ContainerRequest (which is actually the same ContainerRequestContext . 然后,您可以调用getContainerRequest()来获取ContainerRequest (实际上是相同的ContainerRequestContext

class MyAuthCredentialsFactory 
        extends AbstractContainerRequestValueFactory<AuthCredentials> {
    @Override
    public AuthCrendials provide() {
        ContainerRequest request = getContainerRequest();
        String header = request.getHeaderString(...);
    }
}

Alternatively, instead of setting a header, you could set an arbitrary property on the ContainerRequestContext with setProperty(key, value) , then in the factory just get it with getProperty(key) 另外,除了设置标题之外,您还可以使用setProperty(key, value)ContainerRequestContext上设置任意属性,然后在工厂中使用getProperty(key)获取它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM