Invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN'

前端 未结 11 2476
你的背包
你的背包 2020-12-07 15:38

After configuring Spring Security 3.2, _csrf.token is not bound to a request or a session object.

This is the spring security config:

&l         


        
相关标签:
11条回答
  • 2020-12-07 16:00

    I used to have the same problem.

    Your config use security="none" so cannot generate _csrf:

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

    you can set access="IS_AUTHENTICATED_ANONYMOUSLY" for page /login.jsp replace above config:

    <http>
        <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/**" access="ROLE_USER"/>
        <form-login login-page="/login.jsp"
                authentication-failure-url="/login.jsp?error=1"
                default-target-url="/index.jsp"/>
        <logout/>
        <csrf />
    </http>
    
    0 讨论(0)
  • 2020-12-07 16:02

    Shouldn't you add to the login form?;

    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> 
    

    As stated in the here in the Spring security documentation

    0 讨论(0)
  • 2020-12-07 16:08

    Please see my working sample application on Github and compare with your set up.

    0 讨论(0)
  • 2020-12-07 16:12

    In your controller add the following:

    @RequestParam(value = "_csrf", required = false) String csrf
    

    And on jsp page add

    <form:form modelAttribute="someName" action="someURI?${_csrf.parameterName}=${_csrf.token}
    
    0 讨论(0)
  • 2020-12-07 16:13

    Neither one of the solutions worked form me. The only one that worked for me in Spring form is:

    action="./upload?${_csrf.parameterName}=${_csrf.token}"

    REPLACED WITH:

    action="./upload?_csrf=${_csrf.token}"

    (Spring 5 with enabled csrf in java configuration)

    0 讨论(0)
提交回复
热议问题