- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试实现一个简单的 Guice+Shiro Web 应用程序。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.0"
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_3_0.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>
ShiroWebModuleShiroModule 的扩展,用于设置 Web 环境并允许过滤器链配置
class SecurityModule extends ShiroWebModule {
SecurityModule(ServletContext sc) {
super(sc);
}
protected void configureShiroWeb() {
try {
bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
} catch (NoSuchMethodException e) {
addError(e);
}
addFilterChain("/login/**", AUTHC);
addFilterChain("/app/**", AUTHC);
}
@Provides
Ini loadShiroIni() {
return Ini.fromResourcePath("classpath:shiro.ini");
}
}
GuiceServletContextListener
public class GuiceServletConfig extends GuiceServletContextListener {
private ServletContext servletContext;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
this.servletContext = servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
Injector injector = Guice.createInjector(
new SecurityModule(servletContext),
ShiroWebModule.guiceFilterModule(),
new GuiceServletModule()
);
return injector;
}
}
调试
22:56:38,350 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,357 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,365 DEBUG main org.apache.shiro.realm.text.IniRealm:processDefinitions:185 - Discovered the [users] section. Processing...
22:56:38,372 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,373 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,376 WARN main org.apache.shiro.realm.text.IniRealm:onInit:139 - Users or Roles are already populated. Configured Ini instance will be ignored.
22:56:38,377 DEBUG main org.apache.shiro.realm.text.IniRealm:onInit:145 - Instance is already populated with users or roles. No additional user/role population will be performed.
22:56:38,395 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,396 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,397 DEBUG main org.apache.shiro.realm.text.IniRealm:processDefinitions:185 - Discovered the [users] section. Processing...
22:56:38,400 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,401 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,403 WARN main org.apache.shiro.realm.text.IniRealm:onInit:139 - Users or Roles are already populated. Configured Ini instance will be ignored.
22:56:38,404 DEBUG main org.apache.shiro.realm.text.IniRealm:onInit:145 - Instance is already populated with users or roles. No additional user/role population will be performed.
22:56:38,406 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,406 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,407 DEBUG main org.apache.shiro.realm.text.IniRealm:processDefinitions:185 - Discovered the [users] section. Processing...
22:56:38,411 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,411 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,414 WARN main org.apache.shiro.realm.text.IniRealm:onInit:139 - Users or Roles are already populated. Configured Ini instance will be ignored.
22:56:38,414 DEBUG main org.apache.shiro.realm.text.IniRealm:onInit:145 - Instance is already populated with users or roles. No additional user/role population will be performed.
22:56:38,415 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,415 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,416 DEBUG main org.apache.shiro.realm.text.IniRealm:processDefinitions:185 - Discovered the [users] section. Processing...
22:56:38,420 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
22:56:38,421 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
22:56:38,426 WARN main org.apache.shiro.realm.text.IniRealm:onInit:139 - Users or Roles are already populated. Configured Ini instance will be ignored.
22:56:38,426 DEBUG main org.apache.shiro.realm.text.IniRealm:onInit:145 - Instance is already populated with users or roles. No additional user/role population will be performed.
为什么 IniRealm 被实例化四次?可以吗?
谢谢。
最佳答案
问题出在 bindRealm().toConstructor(Constructor 构造函数) 方法中。我使用了另一种方法来实例化 IniRealm:
class SecurityModule extends ShiroWebModule {
private final Logger logger = LoggerFactory.getLogger(getClass());
SecurityModule(ServletContext sc) {
super(sc);
}
@Override
protected void configureShiroWeb() {
bindRealm().to(IniRealm.class);
logger.info("SecurityModule.configureShiroWeb() method called");
}
@Provides
@Singleton
IniRealm loadIniRealm(Ini ini) {
IniRealm realm = new IniRealm(ini);
return realm;
}
@Provides
@Singleton
Ini loadShiroIni() {
logger.info("SecurityModule.loadShiroIni() method called");
return Ini.fromResourcePath("classpath:shiro.ini");
}
}
调试:
08:28:03,354 INFO main org.shirotest.app.SecurityModule:loadShiroIni:50 - SecurityModule.loadShiroIni() method called
08:28:03,354 DEBUG main org.apache.shiro.io.ResourceUtils:loadFromClassPath:159 - Opening resource from class path [shiro.ini]
08:28:03,370 DEBUG main org.apache.shiro.config.Ini:load:342 - Parsing [users]
08:28:03,385 DEBUG main org.apache.shiro.realm.text.IniRealm:processDefinitions:185 - Discovered the [users] section. Processing...
完整的代码示例可以在这里找到:https://issues.apache.org/jira/browse/SHIRO-320
关于java - 希罗+吉斯 : Why IniRealm is instantiated four times?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24539675/
我刚遇到 MSVC(版本 12 更新 5)的问题: 如果模板函数具有通过 SFINAE 禁用的重载,则显式实例化模板函数会失败。但是,调用该函数(从而隐式实例化它)是有效的。 示例代码: #inclu
我正在阅读一本关于 DI 的书,该书总是谈论 DI 框架“实例化对象图”。为什么这样说而不是“实例化对象”? 最佳答案 对象图由保存彼此引用的对象组成。在这种情况下,图的另一个名称是网络。 如果 IO
一个类Customers实例化许多其他类(例如CustomersFromMysql、CustomersFromPostgeSQL),所有查询数据库都返回客户名称。现在,这些客户名称返回为例如 name
当我尝试调用 listenEventReducer 时,出现了这个奇怪的错误。该方法知道类型,但仍然不确定我哪里出错了。 import 'package:test/test.dart'; enum O
我正在尝试使用 org.hibernate.Interceptor.instantiate() 来拦截实例化(显然)并使用默认构造函数之外的构造函数手动实例化特定对象。如果要阅读此方法的 JavaDo
public class TestingClass { public static void main(String[] args) { int numberRooms = 6
为什么 C++ 以这样的方式创建,如果您有一个类 A 并声明一个类型 A 的数组,那么整个数组将填充使用该类的默认构造函数实例化的对象? 最佳答案 因为当您创建一个给定大小的数组时,数组的每个元素都必
考虑下面的例子 template struct S { A a; void foo() {} }; template void bar() { S *p = 0; } templat
Note that code is instantiated only for member functions that are called. For class templates, membe
当我尝试运行这段代码时: import java.io.*; import java.util.*; public class TwoColor { public static void ma
每当我尝试在 Unity 3D 中实例化粒子系统时,命令都会定位粒子但不会播放/运行动画。 这是我的代码 GameObject impactGO = Instantiate(impactEffect,
我使用以下代码在 verilog 中实例化二维内存 reg [15:0] data_pattern_even [3:0] = {16'hFFFF,16'hFFFF,16'hFFFF,16'hFFFF
假设我获得了我作为 String 创建的类的名称。 .如何使用该字符串中包含的名称实例化类?我知道它将派生自某个父类,但实际类会有所不同。 最佳答案 var instance : MyClass =
python 的 attrs 包提供了一种在实例化时验证传递的变量的简单方法 (example taken from attrs page): >>> @attr.s ... class C(obje
我收到 java 空指针异常。我无法解决它。我已在 testbase 类中初始化驱动程序,并希望在我的 Testing_TVO 类中使用相同的驱动程序 这是我的测试基类 public class te
我对 Java 编程还比较陌生,可能错过了一些明显的东西,所以请耐心等待。 我正在创建一个程序,该程序使用 Swing API 和 JDesktopPane 来创建 GUI。主屏幕上有一个按钮,上面写
python 的 attrs 包提供了一种在实例化时验证传递的变量的简单方法 (example taken from attrs page): >>> @attr.s ... class C(obje
C++ 模板中的“延迟实例化”是什么意思? 最佳答案 延迟实例化是指直到第一次使用对应的实体时才实例化模板。例如,您有一个模板化函数: template void YourFunction() {
当我阅读 spring 教程时,我发现了这样的内容: LocalChangeInterceptor localChangeInterceptor; localChangeInterceptor = n
我正在实现 unforgettable factory .一切正常,但有一件事:类(class)有时没有注册。 我认为关键部分是 Registrar::registered成员。如果使用它,“真正有趣
我是一名优秀的程序员,十分优秀!