3

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

有一个名为 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>
4

1 回答 1

1

我找到了一个将令牌字符串添加到肥皂安全标头的示例代码。这是下面代码的标题形式:

<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;
    }
}
于 2012-06-15T06:53:47.873 回答