Skip to content

Instantly share code, notes, and snippets.

View javaeeeee's full-sized avatar

Dmitry Noranovich javaeeeee

View GitHub Profile
@javaeeeee
javaeeeee / Dropwizard Maven Authentication dependency
Created February 10, 2015 03:46
Dropwizard Maven Authentication dependency
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-auth</artifactId>
<version>${dropwizard.version}</version>
</dependency>
@javaeeeee
javaeeeee / Dropwizard Authenticator Subclass
Created February 10, 2015 03:48
Dropwizard Authenticator Subclass
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();
@javaeeeee
javaeeeee / Dropwizard Sub-resource Method
Created February 10, 2015 03:49
Dropwizard Sub-resource Method
public String getTailoredGreetingWithQueryParam(@DefaultValue("world")
@QueryParam("name") String name)
@javaeeeee
javaeeeee / Dropwizard Sub-resource Guava Optional
Created February 10, 2015 03:50
Dropwizard Sub-resource Guava Optional
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");
}
@javaeeeee
javaeeeee / Dropwizard Authenticator Registration
Created February 10, 2015 03:52
Dropwizard Authenticator Registration
@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
}
@javaeeeee
javaeeeee / Dropwizard Secured Resource
Created February 10, 2015 03:53
Dropwizard Secured Resource
@Path("secured_hello")
public class SecuredHelloResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getGreeting(@Auth User user) {
return "Hello world!";
}
}
@javaeeeee
javaeeeee / Dropwizard Custom Configuration
Created February 10, 2015 03:55
Dropwizard Custom Configuration
## Configuration file for DWGettingStarted application.
---
# User login.
login: javaeeeee
# User password.
password: crimson
@javaeeeee
javaeeeee / Dropwizard Configuration Subclass
Created February 10, 2015 03:56
Dropwizard Configuration Subclass
public class DWGettingStartedConfiguration extends Configuration {
@NotNull
private String login;
@NotNull
private String password;
@JsonProperty
public String getLogin() {
@javaeeeee
javaeeeee / Dropwizard Authenticator with Configuration
Created February 10, 2015 03:58
Dropwizard Authenticator with Configuration
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;
@javaeeeee
javaeeeee / Register Dropwizard Authenticator
Created February 10, 2015 03:59
Register Dropwizard Authenticator
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)));
// ...
}