gpt4 book ai didi

java - 将安全 header 信息添加到从 WSDL 生成的 Java 代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:08 25 4
gpt4 key购买 nike

我使用 Netbeans Web 服务向导生成给定 WSDL 的 Java 代码。如果我将 Web 服务方法拖到一个类中,它会创建一些 Java 代码来调用该 Web 服务(例如:SubmitApplication)。我可以看到如何填充对象以将信息发送到该 Web 服务,但该服务还需要带有用户名/密码的安全 header 。

有一个名为 SecurityHeader 的生成类,其中包含用户名/密码属性。我可以使用有效的用户名/密码创建此对象,但我看不到如何传递该对象或将其添加到 SubmitApplication 调用。如何将 SecurityHeader 添加到 SubmitApplication 调用?

下面是 SOAP 请求的示例:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<SecurityHeader xmlns="http://schemas.turss.com/BDS/1.0/">
<CreateTime>6/8/2012 8:32:59 PM</CreateTime>
<Owner>Sample_Owner</Owner>
<HashKey>Sample_Hash_Key</HashKey>
</SecurityHeader>
</soap:Header>
<soap:Body>
<SubmitApplication xmlns="http://schemas.turss.com/BDS/1.0/">
<newSearch>
<CurrentApplicant xmlns="http://schemas.turss.com/BDS/1.0/proxy">
<FirstName>Bob</FirstName>
<MiddleName />
<LastName>Smith</LastName>
<Suffix />
<BirthDate>1970-10-20T00:00:00</BirthDate>
<SSN />
<Address />
<City />
<State />
<PostalCode />
</CurrentApplicant>
<PermissiblePurpose xmlns="http://schemas.turss.com/BDS/1.0/proxy">TenantScreening</PermissiblePurpose>
</newSearch>
</SubmitApplication>
</soap:Body>
</soap:Envelope>

最佳答案

我找到了一个将 token 字符串添加到 soap 安全 header 的示例代码。这是下面代码的标题形式:

<TicketHeader>
<Ticket>OD01096347CCA</Ticket>
</TicketHeader>

给消息添加头部的方法:

// Security token
String token;
// MyService and MySoapService are stubs generated from WSDL
MyService service = new MyService();
MyServiceSoap ep = service.getMyServiceSoap();

Binding binding = ((BindingProvider) ep).getBinding();
List handlers = binding.getHandlerChain();
handlers.add(new MySOAPHandler(token));
binding.setHandlerChain(handlers);

MySoapHandler 代码:

public class MySOAPHandler implements SOAPHandler {

private String token;

public DHSOAPHandler(String token) {
this.token = token;
}
public boolean handleMessage(SOAPMessageContext messageContext) {
SOAPMessage
msg = messageContext.getMessage();
if ((Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
try {
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPHeader header = envelope.addHeader();
SOAPElement el = header.addHeaderElement(envelope.createName("TicketHeader",
"", "http://ws.service.com/"));
el = el.addChildElement(envelope.createName("Ticket", "", "http://ws.service.com/"));
el.setValue(token);
msg.saveChanges();
}
catch (SOAPException e) {
return false;
}
}
return true;
}

public boolean handleFault(SOAPMessageContext messageContext) {
return true;
}

public void close(MessageContext messageContext){
}
// I'm not quite sure about what should this function do, but I guess something like this...
public Set getHeaders(){
Set headers = new HashSet();
headers.add(new QName("https://ws.service.com/", "TicketHeader"));
return headers;
}
}

关于java - 将安全 header 信息添加到从 WSDL 生成的 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11006613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com