I develop a rest api with Jersey and want to use Google Guice for Dependency Injection and Apache Shiro as a security framwork.
For Authentication i created a custom Realm to which I have to inject an custom Authenticator which is connected to the EntityManager.
However the dependency is not injected into the Realm. I guess that shiro.ini (in which I have to define the used realm) is not managed by guice.
How can I inject dependencies into Apache Shiro, especially into the used Realm?
My web.xml only has a filter mapped to guice
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>GuiceServletConfig</listener-class>
</listener>
</web-app>
My GuiceServletConfig configures all dependencies including the CustomRealm
public class GuiceServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new DbModule(), new JerseyServletModule() {
@Override
protected void configureServlets() {
// ...
// CustomRealm is only used when i use it as an eager singleton
bind(CustomRealm.class).asEagerSingleton();
bind(org.apache.shiro.web.servlet.IniShiroFilter.class).in(Singleton.class);
filter("/*").through(org.apache.shiro.web.servlet.IniShiroFilter.class);
serve("/api/*").with(GuiceContainer.class);
}
});
}
}
The shiro ini only defines the realm
[main]
myRealm = CustomRealm
[users] # for testing
root = secret,admin
[roles] # for testing
admin = *
[urls]
/api/** = authcBasic