2016-05-06 78 views
0

有沒有一種方法可以實現僅使用spring安全性的CSRF保護,而不使用身份驗證和授權等其他功能?使用Spring Security的CSRF保護

我嘗試了以下配置,但它關閉了spring-security的所有功能。想知道是否有一種方法可以讓csrf功能開啓。

<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-4.0.xsd"> 

    <http auto-config="true" security="none"/> 

    <authentication-manager> 
     <authentication-provider> 
      <user-service> 
       <user name="user" password="Password1" authorities="ROLE_USER"/> 
      </user-service> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 
+0

這個問題實際上並不符合本網站的準則,您需要問什麼具體問題。你已經搜索了一個結果嗎?如果是這樣,那麼你應該嘗試一些東西,並有具體的問題。 – JoeG

回答

0

正如你已經注意到security="none"允許您指定的URL的子集,其春季安全是完全被禁止,如:

<http pattern="/css/**" security="none"/> 
<http pattern="/login.jsp*" security="none"/> 

如果您想保留春季安全的URL ,但要禁用認證等,使用access="permitAll"代替:

<http> 
    <intercept-url pattern="/**" access="permitAll"/> 
</http> 

我看到你正在使用Spring Security的4,所以CSRF保護會默認啓用。如果你使用Spring安全3,您需要自己啓用該選項,如:

<http> 
    <intercept-url pattern="/**" access="permitAll"/> 
    <csrf/> 
</http> 

編輯 我已經更新我的答案。自從我使用xml config以來已經有一段時間了。也許你應該考慮使用Java配置?

+0

似乎不起作用。我得到以下異常: cvc-complex-type.3.2.2:屬性'access'不允許出現在元素'http'中。 我嘗試以下代替: <截距-URL圖案= 「/ **」 訪問= 「permitAll」/> 現在,我得到以下情況例外: org.springframework.beans.factory。 parsing.BeanDefinitionParsingException:配置問題:無法建立AuthenticationEntryPoint。 – user3768145