spring集成cxf之SOAP方式

简介

    Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,
像 JAX-WS 。比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然
地和 Spring 进行无缝集成。

1 . 导入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.10</version>
        </dependency>
    </dependencies>

2 . 创建一个helloworld
接口

package com.baibin.demo1;

import javax.jws.WebService;

/**
 * Created by 白彬 on 2017/3/28.
 */
@WebService
public interface HelloService {
    String say(String name);
}

3 .实现类

package com.baibin.demo1;

import org.springframework.stereotype.Service;

/**
 * Created by 白彬 on 2017/3/28.
 */
@Service
public class HelloServiceImpl implements HelloService {
    public String say(String name) {
        return "hello " + name;
    }
}

4 . 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <!--spring-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
</web-app>
    说明:这里路径中带有ws的请求交给cxf来进行管理

5 . spring配置

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <context:component-scan base-package="com.baibin"></context:component-scan>
    <import resource="spring-cxf.xml"></import>
</beans>
    说明:context:component-scan来扫描包里的注解,然后导入spring-cxf.xml,这个xml文件是为了让spring的配置更
简洁,所以我们不全都放在一块。

6 . spring-cxf.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <!--通过cxf提供的jaxws命令空间来发布webService,注意的是address属性和我们的最终处理实现类helloServiceImpl-->
    <jaxws:server id="helloService" address="/soap/hello">
        <jaxws:serviceBean>
            <ref bean="helloServiceImpl"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>
    这个时候启动服务器,然后在http://localhost:8080/ws就可以在控制台见到我们发布的webservice,这是使用soap
的方式发布的项目,使用者需要得到wsdl文件描述,然后在本地生成webservice客户端来使用。

7 . 加密

    为了提高安全性,我们加入身份验证的功能。
    在我们的pom.xml文件中引入依赖
    <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-ws-security</artifactId>
         <version>3.1.10</version>
    </dependency>
    spring-cxf.xml中加入拦截器


    ```
     <bean id="wss4JInInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
            <constructor-arg>
                <map>
                    <entry key="action" value="UsernameToken"></entry>
                    <entry key="passwordType" value="PasswordText"></entry>
                    <entry key="passwordCallbackRef" value-ref="serverPasswordCallback"></entry>
                </map>
            </constructor-arg>
        </bean>
    ```
    然后在<jaxws:server>标签中注入wss4JInInterceptor,方式如下:
         <jaxws:inInterceptors>
            <ref bean="wss4JInInterceptor"></ref>
        </jaxws:inInterceptors>
    这样就可以实现安全控制了。

猜你喜欢

转载自blog.csdn.net/baibinboss/article/details/67637455