2012-01-21 37 views
2

第一次使用,請親切!Shiro 1.2不與Guice(和Vaadin)合作

配置Shiro使用Guice過濾Vaadin生成的頁面時,我遇到了一些問題。

我在網上查看過各種網站,包括Apache Shiro的指南等等。問題是,大多數網站傾向於使用'舊'時尚方式,即使用Shiro 1.1(它沒有原生Guice支持)。

所以這裏是問題所在。我的頁面沒有通過Shiro過濾。我已經嘗試過數十萬種不同的東西,包括使用AOP進行方法驗證,在web.xml中手動設置過濾器。甚至建立一個shiro.ini文件(在任何情況下我都不想這麼做)。

所以這裏的東西我使用的列表: - 四郎1.2.0-SNAPSHOT - 吉斯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)); 
    } 

哪然後創建servlet模塊,其將請求傳遞到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); 
    } 
} 

境界類返回「真」的支持()但對於所有內容都返回「null」,模擬用戶不存在。

做錯事或錯過一個步驟的機率非常高。有人可以請照顧解釋我錯過了什麼,所以我至少可以得到一個基本的HTTP認證?

非常感謝! Mo.

+0

爲什麼你爲'BasicHttpAuthenticationFilter'使用''/index.html''而不是''/ *「'過濾模式? – eiden

+0

這是我正在做的一項測試。我試圖做「/ **」和「/ *」,並沒有產生任何結果。我會編輯我的帖子以反映這一點。謝謝:) –

回答

-1

經過大量的測試以及與Shiro(以及最終使用1.2版本)的混淆之後,我得到了我的工作。

我已經在我的網站上寫了一個詳細的答案(主要是因爲它更容易編寫!)。看一看:

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

運氣好誰就在那裏!

+1

歡迎來到堆棧溢出!雖然這可能在理論上回答這個問題,[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包括答案的重要部分,並提供供參考的鏈接。 – oers