This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependency> | |
| <groupId>io.dropwizard</groupId> | |
| <artifactId>dropwizard-auth</artifactId> | |
| <version>${dropwizard.version}</version> | |
| </dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GreetingAuthenticator | |
| implements Authenticator<BasicCredentials, User> { | |
| @Override | |
| public Optional<User> authenticate(BasicCredentials credentials) | |
| throws AuthenticationException { | |
| if ("crimson".equals(credentials.getPassword())) { | |
| return Optional.of(new User()); | |
| } else { | |
| return Optional.absent(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public String getTailoredGreetingWithQueryParam(@DefaultValue("world") | |
| @QueryParam("name") String name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public String getTailoredGreetingWithQueryParam( | |
| @QueryParam("name") Optional<String> name) { | |
| if (name.isPresent()) { | |
| return "Hello " + name.get(); | |
| } else { | |
| return "Hello world"; | |
| } | |
| //The same can be accomplished using or(...) method to provide the default value | |
| //return "Hello " + name.or("world"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Override | |
| public void run(final DWGettingStartedConfiguration configuration, | |
| final Environment environment) { | |
| environment.jersey().register(AuthFactory.binder( | |
| new BasicAuthFactory<>( | |
| new GreetingAuthenticator(), | |
| "SECURITY REALM", | |
| User.class))); | |
| // Resources are registered here | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Path("secured_hello") | |
| public class SecuredHelloResource { | |
| @GET | |
| @Produces(MediaType.TEXT_PLAIN) | |
| public String getGreeting(@Auth User user) { | |
| return "Hello world!"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Configuration file for DWGettingStarted application. | |
| --- | |
| # User login. | |
| login: javaeeeee | |
| # User password. | |
| password: crimson |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class DWGettingStartedConfiguration extends Configuration { | |
| @NotNull | |
| private String login; | |
| @NotNull | |
| private String password; | |
| @JsonProperty | |
| public String getLogin() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GreetingAuthenticator | |
| implements Authenticator<BasicCredentials, User> { | |
| private String login; | |
| private String password; | |
| public GreetingAuthenticator(String login, String password) { | |
| this.login = login; | |
| this.password = password; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void run(final DWGettingStartedConfiguration configuration, | |
| final Environment environment) { | |
| environment.jersey().register(AuthFactory.binder( | |
| new BasicAuthFactory<>( | |
| new GreetingAuthenticator(configuration.getLogin(), | |
| configuration.getPassword()), | |
| "SECURITY REALM", | |
| User.class))); | |
| // ... | |
| } |