项目作者: nightowlengineer

项目描述 :
Use a configured header to provide a user into a Dropwizard application, e.g. from an upstream proxy
高级语言: Java
项目地址: git://github.com/nightowlengineer/dw-header-auth-bundle.git
创建时间: 2018-09-29T17:28:55Z
项目社区:https://github.com/nightowlengineer/dw-header-auth-bundle

开源协议:MIT License

下载


Header authentication bundle for Dropwizard

This bundle provides the ability to define a header to take a user (or ‘principal’) ID from, and use as an authenticated
user throughout the application. This is mainly intended for use in applications that sit behind a reverse proxy that
will extract or provide the user information as required. This bundle also registers the RolesAllowedDynamic feature,
meaning you can add @RolesAllowed annotations to restrict calls as needed.

It’s important that any application that relies on authentication done in this manner is properly secured from
malicious requests (for example, by locking down the application to only accept connections from the upstream proxy)

Build Status

Maven Setup

  1. <dependency>
  2. <groupId>engineer.nightowl</groupId>
  3. <artifactId>dw-header-auth-bundle</artifactId>
  4. <version>1.3.5-1</version>
  5. </dependency>

Getting Started

Implement the HeaderAuthConfiguration:

  1. public class SampleConfiguration extends Configuration implements HeaderAuthConfiguration {
  2. @NotNull
  3. @JsonProperty("authentication")
  4. private HeaderAuthConfiguration headerAuthConfiguration;
  5. @Override
  6. public HeaderAuthConfiguration getHeaderAuthConfiguration() {
  7. return headerAuthConfiguration;
  8. }
  9. }

Add the bundle:

  1. public class SampleService extends Application<SampleConfiguration> {
  2. public static void main(String[] args) throws Exception {
  3. new SampleService().run(args);
  4. }
  5. @Override
  6. public void initialize(Bootstrap<SampleConfiguration> bootstrap) {
  7. // User represents your internal user representation which extends Principal
  8. // UserService represents your internal user information source which extends PrincipalService
  9. final HeaderAuthBundle<User, UserService> headerAuthBundle =
  10. new HeaderAuthBundle<>(User.class, new UserService());
  11. bootstrap.addBundle(headerAuthBundle);
  12. }
  13. @Override
  14. public void run(SampleConfiguration configuration, Environment environment) {
  15. ...
  16. }
  17. }

Inside your service’s configuration yml file, add the header name that should be inspected:

  1. authentication:
  2. headerName: USER_ID

And that’s it! Any request that is sent to your service will inspect the header USER_ID and use this to return a single
user and their roles from your UserService.