当前位置: 代码迷 >> Web前端 >> 应用 CXF 做 webservice 简单例子
  详细解决方案

应用 CXF 做 webservice 简单例子

热度:701   发布时间:2012-10-28 09:54:44.0
使用 CXF 做 webservice 简单例子

applicationContext-cxf.xml:

<!--客户端 俱乐部平台提供接口 -->
??? <jaxws:client id="thumbClubServiceClient" serviceClass="com.bplow.look.ws.service.ThumbClubService" address="http://localhost:8080/cms/services/thumbClubServiceWeb">
??? </jaxws:client>
???
??? <!--服务器端 俱乐部平台提供接口 -->
??? <jaxws:endpoint id="thumbClubServiceEP" address="/thumbClubServiceWeb">
??? ??? <jaxws:implementor ref="thumbClubServiceWeb" />
??? ??? <jaxws:properties>
??? ??? ??? <entry key="mtom-enabled" value="true" />
??? ??? </jaxws:properties>
??? </jaxws:endpoint>
??? <bean id="thumbClubServiceWeb" class="com.bplow.look.ws.service.impl.ThumbClubServiceImpl" />
??? <bean id="thumbClubJdbcDaoWeb" class="com.bplow.look.ws.dao.ThumbClubJdbcDao" parent="baseJdbcDao"/>
??? <bean id="thumbClubHibernateWeb" class="com.bplow.look.ws.dao.ThumbClubHibernate"/>

测试获取bean:

ThumbClubService thumbClubServiceClient =
??? ??? ??? (ThumbClubService)SpringUtils.getBean("thumbClubServiceClient");

?

IP地址拦截器

applicationContext-cxf.xml:

<!--客户端 拇指俱乐部平台提供接口 -->
??? <jaxws:client id="thumbClubServiceClient" serviceClass="com.bplow.look.ws.service.ThumbClubService" address="http://localhost:8080/cms/services/thumbClubServiceWeb">
??? </jaxws:client>
???
??? <!--服务器端 拇指俱乐部平台提供接口 -->
??? <jaxws:endpoint id="thumbClubServiceEP" address="/thumbClubServiceWeb">
??? ??? <jaxws:implementor ref="thumbClubServiceWeb" />
??? ??? <jaxws:inInterceptors>
??? ??? ??? <bean class="com.bplow.look.ws.filter.IpAddressInInterceptor"/>
??? ??? </jaxws:inInterceptors>

??? ??? <jaxws:properties>
??? ??? ??? <entry key="mtom-enabled" value="true" />
??? ??? </jaxws:properties>
??? </jaxws:endpoint>
??? <bean id="thumbClubServiceWeb" class="com.bplow.look.ws.service.impl.ThumbClubServiceImpl" />
??? <bean id="thumbClubJdbcDaoWeb" class="com.bplow.look.ws.dao.ThumbClubJdbcDao" parent="baseJdbcDao"/>
??? <bean id="thumbClubHibernateWeb" class="com.bplow.look.ws.dao.ThumbClubHibernate"/>

?

IpAddressInInterceptor.java:

package com.bplow.look.ws.filter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

?/**?
? * IP地址拦截器?
? * @author wmc?
? *?
? */?
?public class IpAddressInInterceptor extends AbstractPhaseInterceptor<Message> {??
??
???? public IpAddressInInterceptor() {
???????? super(Phase.RECEIVE);
???? }
??
???? public void handleMessage(Message message) {
???????? // 通过一个IpAddressConfig对象,从XML文件中读取预先设置的允许和拒绝的IP地址,这些值也可以来自数据库
???????? List<String> allowedList = IpAddressConfig.getAllowedList(); // 允许访问的IP地址
???????? List<String> deniedList = IpAddressConfig.getDeniedList(); // 拒绝访问的IP地址
???????? HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
???????? String ipAddress = request.getRemoteAddr(); // 取客户端IP地址
???????? System.out.println("ip:?????????????? "+ipAddress);
???????? // 先处理拒绝访问的地址
???????? for (String deniedIpAddress : deniedList) {
???????????? if (deniedIpAddress.equals(ipAddress)) {
???????????????? throw new Fault(new IllegalAccessException("ip地址 " + ipAddress + " 拒绝访问"));
???????????? }
???????? }
???????? // 如果允许访问的集合非空,继续处理,否则认为全部IP地址均合法
???????? if (allowedList.size() > 0) {
???????????? boolean contains = false;
???????????? for (String allowedIpAddress : allowedList) {
???????????????? if (allowedIpAddress.equals(ipAddress)) {
???????????????????? contains = true;
???????????????????? break;
???????????????? }
???????????? }
???????????? if (!contains) {
???????????????? throw new Fault(new IllegalAccessException("ip地址 " + ipAddress + " 不允许访问"));
???????????? }
???????? }
???? }

?}

?

IpAddressConfig.java:

package com.bplow.look.ws.filter;

import java.util.ArrayList;
import java.util.List;

public class IpAddressConfig {
???
??? // 允许访问的IP地址
??? public static List<String> getAllowedList(){
??? ??? List<String> li = new ArrayList<String>();
??? ??? li.add("192.168.1.30");
??? ??? return li;
??? }
???
??? // 拒绝访问的IP地址
??? public static List<String> getDeniedList(){
??? ??? List<String> li = new ArrayList<String>();
??? ??? li.add("127.0.0.1");
??? ??? return li;
??? }

}

  相关解决方案