Jersey で REST API を開発しており、依存性注入に Google Guice を使用し、セキュリティ フレームワークとして Apache Shiro を使用したいと考えています。
認証のために、EntityManagerに接続されているカスタムAuthenticatorを注入する必要があるカスタムRealmを作成しました。
ただし、依存関係はレルムに注入されません。shiro.ini (使用される領域を定義する必要があります) は、guice によって管理されていないと思います。
依存関係を Apache Shiro、特に使用されている Realm に注入するにはどうすればよいですか?
私の web.xml には、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>
私の GuiceServletConfig は、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);
}
});
}
}
白鬼は領域のみを定義する
[main]
myRealm = CustomRealm
[users] # for testing
root = secret,admin
[roles] # for testing
admin = *
[urls]
/api/** = authcBasic