Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用API在Rails中进行身份验证_Ruby On Rails_Ruby_Devise_Rest Client - Fatal编程技术网

Ruby on rails 使用API在Rails中进行身份验证

Ruby on rails 使用API在Rails中进行身份验证,ruby-on-rails,ruby,devise,rest-client,Ruby On Rails,Ruby,Devise,Rest Client,我目前有一个Rails应用程序连接到现有的SQL数据库。我使用Desive进行用户管理,但是数据库中预先存在的用户表使用了非常定制的密码加密方法 我可以连接到一个web服务,它通过一个JSON对象和登录信息来验证它是否有效,我必须管理我自己的会话以及之后的一切 我试图遵循“”,并将其与Desive和一些堆栈溢出搜索相结合,但事情进展不太顺利 这就是我现在拥有的,但它没有做任何事情,我只是觉得我没有走上正确的轨道 class SessionsController < Devise::Sess

我目前有一个Rails应用程序连接到现有的SQL数据库。我使用Desive进行用户管理,但是数据库中预先存在的用户表使用了非常定制的密码加密方法

我可以连接到一个web服务,它通过一个JSON对象和登录信息来验证它是否有效,我必须管理我自己的会话以及之后的一切

我试图遵循“”,并将其与Desive和一些堆栈溢出搜索相结合,但事情进展不太顺利

这就是我现在拥有的,但它没有做任何事情,我只是觉得我没有走上正确的轨道

class SessionsController < Devise::SessionsController
    def new
        super
    end

    def create
        post_params = {
            "RuntimeEnvironment" => 1,
            "Email" => params[:session][:email],
            "Password" => params[:session][:password]
        }.to_json
        user_params = RestClient.post 'http://some.ip/WebServices', post_params, :content_type => "json"
        user = User.authenticate(user_params)
        if user
            session[:user_id] = user.user_id
            redirect_to root_path
        else
            flash.now.alert = "Invalid Username or Password"
            render "new"
        end
    end
end
对于失败的,返回什么:

{"Success":true,"ErrorMessage":"","ResponseString":"","LoginResultData":{"FailMessage":"Invalid email address","ResultCode":1,"User":null}}

在连接到API时,是否有一种方法可以使用Desive的会话管理?

您仍然可以使用用户提供的电子邮件和密码通过Desive进行身份验证。RestClient就像是一个双重检查:只需确保除了通过RestClient之外,没有用户可以通过身份验证的路由。您可以通过执行
rake routes
来检查这一点

为了检查结果代码是否有效,您可以执行一些JSON解析,如下所示:

authentication_response = RestClient.post 'http://some.ip/WebServices', post_params, :content_type => "json"
json_authentication_response = JSON.parse(authentication_response)
result_code = json_authentication_response["LoginResultData"]["ResultCode"]
if result_code == 0
    # Authenticate
else
    # Don't authenticate
end

你知道RestClient.post返回的确切内容吗?我已经更新了我的帖子。失败的帖子会说“Success”:true?Success:true用于连接。它会告诉我这是一个失败的登录通过“结果代码:1”,这是无效的电子邮件。结果代码2是无效密码。结果代码0成功登录。哇,这非常有帮助,谢谢。然而,真正困扰我的部分是电子邮件和密码变量是如何传入的,以及如何实际执行
#身份验证
。您仍然可以使用designes的身份验证方法并从参数传入电子邮件/密码。不过,您可能需要创建自己的策略来处理designes的身份验证方法中发生的事情。看看
authentication_response = RestClient.post 'http://some.ip/WebServices', post_params, :content_type => "json"
json_authentication_response = JSON.parse(authentication_response)
result_code = json_authentication_response["LoginResultData"]["ResultCode"]
if result_code == 0
    # Authenticate
else
    # Don't authenticate
end