1

我使用 Metro RI(连同 AuthenticationHandler)在 Java 中开发了一个 Web 服务,但是当我向 Web 服务发送 SOAP 请求时,我无法在 SOAPHandler 中获取 SOAPHeader。

下面是我传递给 web 服务的 SOAP 请求。(我为此目的使用 SOAP UI)

`

<soapenv:Envelope xmlns:ser="http://server.webservice.bank/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header>
      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <wsse:UsernameToken wsu:Id="UsernameToken-4" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
            <wsse:Username>kshitij.jain</wsse:Username>
            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Phone0144</wsse:Password>
            <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">jf97anyZJUpR216tw4GRIw==</wsse:Nonce>
            <wsu:Created>2013-05-15T17:38:49.610Z</wsu:Created>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <ser:getCreditCardTransaction>
         <cardId>2</cardId>
         <amt>109</amt>
         <!--Optional:-->
         <descr>test</descr>
      </ser:getCreditCardTransaction>
   </soapenv:Body>
</soapenv:Envelope>

`

以下是我的 webservice 和 Handler 代码:

`

@WebService(serviceName = "getTransaction")
@HandlerChain(file = "ServerHandler.xml")
public class getTransaction {

    @Inject
    TransactionService tranService;
    @Resource
    private WebServiceContext wsContext;    

    @WebMethod(operationName = "getCreditCardTransaction")
    public CreCardTranResponse getCreditCardTransaction(@WebParam(name = "cardId") int cardId, @WebParam(name = "amt") int amt, @WebParam(name = "descr") String descr) {                 

        CreCardTranResponse res = new CreCardTranResponse();
        tranService.addTransaction(amt, descr, "Debit", cardId);
        res.setAmtDeducted(new Integer(amt).toString());
        res.setCardID(new Integer(cardId).toString());
        ReturnMessage ret = new ReturnMessage();
        ret.setReturnCode("0");
        ret.setReturnMessage("Successful");
        res.setRetMessage(ret);
        return res;
    }
}

`

`

public class AuthenticationHandler implements SOAPHandler<SOAPMessageContext> {

    private static Set<QName> headers;

    static {
        HashSet<QName> set = new HashSet<QName>();
        set.add(new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security"));
        headers = Collections.unmodifiableSet(set);
    }

    @Override
    public Set<QName> getHeaders() {
        return headers;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {

        try {
            handleSecurityMessage(context);
        } catch (SOAPException ex) {
        }

        return true;
    }

    public boolean handleSecurityMessage(SOAPMessageContext context) throws SOAPException {

        SOAPHeader soapHeader = context.getMessage().getSOAPHeader();        
        SOAPFault fault = SOAPFactory.newInstance().createFault();

        if (soapHeader == null) {
            fault.setFaultCode("failed");
            fault.setFaultString("Soap header missing");
            throw new SOAPFaultException(fault);
        }

        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return true;
    }

    @Override
    public void close(MessageContext context) {
    }
}

`

下面是 serverHandler.xml

`

<?xml version="1.0" encoding="UTF-8"?>
<handler-config>
   <handler-chain>
      <handler-chain-name>AuthenticationHandlerChain</handler-chain-name>
      <handler>
         <handler-name>AuthenticationHandler</handler-name>
         <handler-class>bank.webservice.security.AuthenticationHandler</handler-class>
      </handler>
   </handler-chain>
</handler-config>

`

我已经为此浪费了 2 天时间,但是在也从 SOAP UI 添加标头之后,代码将标头显示为 null 。

是 SOAP UI 还是代码的问题?

4

0 回答 0