2012-02-07 71 views
1

Servlet 2.4+ API允許我們使用<filter-mapping>標記中的<dispatcher>標記,其中FORWARD等值可攔截內部轉發給其他資源的請求。對於一個servlet轉發到另一個servlet,彈簧安全限制工作正常。JSF - Spring Security Integration問題

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>REQUEST</dispatcher> 
</filter-mapping>  

問題: 安全過濾器似乎不與JSF操作攔截內部向前

JSF似乎「前進」的請求發送到目標視圖(頁),同時使用JSF行動(導航案例)。這會導致URL在頁面的實際URL後面一步。

這樣做的副作用是彈簧安全性約束(與URL關聯)直到下一個操作纔會生效。

例子: 當前頁面的URL:http://host/myapp/page1.xhtml (page1.xhtml具有導航到第二頁是受保護的動作)

在提交時,該請求被提交給這使得服務器page2.xhtml但該網址仍然爲http://host/myapp/page1.xhtml。 Spring Security沒有攔截和保護page2.xhtml

這可以通過指定以下來克服:

<navigation-case> 
    <from-outcome>page2</from-outcome> 
    <to-view-id>/page2.xhtml</to-view-id> 
    <redirect/> <!--REDIRECT, INSTEAD OF FORWARD--> 
</navigation-case> 

重定向是不是我們想要實現這一目標的方式。有沒有更好的方式讓Spring Security與JSF一起工作?

編輯:(彈簧配置XML培訓相關片段)

<http use-expressions="true" once-per-request="false"> 
    <intercept-url pattern="/index.xhtml" access="permitAll" /> 
    <intercept-url pattern="/page1.xhtml" access="isAuthenticated()" /> 
    <intercept-url pattern="/page2.xhtml" access="hasRole('supervisor')" /> 
    <intercept-url pattern="/page3.xhtml" access="hasRole('teller')" /> 
    <form-login login-page="/login.html" default-target-url="/page1.xhtml"/> 
</http> 

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="rod" password="rod" authorities="supervisor, user" /> 
      <user name="dianne" password="dianne" authorities="teller, user" /> 
      <user name="scott" password="scott" authorities="supervisor" /> 
      <user name="peter" password="peter" authorities="user" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 
+0

你能不能也發表您的applicationContext.xml – Ravi 2012-02-07 15:48:08

+0

@Ravi編輯的問題與來自appcontext.xml的內容任何想法? – 2012-02-08 04:45:43

回答

1

horse's mouth(Oracle文檔)

如果導航情況下不使用重定向元件,新頁面將呈現爲對當前請求的響應,這意味着瀏覽器地址字段中的URL不會更改,並且它將包含地址s的前一頁。

這似乎轉化爲在JSF生命週期期間沒有發生向下一頁的「向前」......所以Spring Security永遠不會處理這個問題。

0

默認情況下,FilterSecurityInterceptor將只執行一次每個請求,並且不會執行安全重新檢查,除非在url中有更改,但是使用JSP/JSF轉發頁面會呈現爲對當前請求的響應,並且瀏覽器中的網址包含上一頁的地址。因此,只需在applicationContext的http元素中設置once-per-request屬性爲false,從而強制安全重新檢查。

<http auto-config="true" use-expressions="true" once-per-request="false"> 

並在您的網站的springSecurityFilterChain過濾器映射中爲轉發添加一個調度程序。XML

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>REQUEST</dispatcher> 
</filter-mapping> 

更多info

另外,您還可以通過附加的參數使頁面重定向面臨重定向=真正的結果是這樣的:

<h:form> 
    <h:commandButton action="page1?faces-redirect=true" value="Page1" /> 
</h:form> 

但作爲BalusC表示,其而不是使用POST進行頁面到頁面導航的良好做法。總是做得到使用

<h:link> or <h:button> 

另見: