14

我在我的spring-boot應用中實現了Oauth2。在我的security-context.xml中,我有這些行 -春季安全 - 允許匿名訪問

<sec:intercept-url pattern="/trusted/**" access="isAnonymous()" /> 
<sec:intercept-url pattern="/**" access="isFullyAuthenticated()" /> 

我希望在/ trusted下的所有內容都可以在沒有身份驗證的情況下使用。但是,當我嘗試訪問/受信任的資源(這些是RESTful資源)時,仍會提示我進行身份驗證。

我錯過了別的嗎?

[編輯:]我正在運行這個應用程序與'提供'的tomcat實例。

+0

isAnonymous()意味着您配置了匿名用戶。我會做這樣的事情: 2015-08-14 21:45:28

+0

Stackoverflow將獎賞獎勵給某人,但我沒有得到一個工作答案。 – Raj

回答

5

你只需要更換信任攔截表達access屬性,它應該工作:

<sec:intercept-url pattern="/trusted/**" filters="none" /> 
<sec:intercept-url pattern="/**" access="isFullyAuthenticated()" /> 

雖然因爲春季安全3.1已棄用filters,你應該使用http標籤來達到同樣的效果:

<http pattern="/trusted/**" security="none"/> 

<http auto-config='true'> 
    <intercept-url pattern="/**" access="isFullyAuthenticated()" /> 
    <form-login login-page='/login.jsp'/> 
</http> 

You can read more about this here

+0

現在我得到org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在SecurityContext中找不到認證對象 – Raj

+1

聽起來就像您的認證設置有其他問題。 –

+0

我已經模仿https://github.com/skate056/spring-security-oauth2-google你知道谷歌誓言提供商和春季開機的其他工作示例嗎? – Raj

0

你的配置有誤。現在來看看發生了什麼,你告訴Spring安全機制允許匿名訪問/trusted/**下的所有內容,但是你可以再次告訴它限制所有匿名訪問權限在/**之下 - 這是應用程序中的每一條路徑,這顯然限制了訪問/trusted/**以及。

您需要更改配置弄成這個樣子:

<sec:intercept-url pattern="/trusted/**" access="isAnonymous()" /> 
<sec:intercept-url pattern="/secure/**" access="isFullyAuthenticated()" /> 

,它會工作。

+0

你的答案是有道理的,但後來我得到這個異常-2015-08-17 20:10:3​​0.760錯誤18078 --- [io-8080-exec-10] osboot.context.web.ErrorPageFilter:轉發到錯誤來自請求的頁面[/ monitor/123]由於異常[在SecurityContext中未找到認證對象] org.springframework.security.authentication.AuthenticationCredentialsNotFoundException:在SecurityContext中找不到認證對象 – Raj

1
<http> 
<intercept-url pattern="/trusted/**" access="ROLE_USER,ROLE_GUEST" /> 
<intercept-url pattern="/messagePost.htm*" access="ROLE_USER" /> 
<intercept-url pattern="/messageDelete.htm*" access="ROLE_ADMIN" /> 
<anonymous username="guest" granted-authority="ROLE_GUEST" /> 
<remember-me /> 
</http> 

<anonymous username="guest" granted-authority="ROLE_GUEST" />

你可以這樣定義ROLE_GUEST作用和提像上面的代碼做什麼。任何匿名成員都可以訪問下的url模式。ROLE_GUEST