Vue apollo Client 통신 header에 서버의 CSRF 토큰 전달 방법
(참고 서버는 스프링)
const csrfToken = document.querySelector('meta[name="_csrf"]').content
const csrfHeader = document.querySelector('meta[name="_csrf_header"]').content
const linkOption = () => {
const options = {
uri: '/graphql',
credentials: 'include',
}
return process.env.NODE_ENV === 'production' ? { ...options, headers: { [csrfHeader]: csrfToken } } : options
}
const httpLink = createHttpLink(linkOption())
httpLike option에 headers를 추가한다.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
</html>
thymeleaf 설정 추가, index.html 에서 meta tag 추가 후 build 시 에러 발생!
thymeleaf meta tag를 인식하지 못하는것 같다(?)
해결 방법으로는 webpack 설정을 통해 meta tag 추가
// html meta tag config
chainWebpack: config => {
config
.plugin('html')
.tap(args => {
args[0].meta = {
'_csrf': {'name': '_csrf', 'th:content': '${_csrf.token}'},
'_csrf_header': {'name': '_csrf_header', 'th:content': '${_csrf.headerName}'},
}
return args
})
},