使用 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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值