根据已给出的webservice 定义,创建服务端接口

1. 使用soapUI 反向出接口定义

输入服务URL地址或选择定义文件

邮件选择生成方式,需要提前配置soapUI的apache CXF路径

这里建议选择生成全部代码

好啦,接下来就可以把反向出的代码复制到项目中啦;代码中可能会有一些注解里面配了原有的包路径,需要改成现在项目中的包,不然启动可能会报错的。

2. pom引用

       <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-bundle</artifactId>
            <version>2.0.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>
                        geronimo-annotation_1.0_spec
                    </artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-stax-api_1.0_spec</artifactId>
                </exclusion>
              </exclusions>
        </dependency>

3. 创建服务

            String address = "http://localhost:8080/test";
            JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
            factory.setAddress(address);
            factory.setServiceClass(XXX.class);
            factory.setServiceBean(new XXX());
            factory.create();

4. 给服务添加身份校验,创建拦截器

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.SOAPException;
import java.util.Base64;

public class AuthenticationInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    private static final Logger logger = LoggerFactory.getLogger(AuthenticationInInterceptor.class);

    private static final String BASIC_PREFIX = "Basic ";
    private static final String USERNAME = "name";
    private static final String PASSWORD = "pwd";

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

    @Override
    public void handleMessage(SoapMessage message) throws Fault {

        HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
        String auth = request.getHeader("Authorization");
        if (auth == null) {
            SOAPException exception = new SOAPException("auth failed, header [Authorization] not exists");
            throw new Fault(exception);
        }
        if (!auth.startsWith(BASIC_PREFIX)) {
            SOAPException exception = new SOAPException("auth failed, header [Authorization] is illegal");
            throw new Fault(exception);
        }
        String plaintext = new String(Base64.getDecoder().decode(auth.substring(BASIC_PREFIX.length())));
        if (StringUtils.isEmpty(plaintext) || !plaintext.contains(":")) {
            SOAPException exception = new SOAPException("auth failed, header [Authorization] is illegal");
            throw new Fault(exception);
        }
        String[] userAndPass = plaintext.split(":");
        String username = userAndPass[0];
        String password = userAndPass[1];
        if (!USERNAME.equals(username) || !PASSWORD.equals(password)) {
            SOAPException exception = new SOAPException("auth failed, username or password is incorrect");
            throw new Fault(exception);
        }
    }

}

创建服务时,加入拦截器

            String address = "http://localhost:8080/test";
            JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
            factory.setAddress(address);
            factory.setServiceClass(XXX.class);
            factory.setServiceBean(new XXX());
            factory.getInInterceptors().add(new AuthenticationInInterceptor()); //拦截器
            factory.create();
soapui 发起带身份校验的请求
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,148评论 1 32
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,186评论 6 13
  • 人间四月天,花事渐了,茶事正欢 -----普茶16班第四...
    Selina与Lucy阅读 958评论 0 4
  • 世界上那么多人 偏偏遇见了你 不愿相信是天意 因为走了这么远才遇见你 世界上寥寥数人 偏偏弄丢了你 定是上天的旨意...
    蒋猛阅读 544评论 2 3
  • 在佛山兼职接近一个礼拜左右,工作是餐饮类。工作过程中遇到令人无法理解的事情、开心的事情等等。有时客人点单,...
    SuperAilin阅读 251评论 0 0