我认为您可以在第二个应用程序上使用您的第一个应用程序身份验证,您可以创建一个链接,就像 facebook 或 twitter 一样,重定向到控制器,然后这个控制器向您的第一个应用程序发送请求(您需要安全代码来做到这一点) 然后得到响应,保存你想要的数据(user_id 或帐户)并且效果很好......
使用protect_from_forgery 的API 类:
class ApiController < ApplicationController
protect_from_forgery :except => 'custom_login'
end
这样做可以避免默认的 rails 保护,并验证
<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
常用于导轨形式。然后你可以实现你的秘密哈希码,并像这样验证它:
class ApiController < ApplicationController
protect_from_forgery :except => :login_from_app2
def login_from_app2
if params[:authentication] == auth_hash
if login(params[:user_credentials])
render :text => "Success" #better you return a json
else
render :text => "Fail login" #better you return a json
end
else
render :text => 'Invalid security hash'
end
end
def auth_hash
"8f2e4b354f193744272fe246ca9e8bf5"
end
end
这样,您只有 app2 的代码将发送“发布”请求以访问 app1,您可以控制登录。
我不知道这是否 100% 安全,我认为是,但如果有人能解释为什么在这种情况下这不是一个好方法,我会很高兴。