2011-03-25 111 views
2

我嘗試使用spring安全性爲我的應用程序實現安全性。 我攔截使用intercept-url頁面,例如:Spring Security intercept-url不匹配通配符

<http auto-config='true'> 
    <intercept-url pattern="/logList*" access="ROLE_ADMIN" /> 
    <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" /> 
    <logout /> 
    <remember-me/> 
</http> 

在我第一次嘗試使用訪問日誌匿名用戶網址:本地主機/項目名稱/ logList 和頁面自動跳轉到登錄頁面

但當我嘗試訪問日誌頁使用url localhost/projectname/logList/匿名用戶可以訪問日誌頁

爲什麼它可以發生在模式/logList*是正確的?

+0

你_sure_你訪問URL的第二次,你不已經有一個驗證會話?沒有其他解釋爲什麼你會在第一次嘗試點擊時重定向到登錄頁面,但不是第二次。你能澄清你正在採取的確切步驟嗎? – 2011-03-25 19:02:17

回答

2

默認情況下,使用AntPathRequestMatcher。如果你添加另一種模式

<intercept-url pattern="/logList/*" access="ROLE_ADMIN" />那麼它會工作。

下面是測試(注:在RegexRequestMatcher,同樣的模式既適用於/ logList /和/ logList):

@Test 
public void antTest1() throws Exception { 

    AntPathRequestMatcher pathMatcher = new AntPathRequestMatcher("/loglist*"); 
    MockHttpServletRequest mockRequest = new MockHttpServletRequest(); 
    mockRequest.setScheme("http"); 
    mockRequest.setPathInfo("/logList"); 
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true)); 
} 

@Test 
public void antTest2() throws Exception { 


    AntPathRequestMatcher pathMatcher = new AntPathRequestMatcher("/loglist/*"); 
    MockHttpServletRequest mockRequest = new MockHttpServletRequest(); 
    mockRequest.setScheme("http"); 
    mockRequest.setPathInfo("/logList/"); 
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true)); 
} 

@Test 
public void regexTest3() throws Exception { 

    RegexRequestMatcher pathMatcher = new RegexRequestMatcher("/logList.*", "GET"); 
    MockHttpServletRequest mockRequest = new MockHttpServletRequest(); 
    mockRequest.setScheme("http"); 
    mockRequest.setMethod("GET"); 
    mockRequest.setPathInfo("/logList/"); 
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true)); 
} 

@Test 
public void regexTest4() throws Exception { 

    RegexRequestMatcher pathMatcher = new RegexRequestMatcher("/logList.*", "GET"); 
    MockHttpServletRequest mockRequest = new MockHttpServletRequest(); 
    mockRequest.setScheme("http"); 
    mockRequest.setMethod("GET"); 
    mockRequest.setPathInfo("/logList"); 
    Assert.assertThat(pathMatcher.matches(mockRequest), is(true)); 
} 

要使用RegexRequestMatcher,添加屬性的要求,匹配'爲http並將其值設置爲「正則表達式」:

<http auto-config="true" request-matcher="regex">

+0

只需注意「請求匹配器」需要Spring Sec 3.1+。低於這個版本的等價物是「路徑類型」。 – 2011-03-25 20:12:19

+0

我使用spring-sec 3.0使用path-type =「regex」,但是發生了NullPointerExecption錯誤。 我還在下載spring sec 3.1+ – 2011-03-26 08:29:48

0

請問pattern="/logList/**"是否有所作爲?

+0

我嘗試使用**模式,但沒有區別 – 2011-03-25 11:17:14