【发布时间】:2012-02-15 16:06:09
【问题描述】:
初次使用,请多多关照!
我在配置 Shiro 以使用 Guice 过滤 Vaadin 生成的页面时遇到了一点问题。
我在各种网站上查看过,包括 Apache Shiro 的指南等。问题是大多数网站都倾向于使用“旧”时尚方式,即使用 Shiro 1.1(不支持原生 Guice)。
所以这就是问题所在。我的页面没有通过 Shiro 过滤。我尝试了无数种不同的方法,包括使用 AOP 进行方法身份验证、在 web.xml 中手动设置过滤器。甚至设置一个 shiro.ini 文件(在任何情况下我都不想这样做)。
所以这里是我正在使用的东西的列表: - Shiro 1.2.0-快照 - Guice 3.0 - Vaadin 6.7.4
这是我的 web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>App</display-name>
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
<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>com.app.GuiceServletInjector</listener-class>
</listener>
</web-app>
这里是 Servlet 注入器:
public class GuiceServletInjector extends GuiceServletContextListener {
private ServletContext servletContext;
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
servletContext = servletContextEvent.getServletContext();
super.contextInitialized(servletContextEvent);
}
@Override
protected Injector getInjector() {
return Guice.createInjector(new GuiceServletModule(), new ShiroConfigurationModule(servletContext));
}
然后创建一个 ServletModule,将请求传递给 Vaadin 应用程序:
protected void configureServlets() {
bind(Application.class).to(VaadinMainWindow.class).in(ServletScopes.SESSION);
bind(BasicHttpAuthenticationFilter.class).in(Singleton.class);
filter("/*").through(BasicHttpAuthenticationFilter.class);
serve("/*", "*").with(VaadinApp.class);
}
同样在注入器阶段,请注意我创建了一个 ShiroConfigurationModule,它负责处理领域等:
public class ShiroConfigurationModule extends ShiroWebModule {
@Inject
public ShiroConfigurationModule(ServletContext servletContext) {
super(servletContext);
}
@Override
protected void configureShiroWeb() {
bindRealm().to(ShiroBaseRealm.class).in(Singleton.class);
bind(Realm.class).to(ShiroBaseRealm.class).in(Singleton.class);
processMethodInterceptors();
}
private void processMethodInterceptors() {
MethodInterceptor interceptor = new AopAllianceAnnotationsAuthorizingMethodInterceptor();
bindInterceptor(any(), annotatedWith(RequiresRoles.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresPermissions.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresAuthentication.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresUser.class), interceptor);
bindInterceptor(any(), annotatedWith(RequiresGuest.class), interceptor);
}
}
领域类为supports()返回'true',但对所有内容都返回'null',模拟用户不存在。
做错事或错过步骤的可能性非常高。有人可以解释一下我缺少什么,以便我至少可以获得基本的 HTTP 身份验证吗?
非常感谢! 莫。
【问题讨论】:
-
为什么
BasicHttpAuthenticationFilter使用过滤模式"/index.html"而不是"/*"? -
那是我正在做的一个测试。我尝试过“/**”和“/*”,但它们都没有产生任何结果。我将编辑我的帖子以反映这一点。谢谢:)
标签: java dependency-injection guice vaadin shiro