首页 > 解决方案 > 使用 SSL、cookie、标头在站点上进行身份验证 - 不起作用

问题描述

对于网站https://app.guardsaas.com上的授权,我需要:

  1. 打开https://app.guardsaas.com/login并获取值“_csrf_token”
  2. 使用参数发出 POST 请求https://app.guardsaas.com/login_check :

    '_username' = login '_password' = pass '_remember_me' = 'on' '_csfr_token' = 来自 p.1 的令牌

我写了一个脚本,首先得到一个令牌,然后在网站的帮助下授权,但是它不起作用,错误可以在SSL,cookie中覆盖,尝试了一堆不同的选项 - 没有任何结果

我的代码:

require "net/http"
require 'net/https'
require "uri"

uri = URI('https://app.guardsaas.com/login')
token = ""
text = ""

http = Net::HTTP.start(uri.host, uri.port,
  :use_ssl => uri.scheme == 'https', 
  :verify_mode => OpenSSL::SSL::VERIFY_NONE)

# make a request to get the server's cookies
response = http.get('https://app.guardsaas.com/login')
puts response.code
if (response.code == '200')
    all_cookies = response.get_fields('set-cookie')
    cookies_array = Array.new
    all_cookies.each { | cookie |
        cookies_array.push(cookie.split('; ')[0])
    }
    cookies = cookies_array.join('; ')

    text = response.body.to_s
    st = text.index('_csrf_token" value=').to_i
    token = text[st+20..st+19+40]
    puts "token: " + token

    data = {
    "_username" => 'login',
    "_password" => 'pass',
    "_remember_me" => 'on',
    "_csrf_token"  => token
    }

    data = URI.encode_www_form(data)

    headers = {
    'Cookie' => cookies,
    'Referer' => 'https://app.guardsaas.com',
    'Content-Type' => 'application/x-www-form-urlencoded'
    }

    headers = URI.encode_www_form(headers)

    data = http.post('https://app.guardsaas.com/login_check', data, headers)


    puts data.body
end

标签: rubyauthenticationsslpostcookies

解决方案


推荐阅读