大侠们,有木有写过基于spring-ws2的web services啊,现在我的demo已经写好了,但是要加入spring security,急急急急急急急急急
大侠们,有木有写过基于spring-ws2的web services啊,现在我的demo已经写好了,但是要加入spring security,官方文档不足,主要是Wss4jSecurityInterceptor拦截器的配置属性不熟悉,而且还要加入*.keystore文件,我不知道这个文件是如何产生的,该如何定义.哪位大侠有没有现成的demo啊,有的话,麻烦传我一份:shzhengzhangwen@163.com,感激不尽.我的demo如下:
1,spring-ws-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="payloadMapping"
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="endpointMap">
<map>
<entry
key="{http://www.ispring.com/ws/hello}eRequest">
<ref bean="helloEndpoint" />
</entry>
</map>
</property>
<!-- -->
<property name="interceptors">
<list>
<ref bean="wss4jSecurityInterceptor" />
</list>
</property>
</bean>
<!-- WSS Security interceptor - encrypt entire body of SOAP response -->
<bean id="wss4jSecurityInterceptor"
class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<!-- Validation part -->
<property name="validateRequest" value="true" />
<property name="validationActions"
value="Signature Encrypt Timestamp" />
<property name="validationSignatureCrypto" ref="serverCrypto" />
<property name="validationDecryptionCrypto" ref="serverCrypto" />
<property name="validationCallbackHandler"
ref="validationCallbackHandler" />
<!-- Sign the response -->
<property name="secureResponse" value="true" />
<property name="securementActions"
value="Timestamp Signature Encrypt" />
<property name="securementSignatureKeyIdentifier"
value="DirectReference" />
<property name="securementUsername" value="myName" />
<property name="securementPassword" value="123456" />
<property name="securementSignatureCrypto" ref="serverCrypto" />
<property name="securementEncryptionUser" value="myName" />
<property name="securementEncryptionKeyIdentifier"
value="Thumbprint" />
<property name="securementEncryptionCrypto" ref="serverCrypto" />
</bean>
<bean id="serverCrypto"
class="org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean">
<property name="keyStorePassword" value="123456" />
<property name="keyStoreLocation"
value="/WEB-INF/crypto.keystore" />
</bean>
<bean id="validationCallbackHandler"
class="org.springframework.ws.soap.security.wss4j.callback.KeyStoreCallbackHandler">
<property name="privateKeyPassword" value="123456" />
</bean>
<bean id="helloEndpoint" class="com.sws.HelloEndPoint">
<property name="helloService" ref="helloService"></property>
</bean>
<bean id="hello"
class="org.springframework.ws.wsdl.wsdl11.SimpleWsdl11Definition">
<!-- -->
<property name="wsdl" value="classpath://hello.wsdl"></property>
<!-- <property name="wsdl" value="/WEB_INF/hello.wsdl"></property> -->
<!-- <constructor-arg value="/WEB_INF/hello.wsdl"/> -->
</bean>
<bean id="helloService" class="com.sws.HelloServiceImpl"></bean>
</beans>
2.hello.wsdl:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:schema="http://www.ispring.com/ws/hello"
xmlns:tns="http://www.ispring.com/ws/hello/definitions"
targetNamespace="http://www.ispring.com/ws/hello/definitions">
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ispring.com/ws/hello">
<element name="dRequest" type="string" />
<element name="dResponse" type="string" />
</schema>
</wsdl:types>
<wsdl:message name="bRequest">
<wsdl:part element="schema:dRequest" name="cRequest" />
</wsdl:message>
<wsdl:message name="bResponse">
<wsdl:part element="schema:dResponse" name="cResponse" />
</wsdl:message>
<wsdl:portType name="HelloPortType">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:bRequest" name="aRequest" />
<wsdl:output message="tns:bResponse" name="aResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloBinding" type="tns:HelloPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHello">
<soap:operation soapAction="" />
<wsdl:input name="aRequest">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="aResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloService">
<wsdl:port binding="tns:HelloBinding" name="HelloPort">
<soap:address location="http://localhost:8080/springws/webservice" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
HelloServiceImpl:
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
HelloEndPoint:
package com.sws;
import org.springframework.util.Assert;
import org.springframework.ws.server.endpoint.AbstractDomPayloadEndpoint;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class HelloEndPoint extends AbstractDomPayloadEndpoint{
/**
* Namespace of both request and response
*/
public static final String NAMESPACE_URI = "http://www.ispring.com/ws/hello";
/**
* The local name of the expected request
*/
public static final String HELLO_REQUEST_LOCAL_NAME = "eRequest";
/**
* The local name of the created response
*/
public static final String HELLO_RESPONSE_LOCAL_NAME = "fResponse";
private HelloService helloService;
@Override
@SuppressWarnings("unchecked")
protected Element invokeInternal(Element requestElement, Document document)throws Exception {
Assert.isTrue(NAMESPACE_URI.equals(requestElement.getNamespaceURI()), "Invalid namespace");
Assert.isTrue(HELLO_REQUEST_LOCAL_NAME.equals(requestElement.getLocalName()), "Invalid local name");
NodeList children = requestElement.getChildNodes();
Text requestText = null;
for(int i=0; i<children.getLength(); i++){
if(children.item(i).getNodeType() == Node.TEXT_NODE){
requestText = (Text) children.item(i);
}
}
if(requestText == null){
throw new IllegalArgumentException("Could not find request text node");
}
else
{
System.out.println("requestText:"+requestText.getWholeText());
System.out.println("requestText.getNodeValue():"+requestText.getNodeValue());
}
String response = helloService.sayHello(requestText.getNodeValue());
Element responseElement = document.createElementNS(NAMESPACE_URI, HELLO_RESPONSE_LOCAL_NAME);
Text responseText = document.createTextNode(response);
responseElement.appendChild(responseText);
return responseElement;
}
public HelloService getHelloService() {
return helloService;
}
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
}
HelloWebServiceClient:
spring spring ws2 spring ws security --------------------编程问答-------------------- HelloWebServiceClient:
package com.sws;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class HelloWebServiceClient {
// 命名空间,在组装SOAP消息中用到
public static final String NAMESPACE_URI = "http://www.ispring.com/ws/hello";
// SOAP标签前缀
public static final String PREFIX = "tns";
// 请求连接
private SOAPConnectionFactory connectionFactory;
// 消息工厂
private MessageFactory messageFactory;
// 服务端请求地址
private URL url;
// 构造函数,初始化soap参数,主要是初始化连接
public HelloWebServiceClient(String url)
throws UnsupportedOperationException, SOAPException,
MalformedURLException {
connectionFactory = SOAPConnectionFactory.newInstance();
messageFactory = MessageFactory.newInstance();
this.url = new URL(url);
}
// 构建soap传到server
private SOAPMessage createHelloRequest() throws SOAPException {
// 用工厂创建一个SOAP message
SOAPMessage message = messageFactory.createMessage();
// 构建创建soap消息主体
// // 创建soap部分
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
Name helloRequestName = envelope.createName("eRequest", PREFIX,
NAMESPACE_URI);
// envelope.setAttribute("xmlns:soap",
// "http://www.w3.org/2001/12/soap-envelope");
// envelope.setAttribute("soap:encodingStyle",
// "http://www.w3.org/2001/12/soap-encoding");
// soap的Header
SOAPHeader soapheader = message.getSOAPHeader();
SOAPHeaderElement helloRequestHeaderElement = soapheader
.addHeaderElement(helloRequestName);
helloRequestHeaderElement.setValue("helloRequestHeaderElement");
// soap的body
SOAPBody soapBody = message.getSOAPBody();
SOAPBodyElement helloRequestBodyElement = soapBody
.addBodyElement(helloRequestName);
// 向服务端传值过去
helloRequestBodyElement.setValue("testSpring-ws2.1.2");
return message;
}
public void callWebService() throws SOAPException {
SOAPMessage request = createHelloRequest();
System.out.println("\n====================发送的消息:");
try {
request.writeTo(System.out);
} catch (IOException e) {
e.printStackTrace();
}
SOAPConnection connection = connectionFactory.createConnection();
// 向服务端发送消息
SOAPMessage response = connection.call(request, url);
System.out.println("\n====================响应消息:");
try {
response.writeTo(System.out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(response.getSOAPBody().getValue());
if (!response.getSOAPBody().hasFault()) {
writeHelloResponse(response);
} else {
SOAPFault fault = response.getSOAPBody().getFault();
System.err.println("Received SOAP Fault");
System.err.println("SOAP Fault Code : " + fault.getFaultCode());
System.err.println("SOAP Fault String : " + fault.getFaultString());
}
}
private void writeHelloResponse(SOAPMessage message) throws SOAPException {
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
Name helloResponseName = envelope.createName("fResponse", PREFIX,
NAMESPACE_URI);
Iterator childElements = message.getSOAPBody().getChildElements(
helloResponseName);
// 这里报异常java.util.NoSuchElementException
if (childElements.hasNext()) {
SOAPBodyElement helloResponseElement = (SOAPBodyElement) childElements
.next();
String value = helloResponseElement.getTextContent();
System.out.println("接收服务端数据: [" + value + "]");
}
}
public static void main(String[] args)
throws UnsupportedOperationException, MalformedURLException,
SOAPException {
String url = "http://localhost:8080/springws/webservice";
HelloWebServiceClient helloClient = new HelloWebServiceClient(url);
helloClient.callWebService();
}
} --------------------编程问答-------------------- 请问:楼主现在搞定了没有啊,目前自己也在学习,嘿嘿,求分享!
补充:Java , Java EE