我的SOAP-Server期望每个请求在soap-header中都有一个有效的令牌来验证soap-client。这个令牌只在一段时间内有效,所以我不得不预计它在每次调用中都是无效的。
我正在尝试找到一种方法,在我(重新)向SOAP-Server进行身份验证后,强制savon重新构建SOAP-Header (即使用新的auth-token)。我不确定这是一个稀树问题还是一个红宝石问题。这是我到目前为止所拥有的。
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不会加载新的标头(结果是无限循环)。有什么想法吗?
发布于 2013-01-28 19:17:52
我在这里添加了rubiii的answer from the GitHub-Issue,以备将来参考:
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
endrubiii补充道:
注意到我删除了Savon::Model,因为你实际上并不需要它,而且我不知道它是否支持这个变通方法。如果您查看#call方法,它会在每次请求之前访问和更改全局变量。
https://stackoverflow.com/questions/14437837
复制相似问题