【问题标题】:Shiro 1.2 not working with Guice (and Vaadin)Shiro 1.2 不适用于 Guice(和 Vaadin)
【发布时间】: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


【解决方案1】:

博客链接自然已过期,现在重定向到的站点不包含任何该博客文章的踪迹。

可在此处找到该文章的副本。

http://web.archive.org/web/20120413052117/http://www.mofirouz.com/wordpress/2012/01/guice-shiro-1-2-and-vaadingwt/

答案的关键:如果您使用的是 guice,那么您必须包含

filter("/*").through(GuiceShiroFilter.class)

在您的 ServletModule 中,否则任何相关的 shiro 过滤器都不会被命中。

【讨论】:

    【解决方案2】:

    就这样,在对 Shiro 进行了大量测试和折腾(以及最终使用 1.2 版本)之后,我开始工作了。

    我已经在我的网站上写了一个详细的答案(主要是因为它更容易写!)。看看:

    http://www.mofirouz.com/wordpress/2012/01/guice-shiro-1-2-and-vaadingwt/

    祝大家好运!

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这在理论上可以回答问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 2011-11-26
    • 2016-03-30
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多