[英]gem "rack-cors" not working and request from angular client to rails api throwing cors error
我正在努力讓 gem “rack-cors” 在本地工作。 我使用 gem 'devise_token_auth' 和 angular 7 客戶端設置了裸軌,使用 'angular-token' 進行身份驗證。 但是來自 angular 的每個請求都會拋出 cors 錯誤。 我已經按照文檔配置了導軌和“機架 cors”,但它不起作用。 這是我在application.rb
的配置
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'],
methods: [:get, :post, :options, :delete, :put]
end
end
如果我使用rails middleware
列出 Middlewares,則Rack::Cors
在use Webpacker::DevServerProxy
之后use Webpacker::DevServerProxy
在第二位,但每個請求都會引發以下錯誤。
Processing by DeviseTokenAuth::SessionsController#create as HTML
Parameters: {"session"=>{}}
HTTP Origin header (http://localhost:4200) didn't match request.base_url (http://localhost:3000)
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
事實證明,我需要更改protect_from_forgery 的處理方式,如文檔中所述
protect_from_forgery with: :null_session
PS:對於刪除他答案的人,請不要這樣做,知道不做什么也是學習的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.