Multiple authenticators for different resources in dropwizard
Let's say I have two resources, / authenticate and / protected_resource. Authentication validates the user / password provided using HTTP Basic Authentication and, if successful, generates a jwt token ./protected_resource expects the token to be provided in the header of the incoming request, as in oauth. Can I set two different authenticators / filters: "basic" and "jwt" and specify that base is for / authenticate and jwt for / protected_resource?
+3
source to share
1 answer
Yes, you can.
I haven't tried this on purpose, but looking at the source code and according to my experience playing with AuthProviders it should work.
Just make sure these authenticators return different classes.
environment.jersey().register(AuthFactory.binder(new BasicAuthFactory<User>(new UserAuthenticator(), "User authentication", User.class)));
environment.jersey().register(AuthFactory.binder(new OAuthFactory<Token>(new JwtAuthenticator(), "Jwt authentication", Token.class)));
and then in the resource classes:
@Path("/authenticate")
public String authenticate(@Auth User user){
return jwtToken;
}
@Path("/protected_resource")
public FooDto getFoo(@Auth Token token){
// do stuff
}
0
source to share