A bundle for securing Dropwizard with Apache Shiro.
Add the following dependencies to pom.xml
in an existing project already using Dropwizard:
<dependency>
<groupId>org.secnod.dropwizard</groupId>
<artifactId>dropwizard-shiro</artifactId>
<version>0.3.0</version>
</dependency>
Version compatibility:
Dropwizard | Dropwizard Shiro |
---|---|
3.0.x | 0.4.0 |
2.0.x | 0.3.0 |
1.0.x | 0.2.0 |
0.9.x | 0.2.0 |
0.8.x | 0.2.0 |
0.7.x | 0.1.1 |
Add the bundle to the Dropwizard environment:
public class ApiApplication extends Application<ApiConfiguration> {
private final ShiroBundle<ApiConfiguration> shiro = new ShiroBundle<ApiConfiguration>() {
@Override
protected ShiroConfiguration narrow(ApiConfiguration configuration) {
return configuration.shiro;
}
};
@Override
public void initialize(Bootstrap<ApiConfiguration> bootstrap) {
bootstrap.addBundle(shiro);
}
}
Edit the Dropwizard YAML config file:
shiro:
iniConfigs: ["classpath:shiro.ini"]
Now create and configure shiro.ini
in the default package
on the classpath.
Shiro exceptions can be mapped to HTTP responses by adding the exception mapper for Shiro exceptions.
public class ApiApplication extends Application<ApiConfiguration> {
@Override
public void run(ApiConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(new ShiroExceptionMapper());
}
}
It is not added by default as most applications will need to customize how Shiro exceptions are mapped to HTTP responses.
The TypeFactory
for a custom user class must be added to Dropwizard:
public class ApiApplication extends Application<ApiConfiguration> {
@Override
public void run(ApiConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(new UserFactory());
}
}
See http://github.com/silb/shiro-jersey#using-shiro.
shiro:
filterUrlPattern = "/*" # The URL pattern for the Shiro servlet filter
Override ShiroBundle.createFilter(T)
.
Override ShiroBundle.createRealms(T)
.
One of my realms has a dependency to an object that is constructed in Application.run(T, Environment). How do I pass it to the realm?
Store the object in a field in the application class:
public class ApiApplication extends Application<ApiConfiguration> {
MyObject myObject;
private final ShiroBundle<ApiConfiguration> shiro = new ShiroBundle<ApiConfiguration>() {
@Override
protected Collection<Realm> createRealms(ApiConfiguration configuration) {
Realm r = new SomeRealm(myObject);
return Collections.singleton(r);
}
};
@Override
public void run(ApiConfiguration configuration, Environment environment) throws Exception {
myObject = new MyObject(configuration);
}
}
Add a SessionHandler
to Jetty:
environment.getApplicationContext().setSessionHandler(new SessionHandler());
See the supplied example application.
mvn -Pintegration-tests test
First, compile against the default Dropwizard version in the POM:
mvn clean compile
Then run the tests by overriding the dependencies on the command line:
mvn -Pintegration-tests -Ddropwizard.version=0.8.0 -Djersey.version=2.16 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=0.8.1 -Djersey.version=2.17 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=0.8.2 -Djersey.version=2.19 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=0.8.3 -Djersey.version=2.19 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=0.8.4 -Djersey.version=2.21 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=0.9.0 -Djersey.version=2.22.1 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=0.9.1 -Djersey.version=2.22.1 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=0.9.2 -Djersey.version=2.22.1 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=1.0.0 -Djersey.version=2.23.1 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=2.0.0 -Djersey.version=2.29.1 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=2.0.20 -Djersey.version=2.33 surefire:test
mvn -Pintegration-tests -Ddropwizard.version=3.0.1 -Djersey.version=2.39.1 surefire:test
Note that running the integration tests against DropWizard 1.0 requires Java 8.