웹 앱 로그아웃 요청 CORS 문제

여러분 안녕하세요, Discourse에 감사합니다. 이 시점까지는 설정이 꽤 쉬웠습니다.

웹 앱에서는 정상적으로 작동하는 SSO 설정이 있는데, admin/users/:id/logout 엔드포인트를 트리거하면 웹 앱에서는 작동하지 않습니다.

어떤 이유인지 모르겠지만 cURL로는 정상적으로 작동합니다:

 curl 'http://localhost/admin/users/2/log_out' \
  -X 'POST' \
  -H 'Api-Key: <>' \
  -H 'Content-Type: multipart/form-data;' -v

*   Trying 127.0.0.1:80...
* Connected to localho.st (127.0.0.1) port 80 (#0)
> POST /admin/users/2/log_out HTTP/1.1
> Host: localho.st
> User-Agent: curl/7.77.0
> Accept: */*
> Api-Key: <>
> Content-Type: multipart/form-data;
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Status: 200 OK
< Cache-Control: no-cache, no-store
< Access-Control-Allow-Origin: http://localhost:3000
< Vary: Accept
< Referrer-Policy: strict-origin-when-cross-origin
< X-Permitted-Cross-Domain-Policies: none
< X-XSS-Protection: 1; mode=block
< X-Request-Id: 26dfcbf6-497c-45db-8e3d-17aef9ba812c
< X-Discourse-Route: users/log_out
< Access-Control-Allow-Headers: Content-Type, Cache-Control, X-Requested-With, X-CSRF-Token, Discourse-Present, User-Api-Key, User-Api-Client-Id, Authorization
< Access-Control-Allow-Credentials: true
< Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS, DELETE
< X-Download-Options: noopen
< X-Runtime: 0.190266
< X-Frame-Options: SAMEORIGIN
< X-Content-Type-Options: nosniff
< X-Discourse-Username: emurillo
< Date: Tue, 30 Nov 2021 21:12:58 GMT
< X-Powered-By: Phusion Passenger(R) 6.0.10
< Server: nginx/1.20.1 + Phusion Passenger(R) 6.0.10
<
* Connection #0 to host localho.st left intact
{"success":"OK"}

그리고 서버 로그는 다음과 같습니다:

Started POST "/admin/users/2/log_out" for 172.18.0.1 at 2021-11-30 21:12:58 +0000
Processing by Admin::UsersController#log_out as */*
  Parameters: {"user_id"=>"2"}
Can't verify CSRF token authenticity.
Completed 200 OK in 75ms (Views: 0.9ms | ActiveRecord: 0.0ms | Allocations: 2350)

그런데 웹 앱에서 시도하면:

axios.post('http://localhost/admin/users/2/log_out', {}, {
  headers: {
    'User-Api-Key': '<>',
    'Content-Type': 'multipart/form-data'
  }
});

서버 로그에는 이렇게 표시됩니다:

Started POST "/admin/users/2/log_out" for 172.18.0.1 at 2021-11-30 21:08:26 +0000
ActionController::RoutingError (No route matches [POST] "/admin/users/2/log_out")

추가 증거:


xxxxxxxxxxx

xxxxxxxxxxx

"웹 앱에서"라는 표현이 정확히 무슨 뜻인가요? 프론트엔드 JavaScript에서 이 API 엔드포인트를 호출하려는 건가요? 그렇게 하면 관리자 API 키가 노출됩니다.

네, 처음에는 프론트엔드에서 먼저 확인해보려 했는데, 로그아웃을 처리하기 위해 서버에 엔드포인트를 설정하라는 조언을 주셨다면 (그리고 더 짧은 우회 경로가 없다면) 조언에 따르겠습니다.