在开发的时候发布webservice,为了安全通常需要安全验证,在CXF中的实现,在这里记录一下。
CXF是啥 我就不介绍了, 开发CXF+Spring的webservice服务:
在这里发布一个简单的服务,比如发布的服务为SpingService
import javax.jws.WebService;
@WebService
public interface SpringService {
String play(String info);
}
具体的实现类:
写道
import javax.jws.WebService;
@WebService(endpointInterface="cn.jd.ws.SpringService")
public class DotaSpringService implements SpringService{
public String play(String info) {
System.out.println("play called!");
return "Dota [ " + info + " ]";
}
}
Spring中的配置:
写道
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
">
需要插入jaxws这个库
<import resource="classpath:meta-inf/cxf/cxf.xml"/>
<import resource="classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:meta-inf/cxf/cxf-servlet.xml"/>
在spring中发布服务:
把我们刚才实现的服务发布出来:
<jaxws:endpoint id="dotaService" implementor="cn.jd.ws.DotaSpringService" address="/DotaService">
<jaxws:inInterceptors>
<ref bean="soapAuth"/>
</jaxws:inInterceptors>
</jaxws:endpoint>
参数implementor指定这个发布出来的WebService服务的实现类是哪个,address表示访问的地址,其中的拦截器就是我们需要将的那个安全验证的拦截器。稍后会介绍到。
可能通过代码提示功能的我们都发现了还有一个发布服务的标签:
<jaxws:server id="" serviceClass="cn.jd.ws.DotaSpringService" address="/DataService">
<jaxws:inInterceptors>
<ref bean="soapAuth"/>
</jaxws:inInterceptors>
</jaxws:server>
那么endpoint和server这两种方式有啥区别?
其实这两种方式就是刚学习发布第一个Webservice时可能编写的那两种方式的替换。
比如我们不通过Spring来简单发布一个Webservie,我们会这么做:
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HiService {
//to make sure the paramter is named correctly in the xml
String sayHi(@WebParam(name="text") String text);
}
实现:
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.jaxws.JaxWsServerfactorybean;
/**
* A simple JAX-WS 规范的XML web services的JAVA API
*/
@WebService(endpointInterface="cn.jd.ws.HiService",serviceName="HiService")
public class HiServiceImpl implements HiService{
public static final String ENDPOINT = "http://localhost:9090/HiSerivice";
@Override
public String sayHi(String text) {
return "Hi " + text;
}
//这种方式就相当于是jaxws:endpoint
public void startServer() {
System.out.println("Starting the server");
HiServiceImpl hiService = new HiServiceImpl();
//通过Endpoint的方式直接就发布了
Endpoint.publish(ENDPOINT,hiService);
}
//这种方式就是jaxws:service方式
public void startServernormal() {
//这里利用的就是JaxWsServerfactorybean
JaxWsServerfactorybean serverfactorybean = new JaxWsServerfactorybean();
HiServiceImpl hiService = new HiServiceImpl();
//服务类接口
serverfactorybean.setServiceClass(HiService.class);
//设置地址
serverfactorybean.setAddress(ENDPOINT);
serverfactorybean.setServiceBean(hiService);
serverfactorybean.getininterceptors().add(new LoggingInInterceptor());
serverfactorybean.create();
}
//发布服务
public static void main(String[] args) {
try {
//能通过浏览器看到 是因为cxf自带了一个jetty服务器
new HiServiceImpl().startServernormal();
System.out.println("发布服务成功");
}catch (Exception e) {
System.out.println("发布服务失败");
}
}
}