Question

Nous utilisons Dropwizard pour notre prochain projet et l'une des choses que nous devrons implémenter est un mécanisme de contrôle d'accès basé sur les rôles.

Existe-t-il un moyen simple et standard de le faire à l'aide de Dropwizard ou des exemples que je peux suivre ?

Était-ce utile?

La solution

Avez-vous jeté un oeil à dropwizard-auth?Il est très facile de brancher la méthode d'authentification de votre choix (Shiro, Spring, etc.).Il prend également en charge OAuth2 si vous voulez aller aussi loin...

Vous pouvez implémenter un authentificateur Shiro comme ceci :

public class BasicAuthenticator implements Authenticator<BasicCredentials, Subject> {

  @Override
  public Optional<Subject> authenticate(BasicCredentials credentials) throws AuthenticationException {
    Subject subject = SecurityUtils.getSubject();
    try {
      subject.login(new UsernamePasswordToken(credentials.getUsername(), credentials.getPassword(), false));
      return Optional.of(subject);
    } catch (UnknownAccountException | IncorrectCredentialsException | LockedAccountException e) {
    } catch (AuthenticationException ae) {
    }
    return Optional.absent();
  }

}

Et vous pouvez enregistrer Shiro avec l'environnement comme celui-ci (appelé depuis votre run méthode):

void configureAuthentication(Environment environment) {
  JdbcRealm realm = getJdbcRealm(); // However your Shiro realm is configured

  DefaultSecurityManager securityManager = new DefaultSecurityManager(realm);
  SecurityUtils.setSecurityManager(securityManager);

  environment.jersey().register(new BasicAuthProvider<Subject>(new BasicAuthenticator(), "Shiro"));
}

Et puis recherchez un rôle comme celui-ci :

@GET
public SecretPlan getSecretPlan(@Auth Subject subject) {
  if (user.hasRole("secretPlanner")) {
    return new SecretPlan();
  } else {
    return new NonSecretPlan();
  }
}

Autres conseils

Vous pouvez très bien utiliser Dropwizard fourni mécanismes d'authentification http://www.dropwizard.io/0.9.1/docs/manual/auth.html

@RolesAllowed("ADMIN")
@GET
public SecretPlan getSecretPlan(@Auth User user) {
   return dao.findPlanForUser(user);
}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top