无法将java soap客户端连接到启用了Ws-Security的.NET Web服务

时间:2016-08-12 21:12:42

标签: java .net web-services soap ws-security

我遇到了各种各样的错误,包括错误的请求和#34;到"不正确的用户名/密码"和"没有用户名"并且"没有可用的密码或回叫处理程序"尝试将代码生成的Java客户端连接到启用了WS-Security的.NET soap服务时。

以下是.NET服务的WSDL:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://filesubmissionservice.fset.rsi" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="FileSubmissionService" targetNamespace="http://filesubmissionservice.fset.rsi">
<wsp:Policy wsu:Id="BasicHttpsBinding_IFileSubmissionService_policy">
<wsp:ExactlyOne>
<wsp:All>
<sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:TransportToken>
<wsp:Policy>
<sp:HttpsToken RequireClientCertificate="false"/>
</wsp:Policy>
</sp:TransportToken>
<sp:AlgorithmSuite>
<wsp:Policy>
<sp:Basic256/>
</wsp:Policy>
</sp:AlgorithmSuite>
<sp:Layout>
<wsp:Policy>
<sp:Lax/>
</wsp:Policy>
</sp:Layout>
<sp:IncludeTimestamp/>
</wsp:Policy>
</sp:TransportBinding>
<sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy>
<sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
<wsp:Policy>
<sp:WssUsernameToken10/>
</wsp:Policy>
</sp:UsernameToken>
</wsp:Policy>
</sp:SignedSupportingTokens>
<sp:Wss10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
<wsp:Policy/>
</sp:Wss10>
</wsp:All>
</wsp:ExactlyOne>
</wsp:Policy>
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://filesubmissionservice.fset.rsi">
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService"/>
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core"/>
<xs:element name="ValidateFlatFile">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="fileName" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="file" nillable="true" type="xs:base64Binary"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ValidateFlatFileResponse">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" minOccurs="0" name="ValidateFlatFileResult" nillable="true" type="q1:SubmissionValidationResults"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ValidateXmlFile">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="xmlContents" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="submissionType" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="schemaVersion" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ValidateXmlFileResponse">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" minOccurs="0" name="ValidateXmlFileResult" nillable="true" type="q2:SubmissionValidationResults"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SubmitMessage">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q3="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" minOccurs="0" name="message" nillable="true" type="q3:MessageData"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SubmitMessageResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="SubmitMessageResult" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/">
<xs:element name="anyType" nillable="true" type="xs:anyType"/>
<xs:element name="anyURI" nillable="true" type="xs:anyURI"/>
<xs:element name="base64Binary" nillable="true" type="xs:base64Binary"/>
<xs:element name="boolean" nillable="true" type="xs:boolean"/>
<xs:element name="byte" nillable="true" type="xs:byte"/>
<xs:element name="dateTime" nillable="true" type="xs:dateTime"/>
<xs:element name="decimal" nillable="true" type="xs:decimal"/>
<xs:element name="double" nillable="true" type="xs:double"/>
<xs:element name="float" nillable="true" type="xs:float"/>
<xs:element name="int" nillable="true" type="xs:int"/>
<xs:element name="long" nillable="true" type="xs:long"/>
<xs:element name="QName" nillable="true" type="xs:QName"/>
<xs:element name="short" nillable="true" type="xs:short"/>
<xs:element name="string" nillable="true" type="xs:string"/>
<xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte"/>
<xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt"/>
<xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong"/>
<xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort"/>
<xs:element name="char" nillable="true" type="tns:char"/>
<xs:simpleType name="char">
<xs:restriction base="xs:int"/>
</xs:simpleType>
<xs:element name="duration" nillable="true" type="tns:duration"/>
<xs:simpleType name="duration">
<xs:restriction base="xs:duration">
<xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?"/>
<xs:minInclusive value="-P10675199DT2H48M5.4775808S"/>
<xs:maxInclusive value="P10675199DT2H48M5.4775807S"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="guid" nillable="true" type="tns:guid"/>
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}"/>
</xs:restriction>
</xs:simpleType>
<xs:attribute name="FactoryType" type="xs:QName"/>
<xs:attribute name="Id" type="xs:ID"/>
<xs:attribute name="Ref" type="xs:IDREF"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService">
<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core"/>
<xs:complexType name="SubmissionValidationResults">
<xs:sequence>
<xs:element xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" minOccurs="0" name="ResultVars" nillable="true" type="q1:ArrayOfKeyValueOfstringstring"/>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" minOccurs="0" name="Submissions" nillable="true" type="q2:ArrayOfSubmissionFormData"/>
<xs:element xmlns:q3="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" minOccurs="0" name="ValidationErrors" nillable="true" type="q3:ArrayOfErrorInfo"/>
</xs:sequence>
</xs:complexType>
<xs:element name="SubmissionValidationResults" nillable="true" type="tns:SubmissionValidationResults"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<xs:complexType name="ArrayOfKeyValueOfstringstring">
<xs:annotation>
<xs:appinfo>
<IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="KeyValueOfstringstring">
<xs:complexType>
<xs:sequence>
<xs:element name="Key" nillable="true" type="xs:string"/>
<xs:element name="Value" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfKeyValueOfstringstring" nillable="true" type="tns:ArrayOfKeyValueOfstringstring"/>
<xs:complexType name="ArrayOfKeyValueOfstringanyType">
<xs:annotation>
<xs:appinfo>
<IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="KeyValueOfstringanyType">
<xs:complexType>
<xs:sequence>
<xs:element name="Key" nillable="true" type="xs:string"/>
<xs:element name="Value" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfKeyValueOfstringanyType" nillable="true" type="tns:ArrayOfKeyValueOfstringanyType"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core">
<xs:import namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
<xs:import namespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin"/>
<xs:complexType name="ArrayOfSubmissionFormData">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="SubmissionFormData" nillable="true" type="tns:SubmissionFormData"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfSubmissionFormData" nillable="true" type="tns:ArrayOfSubmissionFormData"/>
<xs:complexType name="SubmissionFormData">
<xs:sequence>
<xs:element minOccurs="0" name="ContainsPayment" type="xs:boolean"/>
<xs:element minOccurs="0" name="ETIN" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ElectronicPostmark" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="FilerName" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="FormDataStr" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="IdentityNumber" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="IdentityType" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Jurisdiction" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="MessageId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="MessageKey" nillable="true" type="xs:int"/>
<xs:element minOccurs="0" name="MessageSource" nillable="true" type="xs:int"/>
<xs:element xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" minOccurs="0" name="PluginVars" nillable="true" type="q1:ArrayOfKeyValueOfstringanyType"/>
<xs:element minOccurs="0" name="ReceivedTimestamp" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SWID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SchemaLocation" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SchemaVersion" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SoftwareId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Status" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SubmissionCategory" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SubmissionId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SubmissionKey" nillable="true" type="xs:int"/>
<xs:element minOccurs="0" name="SubmissionType" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="TaxYear" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:element name="SubmissionFormData" nillable="true" type="tns:SubmissionFormData"/>
<xs:complexType name="ArrayOfErrorInfo">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="ErrorInfo" nillable="true" type="tns:ErrorInfo"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfErrorInfo" nillable="true" type="tns:ArrayOfErrorInfo"/>
<xs:complexType name="ErrorInfo">
<xs:sequence>
<xs:element minOccurs="0" name="DataValue" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="DocumentId" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ErrorCategory" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ErrorMessage" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="RuleNumber" nillable="true" type="xs:string"/>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin" minOccurs="0" name="Severity" type="q2:RuleSeverity"/>
<xs:element minOccurs="0" name="XPath" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ErrorInfo" nillable="true" type="tns:ErrorInfo"/>
<xs:complexType name="MessageData">
<xs:sequence>
<xs:element minOccurs="0" name="MessageID" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="MessageKey" nillable="true" type="xs:int"/>
<xs:element minOccurs="0" name="Source" type="xs:int"/>
<xs:element minOccurs="0" name="Submissions" nillable="true" type="tns:ArrayOfSubmissionFormData"/>
</xs:sequence>
</xs:complexType>
<xs:element name="MessageData" nillable="true" type="tns:MessageData"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin">
<xs:simpleType name="RuleSeverity">
<xs:restriction base="xs:string">
<xs:enumeration value="RejectAndStop"/>
<xs:enumeration value="Reject"/>
<xs:enumeration value="Alert"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="RuleSeverity" nillable="true" type="tns:RuleSeverity"/>
</xs:schema>
</wsdl:types>
<wsdl:message name="IFileSubmissionService_ValidateFlatFile_InputMessage">
<wsdl:part name="parameters" element="tns:ValidateFlatFile"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_ValidateFlatFile_OutputMessage">
<wsdl:part name="parameters" element="tns:ValidateFlatFileResponse"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_ValidateXmlFile_InputMessage">
<wsdl:part name="parameters" element="tns:ValidateXmlFile"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_ValidateXmlFile_OutputMessage">
<wsdl:part name="parameters" element="tns:ValidateXmlFileResponse"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_SubmitMessage_InputMessage">
<wsdl:part name="parameters" element="tns:SubmitMessage"/>
</wsdl:message>
<wsdl:message name="IFileSubmissionService_SubmitMessage_OutputMessage">
<wsdl:part name="parameters" element="tns:SubmitMessageResponse"/>
</wsdl:message>
<wsdl:portType name="IFileSubmissionService">
<wsdl:operation name="ValidateFlatFile">
<wsdl:input wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateFlatFile" message="tns:IFileSubmissionService_ValidateFlatFile_InputMessage"/>
<wsdl:output wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateFlatFileResponse" message="tns:IFileSubmissionService_ValidateFlatFile_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="ValidateXmlFile">
<wsdl:input wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateXmlFile" message="tns:IFileSubmissionService_ValidateXmlFile_InputMessage"/>
<wsdl:output wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateXmlFileResponse" message="tns:IFileSubmissionService_ValidateXmlFile_OutputMessage"/>
</wsdl:operation>
<wsdl:operation name="SubmitMessage">
<wsdl:input wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/SubmitMessage" message="tns:IFileSubmissionService_SubmitMessage_InputMessage"/>
<wsdl:output wsaw:Action="http://filesubmissionservice.fset.rsi/IFileSubmissionService/SubmitMessageResponse" message="tns:IFileSubmissionService_SubmitMessage_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpsBinding_IFileSubmissionService" type="tns:IFileSubmissionService">
<wsp:PolicyReference URI="#BasicHttpsBinding_IFileSubmissionService_policy"/>
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ValidateFlatFile">
<soap:operation soapAction="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateFlatFile" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="ValidateXmlFile">
<soap:operation soapAction="http://filesubmissionservice.fset.rsi/IFileSubmissionService/ValidateXmlFile" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="SubmitMessage">
<soap:operation soapAction="http://filesubmissionservice.fset.rsi/IFileSubmissionService/SubmitMessage" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="FileSubmissionService">
<wsdl:port name="BasicHttpsBinding_IFileSubmissionService" binding="tns:BasicHttpsBinding_IFileSubmissionService">
<soap:address location="https://10.1.2.166/FileSubmissionService/FileSubmissionService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

以下是我调用代码生成的java客户端并添加标题的地方:

private IFileSubmissionService createFileSubmissionServiceClient() {

    final String webserviceURL = applicationSessionBean.getExtProperties().getProperty("fileSubmissionServiceURL");
    final String username = applicationSessionBean.getExtProperties().getProperty("wsuser");
    final String password = applicationSessionBean.getExtProperties().getProperty("wspass");

    FileSubmissionService fsService = null;

    try {
        fsService = new FileSubmissionService();
    } catch (Exception e) {
        e.printStackTrace();
    }

    if(fsService == null) {
        //get service with default WSDL location
        fsService = new FileSubmissionService();
    }

    IFileSubmissionService client = fsService.getBasicHttpsBindingIFileSubmissionService(); 

    String        salt           = "XXXXXXXX";


    // Add security header
    Map<String, Object> ctx = ((BindingProvider)client).getRequestContext();
    ctx.put("ws-security.username", username);
    ctx.put("ws-security.password", getSecurePassword(password, salt.getBytes()));
    ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, webserviceURL);

    // Add headers
    List<Handler> handlerChain = new ArrayList<Handler>();
    handlerChain.add(new HeaderHandler(username, getSecurePassword(password, salt.getBytes())));
    ((BindingProvider)client).getBinding().setHandlerChain(handlerChain);

    return client;
}

以下是我的#34;标头处理程序&#34;我已经附加了安全标头:

   public boolean handleMessage(SOAPMessageContext context)
    {
        Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outboundProperty.booleanValue())
        {
            try
            {
                SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
                SOAPFactory factory = SOAPFactory.newInstance();
                SOAPHeader header = envelope.getHeader();

                if (header == null)
                {
                    header = envelope.addHeader();
                }

                header.setPrefix("S");

                envelope.addAttribute(QName.valueOf("xmlns:wsu"), utility_uri);

                SOAPElement securityElem = header
                        .addChildElement(new QName(uri, "Security", prefix));
                securityElem.addAttribute(QName.valueOf("S:mustUnderstand"), "1");

                SOAPElement timestampElem = securityElem.addChildElement("Timestamp", "wsu");
                timestampElem.addAttribute(QName.valueOf("wsu:Id"), "_0");

                TimeZone tz = TimeZone.getTimeZone("UTC");
                DateFormat df = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                df.setTimeZone(tz);

                String created = df.format(new Date());

                SOAPElement createdTimestampElem = factory.createElement("Created", utility_prefix,
                        utility_uri);
                createdTimestampElem.addTextNode(created);

                Calendar expiresCal = Calendar.getInstance();
                expiresCal.add(Calendar.MINUTE, 1);
                Date expireDate = expiresCal.getTime();

                String expires = df.format(expireDate);
                SOAPElement expiresTimestampElem = factory.createElement("Expires", utility_prefix,
                        utility_uri);
                expiresTimestampElem.addTextNode(expires);

                timestampElem.addChildElement(createdTimestampElem);
                timestampElem.addChildElement(expiresTimestampElem);
                securityElem.addChildElement(timestampElem);

                SOAPElement tokenElem = securityElem.addChildElement("UsernameToken", prefix);
                tokenElem.addAttribute(QName.valueOf("wsu:Id"),
                        "uuid-" + UUID.randomUUID().toString());

                SOAPElement userElem = factory.createElement("Username", prefix, uri);
                userElem.addTextNode(username);

                SOAPElement pwdElem = factory.createElement("Password", prefix, uri);
                pwdElem.addTextNode(getSecurePassword(password, salt.getBytes()));
                pwdElem.addAttribute(QName.valueOf("Type"),
                        "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");

                tokenElem.addChildElement(userElem);
                tokenElem.addChildElement(pwdElem);
                securityElem.addChildElement(tokenElem);
                header.addChildElement(securityElem);


                SOAPMessage msg = context.getMessage();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                msg.writeTo(out);
                String strMsg = new String(out.toByteArray());
                System.out.println(strMsg);

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else
        {
            try
            {
                // does nothing
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        return true;
    }

最后,这是生成的SOAP消息的示例:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
    <S:Header xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" S:mustUnderstand="1">
            <wsu:Timestamp wsu:Id="_0">
                <wsu:Created>2016-08-12T20:37:42.282Z</wsu:Created>
                <wsu:Expires>2016-08-12T20:38:42.282Z</wsu:Expires>
            </wsu:Timestamp>
            <wsse:UsernameToken wsu:Id="uuid-a5eb8c04-b9d0-4035-acd5-50fae8254d4a">
                <wsse:Username>XXXXXXXX</wsse:Username>
                <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">XXXXXXXX</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </S:Header>
    <soap:Body>
        <ValidateXmlFile xmlns="http://filesubmissionservice.fset.rsi" xmlns:ns2="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:ns3="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Service.FileSubmissionService" xmlns:ns4="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Core" xmlns:ns5="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:ns6="http://schemas.datacontract.org/2004/07/RSI.StateGateway.Plugin">
            <xmlContents>A</xmlContents>
            <submissionType>NC3</submissionType>
            <schemaVersion>FSETStateAnnualFilingV5.2</schemaVersion>
        </ValidateXmlFile>
    </soap:Body>
</soap:Envelope>

问题是我不断收到以下错误消息:

 org.apache.cxf.transport.http.HTTPException: HTTP response '400: Bad Request' when communicating with https://10.1.2.166/FileSubmissionService/FileSubmissionService.svc

当我尝试删除标题注入和/或不设置或设置请求上下文用户名/密码变量时,我也收到上述消息。

我不明白的是A)当我不使用apache CXF时,为什么我得到与apache CXF相关的任何错误?B)为什么这是一个错误的请求?

有人能指出我正确的方向吗?我只想用vanilla java来创建客户端。我不确定我是否需要任何第三方工具包来执行此操作。

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,这就是原因。显然拦截消息并手动设置标头并在请求上下文中设置用户名和密码是JAX-WS中两种完全不同的方法,可以完成同样的事情。

以下文章解释了两者之间的区别: Difference between RequestContext and MessageContext in JAX WS

我正在做的是使用这两种方法,这导致了一个格式错误的请求并导致web服务呕吐并返回HTTP 400代码。

没有捕到的原因是我在处理程序链中记录了SOAP消息(手动方法),但是在修改请求上下文后没有打印最终的外发消息。

如果有人能够进一步阐述这两种不同的方法,我将永远感激不尽。我仍然不完全理解这里的差异,我的结论可能是错的