简体   繁体   English

使用 Spring Security 的 CSRF 保护

[英]CSRF Protection using Spring Security

Is there a way to implement only CSRF protection using spring security without using rest of the features like authentication and authorization ?有没有办法只使用 Spring Security 实现 CSRF 保护而不使用其他功能,例如身份验证和授权?

I tried the following configuration but it turns off all the features of spring-security.我尝试了以下配置,但它关闭了 spring-security 的所有功能。 Wanted to see if there is a way to configure to leave the csrf features ON.想看看是否有一种方法可以配置以保留 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>

As you already noticed security="none" allows you to specify a subset of urls for which Spring Security is to be disabled completely, as in : 正如您已经注意到的, security="none"允许您指定将完全禁用Spring Security的一部分URL,如:

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

If you wish to keep Spring Security for a url, but want to disable authentication etc., use access="permitAll" instead: 如果您希望保留Spring Security的URL,但想禁用身份验证等,请使用access="permitAll"代替:

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

I see that you are using Spring Security 4, so csrf protection will be enabled by default. 我看到您正在使用Spring Security 4,因此默认情况下将启用csrf保护。 If you were using Spring Security 3, you would need to enable it yourself, as in: 如果您使用的是Spring Security 3,则需要自己启用它,如下所示:

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

EDIT I have updated my answer. 编辑我已经更新了我的答案。 It has been a while since I used xml config. 自从我使用xml config以来已经有一段时间了。 Maybe you should consider using Java config as well? 也许您也应该考虑使用Java配置?

I also had the same requirement as you , and I was able to achieve it by minimal configurations in spring-security.xml 我也有与您相同的要求,并且可以通过在spring-security.xml中进行最少的配置来实现此要求。

<authentication-manager />
<http create-session="never" use-expressions="true">
    <csrf />
    <http-basic />
</http>

Here the < authentication-manager /> declares that the spring security doesn't expect an authentication/authorization mechanism, and assumes that all requests are already authenticated. 在这里,<authentication-manager />声明spring security不期望使用身份验证/授权机制,并假定所有请求都已通过身份验证。

After this add the Spring Security Filter on your web.xml file. 之后,在您的web.xml文件中添加Spring Security Filter。 This ensures that all requests first pass through spring security mechanism before being handled by the application controller. 这样可以确保所有请求在通过应用程序控制器处理之前先通过spring安全机制。

<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>
</filter-mapping>

Then in your JSPs (preferably the Header JSP), include the Spring Security's Taglib to access and store the CSRF tokens in the meta tag. 然后在您的JSP(最好是Header JSP)中,包含Spring Security的Taglib,以将CSRF令牌访问和存储在meta标签中。

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<sec:csrfMetaTags />

<script type="text/javascript">
    var csrfHeader = $("meta[name='_csrf_header']").attr("content");
    var csrfToken = $("meta[name='_csrf']").attr("content");
</script>

After this include the CSRF Tokens in all your Ajax calls. 之后,在所有Ajax调用中都包含CSRF令牌。 If not included, you will get the 403 - Access Denied Error. 如果未包括在内,您将收到403-访问被拒绝错误。

For Example, if you are using jQuery for doing ajax calls, then you can configure it globally to include the CSRF tokens in the Request Header. 例如,如果您使用jQuery进行ajax调用,则可以对其进行全局配置以在请求标头中包含CSRF令牌。

$(document).ajaxSend(function(e, xhr, options) {
    xhr.setRequestHeader(csrfHeader, csrfToken);
});

JAR files required for this to work are: 为此需要的JAR文件是:

    spring-security-acl-5.0.7.RELEASE.jar
    spring-security-config-5.0.7.RELEASE.jar
    spring-security-core-5.0.7.RELEASE.jar
    spring-security-taglibs-5.0.7.RELEASE.jar
    spring-security-web-5.0.7.RELEASE.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM