简体   繁体   English

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

[英]Trouble connecting java soap client to Ws-Security enabled .NET webservice

I am getting a variety of wild and wonderful errors ranging from "400 bad request" to "incorrect username/password" and "no username" and "no password or callback handler available" when trying to connect a code generated java client to a .NET soap service that has WS-Security enabled. 当我尝试将代码生成的Java客户端连接到时,遇到了各种各样的错误,从“ 400错误的请求”到“错误的用户名/密码”,“没有用户名”和“没有可用的密码或回调处理程序”。已启用WS-Security的.NET soap服务。

The following is the WSDL for the .NET service: 以下是.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>

And the following is where I'm calling the code generated java client and adding the headers: 以下是我调用代码生成的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;
}

And the following is my "header handler" where I've attached the security headers: 以下是我连接了安全标头的“标头处理程序”:

   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;
    }

And finally here is a sample of the generated SOAP message: 最后是生成的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>

The problem is I keep getting the following error message: 问题是我不断收到以下错误消息:

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

I am also getting the above messages when I try playing around with removing the header injection and or not setting or setting the request context username / password variables. 当我尝试删除标头注入和/或未设置请求上下文的用户名/密码变量时,我也收到上述消息。

What I do not understand is A) why am i getting any error related to apache CXF when I am not using apache CXF and B) why is it a bad request? 我不明白的是:A)为什么我不使用apache CXF时会出现与apache CXF相关的任何错误,以及B)为什么要求不正确?

Can someone point me in the right direction? 有人可以指出我正确的方向吗? I simply want to use vanilla java to create the client. 我只想使用香草java创建客户端。 I am not certain that I need any 3rd party toolkits to do this. 我不确定我是否需要任何第三方工具。

Thank you! 谢谢!

Okay so here is the reason. 好的,这就是原因。 Apparently intercepting the message and manually setting the headers and setting the username and password in the request context are two completely different approaches in JAX-WS that accomplish the same thing. 显然,拦截消息并在请求上下文中手动设置标题以及设置用户名和密码是完成同一件事的JAX-WS中的两种完全不同的方法。

The following post explains the difference between the two: Difference between RequestContext and MessageContext in JAX WS 以下文章解释了两者之间的区别: JAX WS中的RequestContext和MessageContext之间的区别

What I was doing was using both methods, which resulted in a request that was mal-formed and caused the webservice to puke and return a HTTP 400 code. 我正在做的是同时使用这两种方法,这导致请求的格式不正确,导致网络服务呕吐并返回HTTP 400代码。

The reason this was not caught is that I was logging the SOAP message in the handler chain (the manual method) but was not printing the final outgoing message after the request context had been modified. 未被捕获的原因是我在处理程序链中记录了SOAP消息(手动方法),但是在修改了请求上下文之后没有打印最终的传出消息。

If someone can elaborate further on these two different approaches I would be forever thankful. 如果有人可以进一步阐述这两种不同的方法,我将永远感激不已。 I still do not totally understand the difference here and I could be wrong in my conclusions 我仍然不完全理解这里的区别,我的结论可能是错误的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM