6

我試圖讓Tuckey UrlRewriteFilter爲我的webapp整理網址。我得到的一個問題是,當spring-security發現匿名用戶試圖訪問受保護資源時,它會重定向到一個包含servlet路徑的URL。重寫彈簧安全重定向URL

我想,通過例如:

> GET http://localhost:8080/my-context/protected-resource 
< Location: http://localhost:8080/my-context/login 

我目前得到的是:

> GET http://localhost:8080/my-context/protected-resource 
< Location: http://localhost:8080/my-context/-/login 

相關文件,到目前爲止,我發現:

DefaultRedirectStrategy,這有問題的實際重定向:http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/DefaultRedirectStrategy.html。它有一個contextRelative屬性,這個屬性很誘人,但我不認爲會削減它,如果我甚至可以找到配置它的方法。

一篇博客文章,幫助讓我到這地步:http://nonrepeatable.blogspot.com/2009/11/using-spring-security-with-tuckey.html

我想知道的是:

  1. 燦/我應該說服Tuckey重寫Location頭。 <出站規則>在這裏似乎沒有幫助。
  2. 可以/我應該以某種方式調整SS配置以發出重寫的URL。我認爲這不是很整潔,因爲如果重寫被禁用,它會中斷。

web.xml看起來像

<filter> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> 
    <init-param> 
     <param-name>LogLevel</param-name> 
     <param-value>log4j</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>UrlRewriteFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>REQUEST</dispatcher> 
</filter-mapping> 

<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>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>INCLUDE</dispatcher> 
    <dispatcher>ERROR</dispatcher> 
</filter-mapping> 

<servlet> 
    <servlet-name>my-servlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>psms</servlet-name> 
    <url-pattern>/-/*</url-pattern> 
</servlet-mapping> 

urlrewrite.xml樣子:

<urlrewrite> 
    <rule> 
     <from>^/(.*)$</from> 
     <to>/-/$1</to> 
    </rule> 
</urlrewrite> 

applicationContent-security.xml樣子:

<http auto-config="true"> 
    <!-- allow GET requests to /login without authentication --> 
    <intercept-url pattern="/-/login" method="GET" filters="none"/> 

    <intercept-url pattern="/-/admin/**" access="ROLE_ADMIN"/> 
    <intercept-url pattern="/-/**" access="ROLE_USER"/> 

    <form-login login-page="/-/login" 
       login-processing-url="/-/login.do" 
       authentication-failure-url="/-/login?login_error" 
       default-target-url="/-/index" 
       always-use-default-target="true"/> 

    <logout logout-url="/-/logout" 
      logout-success-url="/-/login"/> 

    <access-denied-handler error-page="/-/access-denied"/> 
</http> 
+0

並將登錄頁面屬性設置爲/ login? – rodrigoap 2009-11-20 15:33:37

回答

0

心中已經沒用過Tuckey,但經過快看在文件中entation我會嘗試添加一個規則來進行登錄的情況下:

<urlrewrite> 
    <rule> 
     <from>^/my-context/login$</from> 
     <to>/my-context/login</to> 
    </rule> 
    <rule> 
     <from>^/(.*)$</from> 
     <to>/-/$1</to> 
    </rule> 
</urlrewrite> 

編輯
好了,和這樣的事情:

<urlrewrite> 
    <rule> 
     <from>^/-/login$</from> 
     <to>/login</to> 
    </rule> 
    <rule> 
     <from>^/(.*)$</from> 
     <to>/-/$1</to> 
    </rule> 
</urlrewrite> 
+2

問題不在於入站請求不會被重寫,而不是出站位置標頭作爲重定向的一部分不會被重寫。 我從那以後就研究出一個帶有完整協議,主機,端口,上下文等的出站規則將會捕獲位置標題,但這也不是很好。 – ptomli 2009-11-20 06:54:53

2

我研究過這個問題,去年我們的項目,並在那個時候問題在於Tucky沒有與response.encodeRedirectUrl()合作來重寫重定向URL。我聯繫了他們,但我沒有跟進。

我的解決方案是讓混亂的URL返回到客戶端,但隨後使用Tucky重定向規則(第二個重定向)清理它。

所以再添你的醜陋的網址從安全重定向規則相匹配,併發出自己的重定向到URL清潔:

<rule> 
    <from>^/whatever/ugly.*$</from> 
    <to type="redirect">/login</to> 
</rule> 

是的,它涉及到兩個重定向,但客戶端將永遠不會看到它..這可能是重點。

1

春季安全與絕對URL做重定向像http://example.org/-/login

嘗試使用出站規則,而不^ start of string標記以匹配彈簧產生的絕對URL。

<outbound-rule> 
    <from>/-/login(.*)$</from> 
    <to>/login$1</to> 
</outbound-rule>  
1

我遇到了同樣的問題,但它在Tuckey的3.2.0版中似乎修復了。 即響應.encodeRedirectUrl()現在由出發規則執行發生的Tuckeys UrlRewriteWrappedResponse包裝。