Java 如何在soap头中创建需要用户名/密码的soap服务

Java 如何在soap头中创建需要用户名/密码的soap服务,java,web-services,soap,wsdl,Java,Web Services,Soap,Wsdl,请让我澄清我的要求。以下是我知道/学到的相关要求 添加ws-security标头。(我是使用ApacheRampart完成的。使用UsernameToken进行身份验证) 访问web服务的容器级安全约束(我在web.xml中使用了必需的配置) 用户名/密码绑定为Http请求头的应用程序级别。 以上三种方式我都不想要。我的要求是添加带有用户名/密码的自定义标题元素。对于我们的一个客户,我遇到了这个要求。他们的wsdl部分如下 <s:element name="Authenticatio

请让我澄清我的要求。以下是我知道/学到的相关要求

  • 添加ws-security标头。(我是使用ApacheRampart完成的。使用UsernameToken进行身份验证)
  • 访问web服务的容器级安全约束(我在web.xml中使用了必需的配置)
  • 用户名/密码绑定为Http请求头的应用程序级别。
  • 以上三种方式我都不想要。我的要求是添加带有用户名/密码的自定义标题元素。对于我们的一个客户,我遇到了这个要求。他们的wsdl部分如下

    <s:element name="AuthenticationHeader" type="tns:AuthenticationHeader" />
          <s:complexType name="AuthenticationHeader">
            <s:sequence>
              <s:element minOccurs="0" maxOccurs="1" name="UserName" type="s:string" />
              <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
            </s:sequence>
            <s:anyAttribute />
          </s:complexType>
    
    
    
    前缀s的xmlns=

    在客户端,我通过在soap头中提供上述复杂类型元素来使用该服务

    现在我想知道如何创建一个soap服务,它将接受如上所述的soap头


    提前感谢

    另外,我在提供的XSD中看到了一些可能更好的东西。可以为复杂类型赋予全局元素不同的名称

    <s:element name="AuthenticationHeader" type="tns:AuthenticationHeaderType" />
          <s:complexType name="AuthenticationHeaderType">
            <s:sequence>
              <s:element minOccurs="0" maxOccurs="1" name="UserName" type="s:string" />
              <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
            </s:sequence>
            <s:anyAttribute />
          </s:complexType>
    
    将上述authenticationHeader对象与webservice请求对象一起附加


    您的示例将基于我从您提供的块中准备的xsd创建以下类

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified" elementFormDefault="unqualified">
    <xs:element name="AuthenticationHeader" type="AuthenticationHeader" />
          <xs:complexType name="AuthenticationHeader">
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="1" name="UserName" type="xs:string" />
              <xs:element minOccurs="0" maxOccurs="1" name="Password" type="xs:string" />
            </xs:sequence>
            <xs:anyAttribute />
          </xs:complexType>
          </xs:schema>
    

    这个类AuthenticationHeaderType是什么?它是ws-library的一部分吗?您好,我看到您对复杂类型和全局元素使用了相同的名称。请尝试为上述xsd生成代码并尝试设置对象。请检查实现。我希望你的样品现在一切都清楚了?还有一件事。如何将其附加到soap服务头。在我的impl类中,我对overidden方法公共字符串getHelloWorldAsObject(@WebParam(name=“HelloObject”)字符串名称){return”name Passed:“+new HelloMessage(name).getName();}采用了以下方法,您已经添加了它。检查一下。我会尝试,但我认为这会有帮助,我可以继续
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified" elementFormDefault="unqualified">
    <xs:element name="AuthenticationHeader" type="AuthenticationHeader" />
          <xs:complexType name="AuthenticationHeader">
            <xs:sequence>
              <xs:element minOccurs="0" maxOccurs="1" name="UserName" type="xs:string" />
              <xs:element minOccurs="0" maxOccurs="1" name="Password" type="xs:string" />
            </xs:sequence>
            <xs:anyAttribute />
          </xs:complexType>
          </xs:schema>
    
    import javax.xml.bind.JAXBElement;
    import javax.xml.bind.annotation.XmlElementDecl;
    import javax.xml.bind.annotation.XmlRegistry;
    import javax.xml.namespace.QName;
    
    
    /**
     * This object contains factory methods for each 
     * Java content interface and Java element interface 
     * generated in the com.auth package. 
     * <p>An ObjectFactory allows you to programatically 
     * construct new instances of the Java representation 
     * for XML content. The Java representation of XML 
     * content can consist of schema derived interfaces 
     * and classes representing the binding of schema 
     * type definitions, element declarations and model 
     * groups.  Factory methods for each of these are 
     * provided in this class.
     * 
     */
    @XmlRegistry
    public class ObjectFactory {
    
        private final static QName _AuthenticationHeader_QNAME = new QName("", "AuthenticationHeader");
    
        /**
         * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.auth
         * 
         */
        public ObjectFactory() {
        }
    
        /**
         * Create an instance of {@link AuthenticationHeader }
         * 
         */
        public AuthenticationHeader createAuthenticationHeader() {
            return new AuthenticationHeader();
        }
    
        /**
         * Create an instance of {@link JAXBElement }{@code <}{@link AuthenticationHeader }{@code >}}
         * 
         */
        @XmlElementDecl(namespace = "", name = "AuthenticationHeader")
        public JAXBElement<AuthenticationHeader> createAuthenticationHeader(AuthenticationHeader value) {
            return new JAXBElement<AuthenticationHeader>(_AuthenticationHeader_QNAME, AuthenticationHeader.class, null, value);
        }
    
    }
    
    import java.util.HashMap;
    import java.util.Map;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAnyAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;
    import javax.xml.namespace.QName;
    
    
    /**
     * <p>Java class for AuthenticationHeader complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * &lt;complexType name="AuthenticationHeader">
     *   &lt;complexContent>
     *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       &lt;sequence>
     *         &lt;element name="UserName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
     *         &lt;element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
     *       &lt;/sequence>
     *       &lt;anyAttribute/>
     *     &lt;/restriction>
     *   &lt;/complexContent>
     * &lt;/complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "AuthenticationHeader", propOrder = {
        "userName",
        "password"
    })
    public class AuthenticationHeader {
    
        @XmlElement(name = "UserName")
        protected String userName;
        @XmlElement(name = "Password")
        protected String password;
        @XmlAnyAttribute
        private Map<QName, String> otherAttributes = new HashMap<QName, String>();
    
        /**
         * Gets the value of the userName property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getUserName() {
            return userName;
        }
    
        /**
         * Sets the value of the userName property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setUserName(String value) {
            this.userName = value;
        }
    
        /**
         * Gets the value of the password property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getPassword() {
            return password;
        }
    
        /**
         * Sets the value of the password property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setPassword(String value) {
            this.password = value;
        }
    
        /**
         * Gets a map that contains attributes that aren't bound to any typed property on this class.
         * 
         * <p>
         * the map is keyed by the name of the attribute and 
         * the value is the string value of the attribute.
         * 
         * the map returned by this method is live, and you can add new attribute
         * by updating the map directly. Because of this design, there's no setter.
         * 
         * 
         * @return
         *     always non-null
         */
        public Map<QName, String> getOtherAttributes() {
            return otherAttributes;
        }
    
    }
    
    public class ImplementAuthentication {
    
        void authentication() {
    
            AuthenticationHeader authenticationHeader = new AuthenticationHeader();
    
            authenticationHeader.setPassword("MyPassword");
            authenticationHeader.setUserName("MyUsername");
    
            ObjectFactory obj = new ObjectFactory();
    
            obj.createAuthenticationHeader(authenticationHeader);
        }
    }