- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在创建一个基于 unity 引擎的多人游戏,我想使用 Ruby on rails 公开和 api 来注册和验证用户,同时执行登录/注册等常规操作,以及创建房间等我的功能 - 这将作为条目存储在数据库中-
这些简单的操作我希望 api 来处理它,我不只是问一个教程来为用户进行身份验证,我希望有人解释我的 API 机制将如何,我将对用户进行身份验证基于代币,我猜是这样吗?
如果它是正确的,任何人都可以给我一个简单的步骤或对我的案例有用的教程,因为我已经搜索了很多,尤其是当“设计”不再支持身份验证时,据我所知,我不我认为设计不会帮助我登录操作,因为它适用于存在的模型而不是 API。
我希望我已经表达了我的观点,等待你的帮助:)
最佳答案
您可以使用 devise与 simple_token_authentication
安装 devise
并使用 devise
gem 创建您的可验证模型。
rails generate devise MODEL
将 acts_as_token_authenticatable
添加到 MODEL。
如果要对User
(MODEL)进行认证,需要添加如下代码:
/your_app/app/models/user.rb
class User < ActiveRecord::Base
acts_as_token_authenticatable
# Note: you can include any module you want. If available,
# token authentication will be performed before any other
# Devise authentication method.
#
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
end
添加路由和 Controller
/your_app/app/controllers/api/base_controller.rb
class Api::BaseController < ActionController::Base
self.responder = ApiResponder # Ignore this, just for now.
respond_to :json
end
All your API controllers need to inherit from base controller
/your_app/app/controllers/api/login_attempts_controller.rb
class Api::LoginAttemptsController < Api::BaseController
def create
respond_with Api::LoginAttempt.new(params[:email], params[:password])
rescue
render nothing: true, status: 401
end
end
/your_app/app/models/api/login_attempt.rb
class Api::LoginAttempt
include ActiveModel::Serializers::JSON
attr_accessor :uid, :token
def initialize(_email, _password)
user = MobileUser.where(email: _email).first
raise 'invalid_password' unless user.valid_password? _password
self.uid = user.email
self.token = user.authentication_token
end
def attributes
{ uid: nil, token: nil }
end
end
You need the
LoginAttemptsController
(Call it whatever you want) to sign in the user. If the givenpassword
anduid
andtoken
to authorize user's future requests.
/your_app/app/controllers/api/bills_controller.rb
class Api::BillsController < Api::BaseController
acts_as_token_authentication_handler_for User
def show
respond_with bill
end
private
def bill
@bill ||= Bill.find(params[:id])
end
end
BillsController
it's just a controller to access with an authenticated request. As you can see, you need to addacts_as_token_authentication_handler_for User
to protect the controller of unauthorized requests.
/your_app/config/routes.rb
Rails.application.routes.draw do
namespace :api, defaults: { format: :json } do
resources :login_attempts, only: [:create]
resources :bills, only: [:show]
end
end
创建一个 User
实例:User.create!(email: "leandro@example.com")
。这将使用 authentication_token: "_-7CtsAxPE5SsdsfzMkY"
You can create the user using the api. I don't do this here.
使用用户的电子邮件和密码登录,执行对 /api/login_attempts
的 POST
请求。
执行授权请求:GET/api/bills/:id
传递 header :
在这个例子中我使用了:
/your_app/app/serializers/bill_serializer.rb
class BillSerializer < ActiveModel::Serializer
attributes :id, :created_at, :amount
end
关于ruby-on-rails - 在 ruby on rails 上创建身份验证 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36106337/
我正在使用SQL Server 2008 R2,并且想创建一个触发器。 对于每个添加(仅添加),将像这样更新一列: ABC-CurrentYear-AutoIncrementCode 例子: ABC-
是否可以在显示最终一致性的数据存储中创建/存储用户帐户? 似乎不可能在没有一堆架构复杂性的情况下管理帐户创建,以避免可能发生具有相同 UID(例如电子邮件地址)的两个帐户的情况? 最终一致性存储的用户
您好, 我有一个带有 Identity 的 .NetCore MVC APP并使用 this指导我能够创建自定义用户验证器。 public class UserDomainValidator : IU
这与以下问题相同:HiLo or identity? 我们以本站的数据库为例。 假设该站点具有以下表格: 帖子。 投票。 注释。 使用它的最佳策略是什么: 身份 - 这是更常见的。 或者 HiLo -
我想将 Blazor Server 与 ASP.NET Identity 一起使用。但我需要使用 PostgreSQL 作为用户/角色存储,因为它在 AWS 中。 它不使用 EF,这是我需要的。 我创
我正在开发一个 .NET 应用程序,它可以使用 Graph API 代表用户发送电子邮件。 提示用户对应用程序进行授权;然后使用获取的访问 token 来调用 Graph API。刷新 token 用
我使用 ASP.NET 身份和 ClaimsIdentity 来验证我的用户。当用户通过身份验证时,属性 User.Identity 包含一个 ClaimsIdentity 实例。 但是,在登录请求期
所以我在两台机器上都安装了 CYGWIN。 如果我这样做,它会起作用: ssh -i desktop_rsa root@remoteserver 这需要我输入密码 ssh root@remoteser
我尝试在 mac osx 上的终端中通过 telnet 连接到 TOR 并请求新身份,但它不起作用,我总是收到此错误消息: Trying 127.0.0.1... telnet: connect to
我正在开发一个 .NET 应用程序,它可以使用 Graph API 代表用户发送电子邮件。 提示用户对应用程序进行授权;然后使用获取的访问 token 来调用 Graph API。刷新 token 用
我正在开发一项服务,客户可以在其中注册他们的 webhook URL,我将发送有关已注册 URL 的更新。为了安全起见,我想让客户端(接收方)识别是我(服务器)向他们发送请求。 Facebook和 G
在 Haskell 中,有没有办法测试两个 IORef 是否相同?我正在寻找这样的东西: IORef a -> IORef a -> IO Bool 例如,如果您想可视化由 IORef 组成的图形,这
我是 .NET、MVC 和身份框架的新手。我注意到身份框架允许通过注释保护单个 Controller 操作。 [Authorize] public ActionResult Edit(int? Id)
我有一列具有身份的列,其计数为19546542,我想在删除所有数据后将其重置。我需要类似ms sql中的'dbcc checkident'这样的内容,但在Oracle中 最佳答案 在Oracle 12
这是我用来创建 session 以发送电子邮件的代码: props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enabl
我想了解 [AllowAnonymous] 标签的工作原理。 我有以下方法 [HttpGet] public ActionResult Add() { return View(); } 当我没
在使用沙盒测试环境时,PayPal 身份 token 对某些人显示而不对其他人显示的原因是否有任何原因。 我在英国使用 API,终生无法生成或找到 token 。 我已经遵循协议(protocol)并
我对非常简单的事情有一些疑问:IDENTITY。我尝试在 phpMyAdmin 中创建表: CREATE TABLE IF NOT EXISTS typEventu ( typEventu
习语 #1 和 #5 是 FinnAPL Idiom Library两者具有相同的名称:“Progressive index of (without replacement)”: ((⍴X)⍴⍋⍋X⍳
当我第一次在 TFS 中设置时,我的公司拼错了我的用户名。此后他们将其更改为正确的拼写,但该更改显然未反射(reflect)在 TFS 中。当我尝试 checkin 更改时,出现此错误: 有没有一种方
我是一名优秀的程序员,十分优秀!