3

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

似乎 Shiro 需要配置一个ServletContext,但问题是我在配置时没有 ServletContext(例如在文档所述的ServletModule中)。ServletContextGuiceServletContextListener中可用,但此时,我的注入器已经创建,所以安装Shiro模块为时已晚。

我尝试通过Guice提供程序(Provider<ServletContext> )将ServletContext提供给Shiro ,但仍然没有成功。我认为这个提供者将在创建后为 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 模块?

4

2 回答 2

2

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

恕我直言,对 API 的主要监督。

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

这里还有一个教程:

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

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

  1. 重构代码以仅在GuiceServletContextListener
  2. 使用儿童注射器(棘手)

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

对于您的 Servlet 和过滤器,事情将按预期工作,但如果您在已经创建的 Injector 中有业务逻辑,他们将看不到 Shiro。您可以通过几种方式解决此问题...

于 2012-11-30T15:46:22.663 回答
2

有同样的问题,并使用来自@Mircea 的信息,我所做的只是将 GuiceServletContextLister 转换为 ServletContext 换句话说

Guice.createInjector(new ShiroWebModule((ServletContext)this);

希望这会有所帮助

于 2013-01-02T09:25:12.267 回答