2016-08-12 112 views
0

我得到各种各样的从“400坏请求”到“不正确的用户名/密码”和“没有用户名”和“没有密码或回调处理程序可用“时尝试将代码生成的Java客户端连接到启用了WS-Security的.NET soap服务。无法连接java肥皂客户端启用Ws安全NET Web服务

以下是.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; 
} 

而以下是我的“头处理程序”,我已经把它贴安全标头:

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

和F inally这里是生成的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)为什么这是一个不好的要求?

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

谢谢!

+0

因为我真的需要一个答案,我会开始一个赏金,但是我不能这样做,直到我的声望是75? 我对纯java soap服务和axis,axis 2,cxf,spring和其他实现之间的实现差异感到困惑。我遵循WS-Security规范,正如你所看到的,我已经添加了正确的头文件并提供了预期的输出。为什么我会得到一个'不好的请求'并不是我能找到答案的东西。 – Matt1776

回答

0

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

下面的文章解释之间的区别之二: Difference between RequestContext and MessageContext in JAX WS

我是用这两种方法,这导致在被恶意形成的请求,并引起了web服务想吐,并返回一个HTTP做400码。

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

如果有人能进一步阐述这两种不同的方法,我会永远感激。我仍然不完全明白这里的区别,我的结论可能是错误的