宝石“ rack-cors”不起作用,并且从角度客户端向Rails api的请求引发cors错误

时间:2018-12-10 05:16:27

标签: ruby-on-rails ruby angular cors rack-cors

我正在努力让宝石“ rack-cors”在本地工作。我使用gem'devise_token_auth'进行裸露的设置,并使用'angular-token'对角7客户端进行身份验证。但是来自angular的每个请求都会引发cors错误。我已经按照文档配置了rails和“ rack-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列出中间件,则Rack::Cors排在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):

1 个答案:

答案 0 :(得分:0)

事实证明,我需要更改docs中所述的protect_from_forgery的处理方式

protect_from_forgery with: :null_session

PS:对于删除答案的人,请不要那样做,因为知道不做什么也是学习的一部分。