Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorization in dropwizard

I want to make an small application using dropwizard in 0.8.0-rc3-SNAPSHOT. In that I want if any user will call my api user should pass an authtoken in the header part.What I have done till now is---

@Override
public void run(HelloWorldConfigurationhelloWorldConfiguration,Environment environment) throws Exception{
environment.jersey().register(new ViewResource());      
environment.servlets().addFilter("MyCustomRequestFilter", new MyCustomRequestFilter())
            .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),false, "/*");
}

public class MyCustomRequestFilter implements ContainerRequestFilter {
@Override
public ContainerRequest filter(ContainerRequest request) {
    System.out.print("test");
    if ( request.getQueryParameters().containsKey("validateMeParam") ) {
              /* validation logic */
    }
    // finished validation
    return request;
}
}

I don't know what I am doing wrong.It's not working.


1 Answers

ContainerRequestFilter is not a Servlet Filter, which is what you are assuming by doing environment.servlets().addFilter. This should be added to the Jersey configuration.

environment.jersey().register(MyCustomRequestFilter.class);

And don't forget the @Provider annotation on the filter class.

  • See more about filters in Jersey Filters in the Dropwizard documentation.

UPDATE

I see another serious problem. You say you're using Dropwizard 0.8.0, which uses Jersey 2. In which case, the ContainerRequestFilter you posted should not even exist. In Jersey 1, the parameter to the filter method, is ContainerRequest, while the argument in Jersey 2 is ContainerRequestContext. Please show you dependencies, and verify that the class you have above is the actual class

like image 51
Paul Samsotha Avatar answered Feb 21 '26 14:02

Paul Samsotha