Spring Security 인증과 cookie의 교차 발신지 vs Authorization 헤더
REST API 인증에 Spring Security를 사용하고 있습니다.인증은 정상적으로 동작하며 Spring Security는 다음 토큰을 반환한다.Set-Cookie
.그rememberMe
기능이 켜져 있고 쿠키에서도 동일한 기능이 반환됩니다.
이후 요청을 할 때 사용자 이름/비밀번호를 전송하지 않으면 인증이 실패합니다.Authorization
우편배달부에서 테스트하면 동작합니다.그게 더해지는구나.JSESSIONID
cookie에 추가해 주세요.
쿠키가 as이기 때문에Http-Only
cookie, 내 JavaScript 함수로 읽을 수 없을 것 같아.
그럼 사용자 인증은 어떻게 해야 하나요?서버가 쿠키가 반환되면set-cookie
브라우저는 각 요구에 대해 자동으로 추가합니다.
로그인 클라이언트 요청:
context.$http.get(url, {
headers:
{
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'Authorization': auth
}
}
요청 헤더:
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-GB,en;q=0.8,fr;q=0.6,en-US;q=0.4
Access-Control-Request-Headers:access-control-allow-methods,access-control-allow-origin,authorization
Access-Control-Request-Method:GET
Connection:keep-alive
DNT:1
Host:localhost:9000
Origin:http://localhost:8080
Referer:http://localhost:8080/login?redirect=%2F
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
응답 헤더:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8080
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:13
Content-Type:application/json;charset=UTF-8
Date:Fri, 18 Aug 2017 05:40:55 GMT
Expires:0
Pragma:no-cache
Set-Cookie:remember-me=dTVTMi94dnVkQzJtYXRFYmJkR1VKZz09OjA1T1F4RXVOV0RiTEx4VFdUSVByeGc9PQ; Max-Age=86400; Expires=Sat, 19-Aug-2017 05:40:55 GMT; Path=/; Secure; HttpOnly
Set-Cookie:JSESSIONID=4A856E0216191E8601100B600E4A227B; Path=/; HttpOnly
Vary:Origin
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
이후 데이터 가져오기 요청:
this.$http.get(url).then(function (res) {
this.items = res.data
})
클라이언트 요청:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-GB,en;q=0.8,fr;q=0.6,en-US;q=0.4
Connection:keep-alive
DNT:1
Host:localhost:9000
Origin:http://localhost:8080
Referer:http://localhost:8080/Languages
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
서버 응답:
HTTP/1.1 401
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Access-Control-Allow-Origin: http://localhost:8080
Vary: Origin
Access-Control-Allow-Credentials: true
WWW-Authenticate: Basic realm="Realm"
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 18 Aug 2017 05:43:26 GMT
{timestamp: 1503038407254, status: 401, error: "Unauthorized",…}
error :
"Unauthorized"
message:"Full authentication is required to access this resource"
path:"/languages"
status:401
timestamp:1503038407254
추가 중withCredentials : true
:
GET /languages HTTP/1.1
Host: 192.168.16.142:12345
Connection: keep-alive
Access-Control-Allow-Origin: *
Accept: application/json, text/plain, */*
Origin: http://localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
Access-Control-Allow-Methods: GET,PUT,POST,DELETE
DNT: 1
Referer: http://localhost:8080/languages
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en;q=0.8,fr;q=0.6,en-US;q=0.4
응답:
Access-Control-Allow-Origin:*
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Type:application/json;charset=UTF-8
Date:Mon, 21 Aug 2017 06:26:24 GMT
Expires:0
Pragma:no-cache
Transfer-Encoding:chunked
Vary:Origin
WWW-Authenticate:Basic realm="Realm"
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
참고로 저는 프런트엔드에 vuejs를 사용합니다.
응답/요구 플로우를 주의 깊게 살펴본 후 포트 8080에서 포트 9000으로 요구를 발신하고 있으며, 이 요구를 통해 동일한 발신기지 정책(CORS) 규칙이 트리거됩니다.기본적으로는 다음 링크에 기재되어 있는 credential 또는 cookie는 전송되지 않습니다.자격 정보/쿠키 전파를 활성화하려면 AJAX 호출에서 "withCredentials" 플래그를 설정해야 합니다.
게다가 당신은 vuej를 사용하고 있는 것 같습니다.자격 증명을 사용하려면 아래 설정을 사용해야 합니다.
Vue.http.options.credentials = true;
언급URL : https://stackoverflow.com/questions/45740207/spring-security-authentication-cross-origin-with-cookies-vs-authorization-header
'programing' 카테고리의 다른 글
이전 버전의 휴지 상태(~2009)를 사용하여 행을 카운트하려면 어떻게 해야 합니까? (0) | 2022.08.11 |
---|---|
특정 인터페이스에서 cURL을 사용하는 방법 (0) | 2022.08.11 |
페이지 새로 고침 후 v-treeview 상태를 유지하는 방법 (0) | 2022.08.11 |
getter에서 Vue 체인 여러 필터 (0) | 2022.08.11 |
VueJ: 렌더링을 또는 (0) | 2022.08.11 |