当前位置: 首页 > 知识库问答 >
问题:

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

丌官嘉勋
2023-03-14

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

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

我试图通过 Guice 提供程序 (提供程序) 向 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模块呢?


共有2个答案

宓和同
2023-03-14

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

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

希望这能有所帮助

公良安邦
2023-03-14

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

Imho是API的一个主要疏忽。

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

这里还有一个教程:

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

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

  1. 重构代码仅在GuiceServletContextListener
  2. 中创建注入器
  3. 使用儿童注射器(棘手)

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

对于Servlets和html" target="_blank">过滤器来说,事情会像预期的那样工作,但是如果在已经创建的注入器中有业务逻辑,它们将看不到Shiro。您可以用几种方法来解决这个问题...

 类似资料:
  • 问题内容: 我正在尝试设置spring xml配置,而不必创建进一步的。但是,即使我将数据库属性包括在 spring.xml: 我在这里想念什么? 问题答案: 在entityManagerFactory bean定义中指定“ packagesToScan”和“ persistenceUnitName”属性。 请注意,这适用于Spring版本> 3.1

  • 我看到的所有解决方案都需要使用。但是,我想在Eclipse之外的单个文件上使用CDT解析器。那有什么办法吗?

  • 我知道这可以在Servlet3.0中通过@Webservlet注释实现,在这里您只需分配url模式,而不必在web.xml中进行任何配置。是否有一种方法可以通过编程方式为运行Servlet2.5的应用程序分配servlets url模式? 我正在创建一个库,多个应用程序将依赖于它,并试图使它,以便这些应用程序中的每一个都不必显式配置任何servlet url映射,我正在创建的库中的servlet在

  • 我正在尝试构建一个基本的REST服务,它使用Spring Security和OAuth2.0身份验证和授权进行安全保护。 我试图限制所涉及的元素,所以我不是复制粘贴依赖于Spring bean、Spring MVC等的Spring Security Oath XML配置,而是直接使用Spring Security Oauth类。 尝试从/oauth/Token获取访问令牌时遇到了一个障碍。我可能缺

  • 在设备上没有安装Google Play服务(aosp)的情况下,是否可以使用Firebase Remote Config? 我确实在我的android项目中遵循了这个设置:(通过https://firebase.google.com/docs/android/setup): 将类路径'com.google.gms:google-services:4.3.3'添加到主分级文件 将实现“com.goo

  • 我正在尝试设置SpringXML配置,而不必创建进一步的。但是我经常遇到以下异常,即使我在 spring.xml: 我错过了什么?