Spring Security绕过特定URL的CSRF验证

时间:2019-06-22 07:43:12

标签: java spring spring-security csrf

我有一个基于spring security + MVC的项目。

项目的Web方面已经由以前的开发人员开发。我的任务是公开一些API 服务提供商将在其上进行回调。

要对此进行存档,我已完成以下代码。

@RestController
public class LeegalityController {

    private static final Logger logger = LoggerFactory.getLogger(LeegalityController.class);

    @RequestMapping(value = "webhook/{user}/{id}", method = RequestMethod.POST)
    public ResponseEntity<String> webhook(@PathVariable("user") int user, @PathVariable("id") String id,
            @RequestBody LeegalityReqResp reqResp, HttpServletRequest request) {
        try {
            logger.info("webhook()== user[" + user + "] == id[" + id + "]");
            logger.info("webhook()== LeegalityReqResp-->" + reqResp.toString());

        } catch (Exception e) {
            logger.error("webhook() Error==[" + e.getMessage() + "]", e);
        }

        return new ResponseEntity<String>("success", HttpStatus.OK);
    }

}

但是通过邮递员测试API时,出现以下错误

enter image description here

我尝试了如下对安全性配置xml的更改

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">


    <http pattern="*/webhook/**" security="none" create-session="stateless" />

    <http auto-config="true">
        <intercept-url pattern="/"
            access="hasAnyRole('ROLE_ADMIN','ROLE_ONE','ROLE_TWO')" />

        <!-- <intercept-url pattern="/webhook/**" method="POST" access="permitAll" /> -->


        <intercept-url pattern="/admin"
            access="hasRole('ROLE_ADMIN')" />

        <intercept-url pattern="/otp"
            access="hasAnyRole('ROLE_ONE','ROLE_TWO')" />


        <form-login login-page="/ProjectName-Home"
            default-target-url="/" authentication-failure-url="/login?error"
            username-parameter="username" password-parameter="password" />

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

    <authentication-manager
        alias="authenticationManager">
        <authentication-provider
            ref="userAuthenticationProvider">
        </authentication-provider>

    </authentication-manager>

    <beans:bean id="userAuthenticationProvider"
        class="com.java.ProjectName.service.user.UserAuthenticationProvider"></beans:bean>

    <beans:bean id="encoder"
        class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <beans:constructor-arg name="strength"
            value="11" />
    </beans:bean>
</beans:beans>

我该怎么做才能确保此/webhook/*被CSRF验证排除。感谢您的帮助,谢谢。

我的spring版本是4.2.0.RELEASE,spring安全版本是4.0.2.RELEASE。

1 个答案:

答案 0 :(得分:1)

要从CSRF保护中排除特定URL,可以使用<csrf request-matcher-ref="csrfMatcher">csrfMatcherRequestMatcher,它定义哪个URL请求将具有CSRF保护。

Docs的示例仅排除了特定的URL,同时仍保持其他默认设置不变:

<http ...>
    <csrf request-matcher-ref="csrfMatcher"/>
    ...
</http>


<beans:bean id="csrfMatcher" class="org.springframework.security.web.util.matcher.AndRequestMatcher">
    <beans:constructor-arg value="#{T(org.springframework.security.web.csrf.CsrfFilter).DEFAULT_CSRF_MATCHER}"/>
    <beans:constructor-arg>
        <beans:bean class="org.springframework.security.web.util.matcher.NegatedRequestMatcher">
          <beans:bean class="org.springframework.security.web.util.matcher.AntPathRequestMatcher">
            <beans:constructor-arg value="/webhook/**"/>
          </beans:bean>
        </beans:bean>
    </beans:constructor-arg>
</beans:bean>