2

初次使用,请多指教!

我在配置 Shiro 以使用 Guice 过滤 Vaadin 生成的页面时遇到了一点问题。

我在各种网站上查看过,包括 Apache Shiro 的指南等。问题是大多数网站都倾向于使用“旧”时尚方式,即使用 Shiro 1.1(它没有原生 Guice 支持)。

所以这就是问题所在。我的页面没有通过 Shiro 过滤。我尝试了无数种不同的方法,包括使用 AOP 进行方法身份验证、在 web.xml 中手动设置过滤器。甚至设置一个 shiro.ini 文件(在任何情况下我都不想这样做)。

所以这里是我正在使用的东西列表: - Shiro 1.2.0-SNAPSHOT - 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);
    }
}

领域类为 support() 返回“真”,但对所有内容都返回“空”,模拟用户不存在。

做错事或错过一步的机会非常高。有人可以解释一下我缺少什么,以便我至少可以获得基本的 HTTP 身份验证吗?

非常感谢!莫。

4

2 回答 2

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 过滤器都不会被命中。

于 2015-07-01T04:02:22.017 回答
-1

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

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

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

祝你好运!

于 2012-01-25T07:43:01.097 回答