- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我的 Rails 应用使用 Devise 进行身份验证。它有一个姊妹 iOS 应用程序,用户可以使用与 Web 应用程序相同的凭据登录到 iOS 应用程序。所以我需要某种 API 来进行身份验证。
这里有很多类似的问题指向this tutorial ,但它似乎已过时,如 token_authenticatable
模块已从设计中删除,并且一些行抛出错误。 (我使用的是 Devise 3.2.2。)我尝试根据该教程(和 this one )推出自己的版本,但我对它不是 100% 有信心 - 我觉得我可能有一些东西被误解或错过了。
首先,听从this gist的建议,我添加了一个 authentication_token
我的 users
的文本属性表,以及以下到 user.rb
:
before_save :ensure_authentication_token
def ensure_authentication_token
if authentication_token.blank?
self.authentication_token = generate_authentication_token
end
end
private
def generate_authentication_token
loop do
token = Devise.friendly_token
break token unless User.find_by(authentication_token: token)
end
end
然后我有以下 Controller :
api_controller.rb
class ApiController < ApplicationController
respond_to :json
skip_before_filter :authenticate_user!
protected
def user_params
params[:user].permit(:email, :password, :password_confirmation)
end
end
(请注意,我的 application_controller
有 before_filter :authenticate_user!
行。)
api/sessions_controller.rb
class Api::SessionsController < Devise::RegistrationsController
prepend_before_filter :require_no_authentication, :only => [:create ]
before_filter :ensure_params_exist
respond_to :json
skip_before_filter :verify_authenticity_token
def create
build_resource
resource = User.find_for_database_authentication(
email: params[:user][:email]
)
return invalid_login_attempt unless resource
if resource.valid_password?(params[:user][:password])
sign_in("user", resource)
render json: {
success: true,
auth_token: resource.authentication_token,
email: resource.email
}
return
end
invalid_login_attempt
end
def destroy
sign_out(resource_name)
end
protected
def ensure_params_exist
return unless params[:user].blank?
render json: {
success: false,
message: "missing user parameter"
}, status: 422
end
def invalid_login_attempt
warden.custom_failure!
render json: {
success: false,
message: "Error with your login or password"
}, status: 401
end
end
api/registrations_controller.rb
class Api::RegistrationsController < ApiController
skip_before_filter :verify_authenticity_token
def create
user = User.new(user_params)
if user.save
render(
json: Jbuilder.encode do |j|
j.success true
j.email user.email
j.auth_token user.authentication_token
end,
status: 201
)
return
else
warden.custom_failure!
render json: user.errors, status: 422
end
end
end
在 config/routes.rb 中:
namespace :api, defaults: { format: "json" } do
devise_for :users
end
我有点不知所措,但我敢肯定,我 future 的自己会在这里回顾和畏缩(通常是这样)。一些不确定的部分:
首先,您会注意到 Api::SessionsController
继承自 Devise::RegistrationsController
而Api::RegistrationsController
继承自 ApiController
(我还有一些其他 Controller ,例如 Api::EventsController < ApiController
,它们为我的其他模型处理更多标准 REST 内容,并且与 Devise 没有太多联系。)这是一个非常丑陋的安排,但我想不出另一种方法在 Api::RegistrationsController
中获取我需要的方法.我上面链接的教程有 include Devise::Controllers::InternalHelpers
行,但该模块似乎已在较新版本的 Devise 中被删除。
其次,我已通过 skip_before_filter :verify_authentication_token
行禁用了 CSRF 保护。 .我怀疑这是否是个好主意 - 我看到很多 conflicting或 hard to understand关于 JSON API 是否容易受到 CSRF 攻击的建议 - 但添加该行是我可以让该死的东西工作的唯一方法。
第三,我想确保了解用户登录后身份验证的工作原理。假设我有一个 API 调用 GET /api/friends
它返回当前用户的 friend 列表。据我了解,iOS 应用程序必须获取用户的 authentication_token
从数据库中(对于每个用户来说这是一个永远不会改变的固定值??),然后将其作为参数与每个请求一起提交,例如GET /api/friends?authentication_token=abcdefgh1234
,然后我的Api::FriendsController
可以做一些像 User.find_by(authentication_token: params[:authentication_token])
获取 current_user。真的这么简单,还是我遗漏了什么?
因此,对于任何设法一直阅读到这个庞大问题结尾的人,感谢您的宝贵时间!总结一下:
Devise::RegistrationsController
的丑陋设计以及来自 ApiController
的其他人.谢谢!
最佳答案
您不想禁用 CSRF,我读到人们认为出于某种原因它不适用于 JSON API,但这是一种误解。要使其保持启用状态,您需要进行一些更改:
在服务器端添加一个 after_filter 到你的 session Controller :
after_filter :set_csrf_header, only: [:new, :create]
protected
def set_csrf_header
response.headers['X-CSRF-Token'] = form_authenticity_token
end
这将生成一个 token ,将其放入您的 session 中并将其复制到所选操作的响应 header 中。
客户端 (iOS) 你需要确保两件事情就位。
您的客户端需要扫描此 header 的所有服务器响应并在传递时保留它。
... get ahold of response object
// response may be a NSURLResponse object, so convert:
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
// grab token if present, make sure you have a config object to store it in
NSString *token = [[httpResponse allHeaderFields] objectForKey:@"X-CSRF-Token"];
if (token)
[yourConfig setCsrfToken:token];
最后,您的客户端需要将此 token 添加到它发出的所有“非 GET”请求中:
... get ahold of your request object
if (yourConfig.csrfToken && ![request.httpMethod isEqualToString:@"GET"])
[request setValue:yourConfig.csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
最后一个难题是要了解当登录到设计时,正在使用两个后续 session /csrf token 。登录流程如下所示:
GET /users/sign_in ->
// new action is called, initial token is set
// now send login form on callback:
POST /users/sign_in <username, password> ->
// create action called, token is reset
// when login is successful, session and token are replaced
// and you can send authenticated requests
关于ruby-on-rails - 这个 Rails JSON 身份验证 API(使用 Devise)安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745843/
我正在学习 Spring 安全性,但我对它的灵活性感到困惑.. 我知道我可以通过在标签中定义规则来保护网址 然后我看到有一个@secure 注释可以保护方法。 然后还有其他注释来保护域(或 POJO)
假设有一个 key 加密 key 位于内存中并且未写入文件或数据库... byte[] kek = new byte[32]; secureRandom.nextBytes(kek); byte[]
我有 Spring Security 3.2.0 RC1 的问题 我正在使用标签来连接我 这表示“方法‘setF
我正在创建一个使用 Node Js 服务器 API 的 Flutter 应用程序。对于授权,我决定将 JWT 与私钥/公钥一起使用。服务器和移动客户端之间的通信使用 HTTPS。 Flutter 应用
在过去的几年里,我一直在使用范围从 Raphael.js 的 javascript 库。至 D3 ,我已经为自己的教育操纵了来自网络各地的动画。我已经从各种 git 存储库下载了 js 脚本,例如 s
在 python 中实现身份验证的好方法是什么?已经存在的东西也很好。我需要它通过不受信任的网络连接进行身份验证。它不需要太高级,只要足以安全地获取通用密码即可。我查看了 ssl 模块。但那个模块让我
我正在尝试学习“如何在 Hadoop 中实现 Kerberos?”我已经看过这个文档 https://issues.apache.org/jira/browse/HADOOP-4487我还了解了基本的
我有一个带有 apache2、php、mysql 的生产服务器。我现在只有一个站点 (mysite.com) 作为虚拟主机。我想把 phpmyadmin、webalizer 和 webmin 放在那里
前些天在网上看到防火墙软件OPNsense,对其有了兴趣,以前写过一个其前面的一个软件M0n0wall( 关于m0n0wa
我在 Spring Boot 和 oauth2(由 Google 提供)上编写了 rest 后端,在 "/login" 上自动重定向。除了 web 的 oauth 之外,我还想在移动后端进行 Fire
我想调用类 Foo,它的构造函数中有抽象类 Base。我希望能够从派生自 Base 的 Derived 调用 Foo 并使用 Derived覆盖方法而不是 Base 的方法。 我只能按照指示使用原始指
如何提高 session 的安全性? $this->session->userdata('userid') 我一直在为我的 ajax 调用扔掉这个小坏蛋。有些情况我没有。然后我想,使用 DOM 中的
我目前正在为某些人提供程序集编译服务。他们可以在在线编辑器中输入汇编代码并进行编译。然后编译它时,代码通过ajax请求发送到我的服务器,编译并返回程序的输出。 但是,我想知道我可以做些什么来防止对服务
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
目前,我通过将 session 中的 key 与 MySQl 数据库中的相同 key 相匹配来验证用户 session 。我使用随机数重新生成 session ,该随机数在每个页面加载时都受 MD5
Microsoft 模式与实践团队提供了一个很棒的 pdf,称为:“构建安全的 asp.net 应用程序”。 microsoft pdf 由于它是为 .Net 1.0 编写的,所以现在有点旧了。有谁知
在 Lua 中,通常会使用 math.random 生成随机值和/或字符串。 & math.randomseed , 其中 os.time用于 math.randomseed . 然而,这种方法有一个
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我们有一个严重依赖 Ajax 的应用程序。确保对服务器端脚本的请求不是通过独立程序而是通过坐在浏览器上的实际用户的好方法是什么 最佳答案 真的没有。 通过浏览器发送的任何请求都可以由独立程序伪造。 归
我正在寻找使用 WebSockets 与我们的服务器通信来实现 web (angular) 和 iPhone 应用程序。在过去使用 HTTP 请求时,我们使用请求数据、url、时间戳等的哈希值来验证和
我是一名优秀的程序员,十分优秀!