设计令牌身份验证 - 不将令牌返回给客户端

时间:2021-02-16 05:05:28

标签: ruby-on-rails devise devise-token-auth

设置 devise_token_auth。

不断遇到源代码中的问题。

例如:

def token_validation_response
  as_json(except: %i[tokens created_at updated_at])
end

它将被调用:

SessionsController::create > render_create_success > token_validation_response

据我了解,您需要将令牌获取到客户端并将其存储在客户端并随每个请求一起发送。 如果我们不将令牌提供给客户端并except 它,我不确定这应该如何工作。

except: %i[tokens created_at updated_at]

来源链接: https://github.com/lynndylanhurley/devise_token_auth/blob/c92258038c05fcc8f6a0374ccce2e63b9f8d5312/app/controllers/devise_token_auth/sessions_controller.rb#L36

https://github.com/lynndylanhurley/devise_token_auth/blob/c92258038c05fcc8f6a0374ccce2e63b9f8d5312/app/models/devise_token_auth/concerns/user.rb#L205

1 个答案:

答案 0 :(得分:0)

也许这对某人有用。令牌出现在标题而不是正文中。

更新:

我还需要解决客户端 cors 问题。

一些有帮助的选项:

Vue Axios CORS policy: No 'Access-Control-Allow-Origin'

https://blog.bitsrc.io/how-and-why-you-should-avoid-cors-in-single-page-apps-db25452ad2f8

最简单的解决方案是在命令行中执行 chrome:

open -n -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir="/tmp/chrome_dev_test" --disable-web-security

来源:https://alfilatov.com/posts/run-chrome-without-cors/