Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. package org.dropwizard.auth;
  2.  
  3. import io.dropwizard.Application;
  4. import io.dropwizard.auth.AuthDynamicFeature;
  5. import io.dropwizard.auth.AuthValueFactoryProvider;
  6. import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
  7. import io.dropwizard.setup.Bootstrap;
  8. import io.dropwizard.setup.Environment;
  9. import org.dropwizard.auth.core.DropwizardAuthenticator;
  10. import org.dropwizard.auth.core.DropwizardAuthorizer;
  11. import org.dropwizard.auth.core.User;
  12. import org.dropwizard.auth.resource.AuthenticatedResource;
  13. import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
  14.  
  15. public class AuthApplication extends Application<AuthConfiguration> {
  16.  
  17.     public static void main(final String[] args) throws Exception {
  18.         new AuthApplication().run(args);
  19.     }
  20.  
  21.     @Override
  22.     public String getName() {
  23.         return "Auth";
  24.     }
  25.  
  26.     @Override
  27.     public void initialize(final Bootstrap<AuthConfiguration> bootstrap) {
  28.         // TODO: application initialization
  29.     }
  30.  
  31.     @Override
  32.     public void run(final AuthConfiguration configuration,
  33.                     final Environment environment) {
  34.  
  35.         BasicCredentialAuthFilter<User> authFilterBuilder =
  36.                 new BasicCredentialAuthFilter.Builder<User>()
  37.                 .setAuthenticator(new DropwizardAuthenticator())
  38.                 .setAuthorizer(new DropwizardAuthorizer())
  39.                 .setRealm("A REALM")
  40.                 .buildAuthFilter();
  41.  
  42.         environment.jersey().register(new AuthDynamicFeature(authFilterBuilder));
  43.         environment.jersey().register(RolesAllowedDynamicFeature.class);
  44.  
  45.         // Allows @Auth annotation to inject custom principals into resource
  46.         environment.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));
  47.  
  48.         // Registering resources
  49.         environment.jersey().register(new AuthenticatedResource());
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement