简体   繁体   中英

Adding custom headers in Jersey Filters isn't working

I am new to DropWizard framework and Jersey in general. 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. I am trying to provide a AuthCredentials object for every resource that requires auth from the header parameters . 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.

@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.

When I print the value of the param1 from the header I am getting a null. 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. 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. You can inject ContainerRequestContext into the Factory though, and just get the property from there

 @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 . Then you can just call getContainerRequest() , to get the ContainerRequest (which is actually the same 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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