programing

요청 매개 변수 '_csrf' 또는 헤더 'X-CSRF-TOKEN'에 잘못된 CSRF 토큰 'null'이 있습니다.

i4 2023. 3. 27. 21:00
반응형

요청 매개 변수 '_csrf' 또는 헤더 'X-CSRF-TOKEN'에 잘못된 CSRF 토큰 'null'이 있습니다.

Spring Security 3.2 설정 후_csrf.token는 요구 또는 세션오브젝트에 바인드 되지 않습니다.

스프링 보안 설정은 다음과 같습니다.

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

<http>
    <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>

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

login.jsp 파일

<form name="f" action="${contextPath}/j_spring_security_check" method="post" >
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
    <button id="ingresarButton"
            name="submit"
            type="submit"
            class="right"
            style="margin-right: 10px;">Ingresar</button>
    <span>
        <label for="usuario">Usuario :</label>
        <input type="text" name="j_username" id="u" class="" value=''/>
    </span>
    <span>
        <label for="clave">Contrase&ntilde;a :</label>

        <input type="password"
               name="j_password"
               id="p"
               class=""
               onfocus="vc_psfocus = 1;"
               value="">
    </span>
</form>

다음 html을 렌더링합니다.

<input type="hidden" name="" value="" />

결과는 403 HTTP 상태가 됩니다.

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

업데이트 디버깅 후 요청 객체는 DeleginatingFilterProxy에서 정상적으로 출력되지만 CoyoteAdapter의 행 469에서는 request.recycle()을 실행하여 모든 속성을 지웁니다.

JDK 1.7을 사용하여 Tomcat 6.0.36, 7.0.50에서 테스트합니다.

CSRF와 연동되는 Spring Security 3.2와의 애플리케이션 샘플 전쟁 방향을 누군가가 지시하는 것이 아니라 이 동작을 이해할 수 없습니다.

Spring 응용 프로그램에서 CSRF(Cross Site Request Formature) 보호가 활성화되어 있는 것 같습니다.실제로는 디폴트로 유효하게 되어 있습니다.

봄철에 따라서.IO:

CSRF 보호는 언제 사용해야 합니까?일반 사용자가 브라우저에서 처리할 수 있는 모든 요구에 CSRF 보호를 사용할 것을 권장합니다.브라우저 이외의 클라이언트에서 사용되는 서비스만 작성하는 경우 CSRF 보호를 디세블로 할 수 있습니다.

비활성화하려면:

@Configuration
public class RestSecurityConfig extends WebSecurityConfigurerAdapter {
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable();
  }
}

CSRF 보호를 유효하게 하는 경우는, 폼에 다음의 항목을 포함할 필요가 있습니다.csrftoken. 다음과 같이 할 수 있습니다.

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

CSRF 토큰을 폼의 액션에 포함할 수도 있습니다.

<form action="./upload?${_csrf.parameterName}=${_csrf.token}" method="post" enctype="multipart/form-data">

로그인 양식에 추가해야 하지 않나요?

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

스프링 보안 매뉴얼에 기재되어 있는 바와 같이

신청하시는 경우security="none"CSRF 토큰은 생성되지 않습니다.페이지가 보안 필터를 통과하지 않습니다.Anonymous 역할을 사용합니다.

자세한 것은 말하지 않았지만, 나에게는 효과가 있다.

 <http auto-config="true" use-expressions="true">
   <intercept-url pattern="/login.jsp" access="hasRole('ANONYMOUS')" />
   <!-- you configuration -->
   </http>

변경을 시도합니다.<csrf />다음과 같이 입력합니다.<csrf disabled="true"/>csfr을 디세블로 합니다.

티멜리프를 사용하여 다음을 추가할 수 있습니다.

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

CSRF를 디세블로 하는 스프링 매뉴얼:https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#csrf-configure

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

   @Override
   protected void configure(HttpSecurity http) throws Exception {
      http.csrf().disable();
   }
}

나도 예전에 같은 문제가 있었어.

사용자 구성에서 security="none"을 사용하므로 _securityf:를 생성할 수 없습니다.

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

액세스 설정 가능="/login.jsp 페이지의 IS_AUTHENTICATED_ANONYMONY"는 위의 설정을 대체합니다.

<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>

나는 csrf가 봄철 형태로만 작동한다고 생각한다.

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

로로 로 form:form이치노

Github에 대한 나의 작업 샘플 어플리케이션을 보고 당신의 설정과 비교해 보세요.

어느 해결책도 나에게는 효과가 없었다.Spring 폼에서 나에게 효과가 있었던 것은 다음과 같습니다.

action="/filename?${_csrf.parameterName}=${_csrf.토큰}"

대체 순서:

action="/filename?_syslogf=${_syslogf.토큰}"

(자바 구성으로 CSRF가 유효하게 되어 있는 스프링 5)

컨트롤러에서 다음을 추가합니다.

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

그리고 jsp 페이지에 추가

<form:form modelAttribute="someName" action="someURI?${_csrf.parameterName}=${_csrf.token}

언급URL : https://stackoverflow.com/questions/21128058/invalid-csrf-token-null-was-found-on-the-request-parameter-csrf-or-header

반응형