如何在 soap Header 中创建需要 username/password 的 soap 服务
How to create a soap service which requires username/password in soap Header
请让我澄清一下我的要求。以下是我 know/learned .
的相关要求
- 添加 ws-security header。 (我是用 apache rampart 做的。使用 UsernameToken 进行身份验证)
http://www.ibm.com/developerworks/library/j-jws4/
- 访问容器级安全约束 web-service(我确实使用了 web.xml 中所需的配置)
http://www.mkyong.com/webservices/jax-ws/container-authentication-with-jax-ws-tomcat/
- username/password 绑定为 Http 请求 header 的应用程序级别。
http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/
以上三种方式我都不想要。我的要求是添加采用 username/password 的自定义 header 元素。对于我们的一位客户,我遇到了这个要求。他们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>
xmlns 前缀 s=http://www.w3.org/2001/XMLSchema
在客户端,我通过在 soap header
中提供上述 complex-type 元素来使用该服务
现在我想知道如何创建一个接受上述 soap-header 的 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>
如果 xsd 如上所述
,则执行如下
AuthenticationHeader authenticationHeader = new AuthenticationHeader();
AuthenticationHeaderType authenticationHeaderType = new AuthenticationHeaderType();
authenticationHeaderType.setUsername("MyUsername");
authenticationHeaderType.setPassword("MyPassword");
authenticationHeader.setAuthenticationHeader(authenticationHeaderType );
附加上面的 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>
生成的类如下
ObjectFactory.java
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);
}
}
和AuthenticationHeader.java
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>
* <complexType name="AuthenticationHeader">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="UserName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* <anyAttribute/>
* </restriction>
* </complexContent>
* </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);
}
}
请让我澄清一下我的要求。以下是我 know/learned .
的相关要求- 添加 ws-security header。 (我是用 apache rampart 做的。使用 UsernameToken 进行身份验证) http://www.ibm.com/developerworks/library/j-jws4/
- 访问容器级安全约束 web-service(我确实使用了 web.xml 中所需的配置) http://www.mkyong.com/webservices/jax-ws/container-authentication-with-jax-ws-tomcat/
- username/password 绑定为 Http 请求 header 的应用程序级别。 http://www.mkyong.com/webservices/jax-ws/application-authentication-with-jax-ws/
以上三种方式我都不想要。我的要求是添加采用 username/password 的自定义 header 元素。对于我们的一位客户,我遇到了这个要求。他们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>
xmlns 前缀 s=http://www.w3.org/2001/XMLSchema
在客户端,我通过在 soap header
中提供上述 complex-type 元素来使用该服务现在我想知道如何创建一个接受上述 soap-header 的 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>
如果 xsd 如上所述
,则执行如下 AuthenticationHeader authenticationHeader = new AuthenticationHeader();
AuthenticationHeaderType authenticationHeaderType = new AuthenticationHeaderType();
authenticationHeaderType.setUsername("MyUsername");
authenticationHeaderType.setPassword("MyPassword");
authenticationHeader.setAuthenticationHeader(authenticationHeaderType );
附加上面的 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>
生成的类如下
ObjectFactory.java
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);
}
}
和AuthenticationHeader.java
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>
* <complexType name="AuthenticationHeader">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="UserName" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="Password" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* <anyAttribute/>
* </restriction>
* </complexContent>
* </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);
}
}