首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >身份验证后强制重建SOAP-Header

身份验证后强制重建SOAP-Header
EN

Stack Overflow用户
提问于 2013-01-21 19:40:44
回答 1查看 279关注 0票数 1

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

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

代码语言:javascript
复制
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

当我打电话的时候

代码语言:javascript
复制
webservice = Soapservice.new
webservice.get_server_time

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-28 19:17:52

我在这里添加了rubiii的answer from the GitHub-Issue,以备将来参考:

代码语言:javascript
复制
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方法,它会在每次请求之前访问和更改全局变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14437837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档