cxf在服务器定义input拦截器功能

1、定义一个自定义的拦截器

public class HelloWorldService {

public static void main(String[] args) {
  HelloWorld hw = new HelloWorldImpl();
  EndpointImpl ep = (EndpointImpl)Endpoint.publish("http://192.168.123.47/ws", hw);
  ep.getInInterceptors().add(new AuthInterceptor());
  System.out.println("web service服务启动成功");
}
}

 

2、自定义拦截器

 

package cn.xjs.util;

import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

  public AuthInterceptor() {
    super(Phase.PRE_INVOKE);
  }

  @Override
  public void handleMessage(SoapMessage mes) throws Fault {
    List<Header> headers = mes.getHeaders();
    if(headers==null || headers.size()<1) {
      throw new Fault(new Exception("没有请求header头信息"));
    }

    Header header = headers.get(0);
    Element ele =(Element)header.getObject();

    NodeList userIds = ele.getElementsByTagName("userId");
    NodeList passList = ele.getElementsByTagName("pass");

    if(userIds.getLength()!=1 ) {
      throw new Fault(new Exception("用户名不对"));
    }
    if(passList.getLength()!=1 ) {
      throw new Fault(new Exception("密码不对"));
    }

    String userId = userIds.item(0).getTextContent();
    String pass= passList.item(0).getTextContent();
    //....
  }
}

转载于:https://www.cnblogs.com/fengfengshaonian/p/8641814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值