티스토리 뷰
728x90
반응형
Spring boot 를 사용하면서 security 를 사용 시 ajax post 통신을 할 때 403 에러가 발생한다.
해결방법으로는 ajax send 전에 header 값에 csrf 토큰 값을 넣어주면 된다.
페이지의 header 부분에 meta 값을 넣어준다.
<meta name="_csrf" th:content="${_csrf.token}">
<meta name="_csrf_header" th:content="${_csrf.headerName}">
jQuery ajax 요청시 csrf 값을 요청 전에 헤더에 담아서 보내면 된다.
$.ajax({
url: opt.url,
type: opt.type,
contentType: contentType,
dataType: "json",
data: opt.param,
beforeSend: function (jqXHR, settings) {
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
jqXHR.setRequestHeader(header, token);
}
}).done(function (rData, textStatus, jqXHR) {
...
}).fail(function (rData, textStatus, errorThrown) {
...
}).always(function (rData, textStatus, jqXHR) {
...
});
출처 및 참고..
https://sarc.io/index.php/cloud/2096-spring-boot-security-ajax-post-403
[Spring Boot] security를 사용할 때 ajax post 403에러 발생
[{"id":"10","listid":"1","parentid":"0","videosource":"youtube","videoid":"KiwjxNKXfxY","imageurl":"https:\/\/i.ytimg.com\/vi\/KiwjxNKXfxY\/default.jpg,120,90;https:\/\/i.ytimg.com\/vi\/KiwjxNKXfxY\/mqdefault.jpg,320,180;https:\/\/i.ytimg.com\/vi\/KiwjxNKX
sarc.io
728x90
반응형
댓글
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Spring Security
- LocalDateTime
- mybatis config
- k8s
- JavaScript
- Linux
- jQuery
- localtime
- maven
- elasticsearch
- springboot
- mybatis
- 북리뷰
- window
- rocky
- LocalDate
- oracle
- config-location
- 베리 심플
- 오라클
- Java
- docker
- input
- Bash tab
- intellij
- Kotlin
- Spring
- claude
- svn
- Mac
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함
반응형