java-在没有ServletContext的情况下使用Guice配置Shiro

我正在使用Guice连接Jetty服务器,并且想通过Apache Shiro添加一些安全性.

似乎Shiro需要配置ServletContext,但是问题是我没有;在配置时没有ServletContext(例如,如文档所述,在ServletModule中). ServletContext在GuiceServletContextListener中可用,但是目前,我的注射器已经创建,因此安装Shiro模块为时已晚.

我试图通过Guice提供程序(Provider< ServletContext>)向Shiro提供ServletContext,但是仍然没有成功.我认为此提供程序将在创建ServletContext后提供服务.这也给出警告:

"com.google.inject.servlet.InternalServletModule$BackwardsCompatibleServletContextProvider get
WARNING: You are attempting to use a deprecated API (specifically, attempting to @Inject ServletContext inside an eagerly created singleton. While we allow this for backwards compatibility, be warned that this MAY have unexpected behavior if you have more than one injector (with ServletModule) running in the same JVM."

创建注射器后,如何安装Shiro Web模块?

解决方法:

获取ServletContext的标准方法是扩展GuiceServletContextListener.

Imho对API的主要监督.

http://code.google.com/p/google-guice/issues/detail?id=603

这里还有一个教程:

https://issues.apache.org/jira/browse/SHIRO-320

(编辑:阅读评论后)
您有2个选择:

>重建代码,仅在GuiceServletContextListener中创建注入器
>使用儿童喷油器(tric脚)

对于子注入器,只有由子注入器创建的实例将获得“ Shiroed”.请记住:为子注入器创建的即时绑定将尽可能在祖先注入器中创建.

事情将按您的Servlet和过滤器的预期进行,但是如果您已创建的Injector中包含业务逻辑,则它们将看不到Shiro.您可以通过几种方法解决此问题…

上一篇:java-什么是紧凑的,可嵌入的Servlet容器,适合与Vaadin一起使用?


下一篇:java – Jetty,JNDI,Postgresql:找不到类