org.apache.shiro.web.filter.authc.LogoutFilter 已在 ShiroWebModule 中配置
org.apache.shiro.web.filter.authc.LogoutFilter is already configured in ShiroWebModule
我使用的是 Shiro 1.7.1 和 Guice 4.2.3,下面是我的 POM 文件的片段,
<properties>
<shiro.version>1.7.1</shiro.version>
<guice.version>4.2.3</guice.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-guice</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
我正在通过创建新的 class、
来自定义 Shiro 的 LogoutFilter
package com.myshiro.myshiro;
import org.apache.shiro.web.filter.authc.LogoutFilter;
public class MyLogoutFilter extends LogoutFilter {
}
并将org.apache.shiro.web.filter.authc.LogoutFilter
绑定到上面自定义的MyLogoutFilter
,
package com.myshiro.myshiro;
public class MyShiroModule extends ShiroWebModule {
public MyShiroModule(ServletContext servletContext) {
super(servletContext);
}
protected void configureShiroWeb() {
try {
bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
} catch (NoSuchMethodException e) {
addError(e);
}
bind(org.apache.shiro.web.filter.authc.LogoutFilter.class).to(MyLogoutFilter.class).in(Scopes.SINGLETON);
addFilterChain("/logout", LOGOUT);
}
}
我尝试在单元测试中创建 Guice 注入器 class 像这样,
public class MyShiroModuleTest {
@Mock
private ServletContext servletContext;
@Test
public void test() {
Guice.createInjector(new MyShiroModule(servletContext));
}
}
失败并出现以下错误,
1) Binding to null instances is not allowed. Use toProvider(Providers.of(null)) if this is your intended behaviour.
at org.apache.shiro.guice.web.ShiroWebModule.configureShiro(ShiroWebModule.java:136)
2) A binding to org.apache.shiro.web.filter.authc.LogoutFilter was already configured at com.myshiro.myshiro.MyShiroModule.configureShiroWeb(MyShiroModule.java:25).
at org.apache.shiro.guice.web.ShiroWebModule.setupFilterChainConfigs(ShiroWebModule.java:209)
从上面的第二个注释,它解释了绑定到 org.apache.shiro.web.filter.authc.LogoutFilter 已经在 MyShiroModule 和 ShiroWebModule 中配置。您知道如何绑定到我的自定义 LogoutFilter 吗?
Shiro 1 中没有出现此问题。3.x。
我的示例项目可用 here,您可以在 mvn clean install
.
时简单地看到错误
听起来你的问题与 Guice 4 有关,与 Shiro 无关。与其重复使用相同的绑定键,不如定义一个新的绑定键,例如:
bind(MyLogoutFilter.class).to(MyLogoutFilter.class).in(Scopes.SINGLETON);
addFilterChain("/logout", Key.get(MyLogoutFilter.class));
我使用的是 Shiro 1.7.1 和 Guice 4.2.3,下面是我的 POM 文件的片段,
<properties>
<shiro.version>1.7.1</shiro.version>
<guice.version>4.2.3</guice.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-guice</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${shiro.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-servlet</artifactId>
<version>${guice.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
...
</dependencies>
我正在通过创建新的 class、
来自定义 Shiro 的 LogoutFilterpackage com.myshiro.myshiro;
import org.apache.shiro.web.filter.authc.LogoutFilter;
public class MyLogoutFilter extends LogoutFilter {
}
并将org.apache.shiro.web.filter.authc.LogoutFilter
绑定到上面自定义的MyLogoutFilter
,
package com.myshiro.myshiro;
public class MyShiroModule extends ShiroWebModule {
public MyShiroModule(ServletContext servletContext) {
super(servletContext);
}
protected void configureShiroWeb() {
try {
bindRealm().toConstructor(IniRealm.class.getConstructor(Ini.class));
} catch (NoSuchMethodException e) {
addError(e);
}
bind(org.apache.shiro.web.filter.authc.LogoutFilter.class).to(MyLogoutFilter.class).in(Scopes.SINGLETON);
addFilterChain("/logout", LOGOUT);
}
}
我尝试在单元测试中创建 Guice 注入器 class 像这样,
public class MyShiroModuleTest {
@Mock
private ServletContext servletContext;
@Test
public void test() {
Guice.createInjector(new MyShiroModule(servletContext));
}
}
失败并出现以下错误,
1) Binding to null instances is not allowed. Use toProvider(Providers.of(null)) if this is your intended behaviour.
at org.apache.shiro.guice.web.ShiroWebModule.configureShiro(ShiroWebModule.java:136)
2) A binding to org.apache.shiro.web.filter.authc.LogoutFilter was already configured at com.myshiro.myshiro.MyShiroModule.configureShiroWeb(MyShiroModule.java:25).
at org.apache.shiro.guice.web.ShiroWebModule.setupFilterChainConfigs(ShiroWebModule.java:209)
从上面的第二个注释,它解释了绑定到 org.apache.shiro.web.filter.authc.LogoutFilter 已经在 MyShiroModule 和 ShiroWebModule 中配置。您知道如何绑定到我的自定义 LogoutFilter 吗?
Shiro 1 中没有出现此问题。3.x。
我的示例项目可用 here,您可以在 mvn clean install
.
听起来你的问题与 Guice 4 有关,与 Shiro 无关。与其重复使用相同的绑定键,不如定义一个新的绑定键,例如:
bind(MyLogoutFilter.class).to(MyLogoutFilter.class).in(Scopes.SINGLETON);
addFilterChain("/logout", Key.get(MyLogoutFilter.class));