1

我的 SOAP-Server 期望每个请求在soap-header 中都有一个有效的令牌来验证soap-client。这个令牌只在一段时间内有效,所以我不得不期望它在每次调用中都是无效的。

在我(重新)使用 SOAP-Server 进行身份验证后,我试图找到一种方法来强制 savon 重建 SOAP-Header(即使用新的 auth-token)。我不确定,这是一个 savon 问题还是一个红宝石问题。这是我到目前为止所拥有的。

class Soapservice
  extend Savon::Model

  # load stored auth-token
  @@header_data = YAML.load_file "settings.yaml"

  client wsdl: 'locally-cached-wsdl.xml', 
    soap_header: {'verifyingToken' => @@header_data}

  operations :get_authentification_token, :get_server_time

  # request a new auth-token and store it
  def get_authentification_token
    response = super(:message => {
        'oLogin' => {
            'Username' => 'username', 
            'Userpass' => 'password'
        }
    })

    settings = {
      'UserID' => response[:user_id].to_i,
      'Token' => response[:token], 
    }

    File.open("settings.yaml", "w") do |file|
        file.write settings.to_yaml
    end

    @@header_data = settings
  end

  def get_server_time
    return super()
    rescue Savon::SOAPFault => error
      fault_code = error.to_hash[:fault][:faultstring]
      if fault_code == 'Unauthorized Request - Invalide Token'
          get_authentification_token
          retry
      end
  end
end

当我打电话

webservice = Soapservice.new
webservice.get_server_time

使用无效令牌,它会重新验证并成功保存新令牌,但retry不会加载新标头(结果是无限循环)。有任何想法吗?

4

1 回答 1

1

我在这里添加了来自 GitHub-Issue 的rubiii 的答案以供将来参考:

class Soapservice

  # load stored auth-token
  @@header_data = YAML.load_file "settings.yaml"

  def initialize
    @client = Savon.client(wsdl: 'locally-cached-wsdl.xml')
  end

  def call(operation_name, locals = {})
    @client.globals[:soap_header] = {'verifyingToken' => @@header_data}
    @client.call(operation_name, locals)
  end

  # request a new auth-token and store it
  def get_authentification_token
    message = {
      'Username' => 'username', 
      'Userpass' => 'password'
    }
    response = call(:get_authentification_token, :message => message)

    settings = {
      'UserID' => response[:user_id].to_i,
      'Token'  => response[:token], 
    }

    File.open("settings.yaml", "w") do |file|
      file.write settings.to_yaml
    end

    @@header_data = settings
  end

  def get_server_time
    call(:get_server_time)
    rescue Savon::SOAPFault => error
      fault_code = error.to_hash[:fault][:faultstring]
      if fault_code == 'Unauthorized Request - Invalide Token'
        get_authentification_token
        retry
      end
  end

end

rubiii 补充说:

请注意,我删除了 Savon::Model,因为您实际上不需要它,而且我不知道它是否支持此解决方法。如果您查看#call 方法,它会在每次请求之前访问并更改全局变量。

于 2013-01-28T11:17:52.447 回答