码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-24 23:49:34      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:tor   str   public   pre   imp   ring   没有   abs   port   

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();
    //....
  }
}

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

标签:tor   str   public   pre   imp   ring   没有   abs   port   

原文地址:https://www.cnblogs.com/fengfengshaonian/p/8641814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!