[Spring] csrf error

yoon·2024년 4월 11일

spring-boot

목록 보기
25/41
post-thumbnail

✅EL1007E: Property or field 'token' cannot be found on null

✔오류원인

csrf token값이 null이라서 생기는 오류

✔변경전

<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>

✔변경 후

<meta name="_csrf" th:content="${_csrf?.token}">
<meta name="_csrf_header" th:content="${_csrf?.headerName}">

token이 null일 경우만 적용하려면

<meta name="_csrf" th:content="${_csrf?.token}" th:if="${_csrf} ne null">
<meta name="_csrf_header" th:content="${_csrf?.headerName}" th:if="${_csrf} ne null">

✅TypeError: Cannot read properties of undefined (reading ‘toLowerCase’)

✔오류원인

csrf_token 값이 null로 설정되었기 때문이다.
값이 있을 때만 적용하도록 변경해준다.

✔변경전

var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");

            
$(document).ajaxSend(function(event, xhr, options) {
      xhr.setRequestHeader(header, token);
});
            

✔변경 후

var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");

if(token && header) {
   $(document).ajaxSend(function(event, xhr, options) {
      xhr.setRequestHeader(header, token);
     });
}
profile
하루하루 차근차근🌱

0개의 댓글