jdk已经实现了soap webservice,其实是可以不用CXF等第三方框架的,在网上看到很多soap的认证都是基于CXF做的,有时候我们可能并不想引用这么多框架,下面是直接用jdk实现的认证
package cn.com.webxml;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.Node;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.PortInfo;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
/**
* <a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a>
* <strong>2400多个城市天气预报Web服务</strong>
* ,包含2300个以上中国城市和100个以上国外城市天气预报数据。数据每2.5小时左右自动更新一次,准确可靠。<br />
* 使用本站 WEB 服务请注明或链接本站:
* <a href="http://www.webxml.com.cn/" target="_blank">http://www.webxml.com.cn/
* </a> 感谢大家的支持!<br />
* <br />
* <img alt="PDF" title=
* "PDF file" src="http://www.webxml.com.cn/images/icon/pdf.gif" style=
* "vertical-align: middle;" /> <a href=
* "http://www.webxml.com.cn/files/WeatherWsHelp.pdf" target="_blank">接口帮助文档</a>
* <img alt="ZIP" title=
* "ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style=
* "vertical-align: middle;" /> <a href=
* "http://www.webxml.com.cn/files/about_city.zip">部分城市介绍和气候背景</a>
* <img alt="ZIP" title=
* "ZIP file" src="http://www.webxml.com.cn/images/icon/zip.gif" style=
* "vertical-align: middle;" /> <a href=
* "http://www.webxml.com.cn/files/city_photo.zip">部分城市图片</a>
* <img alt="HTML" title=
* "HTML file" src="http://www.webxml.com.cn/images/icon/html.gif" style=
* "vertical-align: middle;" /> <a href=
* "http://www.webxml.com.cn/zh_cn/weather_icon.aspx" target="_blank">天气现象和图例
* </a><br />
*
*
* This class was generated by Apache CXF 3.1.2 2015-08-28T12:29:28.130+08:00
* Generated source version: 3.1.2
*
*/
@WebServiceClient(name = "WeatherWS", wsdlLocation = "http://www.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL", targetNamespace = "http://WebXml.com.cn/")
public class WeatherWS extends Service {
public final static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://WebXml.com.cn/", "WeatherWS");
public final static QName WeatherWSSoap12 = new QName("http://WebXml.com.cn/", "WeatherWSSoap12");
public final static QName WeatherWSSoap = new QName("http://WebXml.com.cn/", "WeatherWSSoap");
public final static QName WeatherWSHttpPost = new QName("http://WebXml.com.cn/", "WeatherWSHttpPost");
public final static QName WeatherWSHttpGet = new QName("http://WebXml.com.cn/", "WeatherWSHttpGet");
static {
URL url = null;
try {
url = new URL("http://www.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL");
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(WeatherWS.class.getName()).log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}",
"http://www.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL");
}
WSDL_LOCATION = url;
}
public WeatherWS(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public WeatherWS(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public WeatherWS() {
super(WSDL_LOCATION, SERVICE);
}
public WeatherWS(WebServiceFeature... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public WeatherWS(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, SERVICE, features);
}
public WeatherWS(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return returns WeatherWSSoap
*/
@WebEndpoint(name = "WeatherWSSoap12")
public WeatherWSSoap getWeatherWSSoap12() {
return super.getPort(WeatherWSSoap12, WeatherWSSoap.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure
* on the proxy. Supported features not in the
* <code>features</code> parameter will have their default
* values.
* @return returns WeatherWSSoap
*/
@WebEndpoint(name = "WeatherWSSoap12")
public WeatherWSSoap getWeatherWSSoap12(WebServiceFeature... features) {
return super.getPort(WeatherWSSoap12, WeatherWSSoap.class, features);
}
/**
*
* @return returns WeatherWSSoap
*/
@WebEndpoint(name = "WeatherWSSoap")
public WeatherWSSoap getWeatherWSSoap() {
setHandlerResolver(new HandlerResolver() {
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
List<Handler> handlerList = new ArrayList<Handler>();
handlerList.add(new SOAPHandler<SOAPMessageContext>() {
@Override
public boolean handleMessage(SOAPMessageContext ctx) {
Boolean request_p = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (request_p) {
try {
SOAPMessage msg = ctx.getMessage();
SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
SOAPHeader hdr = env.getHeader();
if (hdr == null)
hdr = env.addHeader();
// 添加认证信息
QName qname_user = new QName("http://com/auth/", "auth");
SOAPHeaderElement helem_user = hdr.addHeaderElement(qname_user);
helem_user.setActor(SOAPConstants.URI_SOAP_1_2_ROLE_NEXT);
// Constants常量类自行补上
helem_user.addTextNode("admin" + "&" + "admin");
msg.saveChanges();
// 把SOAP消息输出到System.out,即控制台
msg.writeTo(System.out);
System.out.println();
return true;
} catch (Exception e) {
e.printStackTrace();
}
} else {
SOAPMessage msg = ctx.getMessage();
// 从SOAP信息中取出认证信息
SOAPEnvelope env;
SOAPHeader hdr = null;
try {
env = msg.getSOAPPart().getEnvelope();
hdr = env.getHeader();
} catch (SOAPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
if (hdr == null) {
// generateSOAPFault(msg,"No message header.");
return false;
}
Iterator it = hdr.extractHeaderElements(SOAPConstants.URI_SOAP_1_2_ROLE_NEXT);
if (it == null || !it.hasNext()) {
// generateSOAPFault(msg,"No header block for
// role next.");
return false;
}
Node next = (Node) it.next();
String value = (next == null) ? null : next.getValue();
if (value == null) {
// generateSOAPFault(msg,"No authentication info
// in header block.");
return false;
}
String auth = value.trim();
String user = auth.split("&")[0];
String pwd = auth.split("&")[1];
return user.equals("admin") && pwd.equals("admin");
}
return false;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
// TODO Auto-generated method stub
return false;
}
@Override
public void close(MessageContext context) {
// TODO Auto-generated method stub
}
@Override
public Set<QName> getHeaders() {
// TODO Auto-generated method stub
return null;
}
});
return handlerList;
}
});
return super.getPort(WeatherWSSoap, WeatherWSSoap.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure
* on the proxy. Supported features not in the
* <code>features</code> parameter will have their default
* values.
* @return returns WeatherWSSoap
*/
@WebEndpoint(name = "WeatherWSSoap")
public WeatherWSSoap getWeatherWSSoap(WebServiceFeature... features) {
return super.getPort(WeatherWSSoap, WeatherWSSoap.class, features);
}
/**
*
* @return returns WeatherWSHttpPost
*/
@WebEndpoint(name = "WeatherWSHttpPost")
public WeatherWSHttpPost getWeatherWSHttpPost() {
return super.getPort(WeatherWSHttpPost, WeatherWSHttpPost.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure
* on the proxy. Supported features not in the
* <code>features</code> parameter will have their default
* values.
* @return returns WeatherWSHttpPost
*/
@WebEndpoint(name = "WeatherWSHttpPost")
public WeatherWSHttpPost getWeatherWSHttpPost(WebServiceFeature... features) {
return super.getPort(WeatherWSHttpPost, WeatherWSHttpPost.class, features);
}
/**
*
* @return returns WeatherWSHttpGet
*/
@WebEndpoint(name = "WeatherWSHttpGet")
public WeatherWSHttpGet getWeatherWSHttpGet() {
return super.getPort(WeatherWSHttpGet, WeatherWSHttpGet.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure
* on the proxy. Supported features not in the
* <code>features</code> parameter will have their default
* values.
* @return returns WeatherWSHttpGet
*/
@WebEndpoint(name = "WeatherWSHttpGet")
public WeatherWSHttpGet getWeatherWSHttpGet(WebServiceFeature... features) {
return super.getPort(WeatherWSHttpGet, WeatherWSHttpGet.class, features);
}
}