我的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
不会加载新标头(结果是无限循环)。有什么想法吗?
答案 0 :(得分:1)
我在这里添加了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
end
rubiii补充道:
注意我删除了Savon :: Model,因为你实际上并不需要它,我也不知道它是否支持这种解决方法。 如果查看#call方法,它会在每次请求之前访问并更改全局变量。