Spring Boot 整合CXF添加日志拦截器

Spring Boot 整合CXF添加日志拦截器

经验文章nimo972025-03-18 22:34:4225A+A-

一、前言

上一篇已经完整的描述了spring boot 与cxf整合工程,包括服务端的发布以及客户端的访问。但是整个日志输出只有一个简单的一句:

Bash
o.a.c.w.s.f.ReflectionServiceFactoryBean : Creating Service {http://ws.cxf.ouyushan.org/}UserServiceService from class org.ouyushan.cxf.ws.UserService

表示通过CXF已创建指定服务。但是webservice服务中设计的请求信息一星半点都没有,当服务出现问题时,如何通过获取日志来分析问题呢?

其实在CXF整合完成后,其它附加的高级功能基本上都是通过拦截器来实现的。日志捕获和输出也是基于此实现的。

本文将带领大家快速实现CXF日志输出功能。

项目源码:
https://github.com/ouyushan/spring-webservice-samples

spring-cxf参考源码:
https://github.com/code-not-found/cxf-jaxws

spring-ws参考源码:
https://github.com/code-not-found/spring-ws

二、开发环境

IDE::IDEA

JDK:1.8

maven:3.6.2

spring boot:2.4.0

cxf:3.4.1

三、实现步骤

具体代码实现步骤和上篇一样。本章在之前基础上讲解新增内容。

3.1、实现服务端和客户端

新建服务端和客户端spring-cxf-client-logging、spring-cxf-server-logging,并完成服务部署和客户端访问接口,具体步骤见上一篇。

3.2、添加日志输出依赖

spring boot与cxf整合后输出业务日志只需引入cxf-rt-features-logging,并实现日志拦截器即可。

服务端、客服端pom文件中添加cxf日志依赖包

Bash

    org.apache.cxf
    cxf-rt-features-logging
    3.4.1

3.3、实现日志输出拦截器

在服务端和客户端配置类中,添加请求接入和响应输出日志拦截器

Bash
@Bean
public LoggingInInterceptor loggingInInterceptor() {
    return new LoggingInInterceptor();
}

@Bean
public LoggingOutInterceptor loggingOutInterceptor() {
    return new LoggingOutInterceptor();
}

服务端在Endpoint中配置新增的拦截器

Bash
// log the request and response messages
endpoint.getInInterceptors()
        .add(loggingInInterceptor());
endpoint.getOutInterceptors()
        .add(loggingOutInterceptor());

客户端在UserService中配置新增的拦截器

Bash
// log the request and response messages
jaxWsProxyFactoryBean.getInInterceptors()
        .add(loggingInInterceptor());
jaxWsProxyFactoryBean.getOutInterceptors()
        .add(loggingOutInterceptor());

3.4、功能验证

分别启动服务端和客户端,访问客户端请求接口:

http://localhost:7070/user/get?userId=1

客户端控制台输出如下:

Bash
2020-11-26 20:22:00.884  INFO 8768 --- [nio-7070-exec-1] o.a.cxf.services.UserService.REQ_OUT     : REQ_OUT
    Address: http://localhost:8080/ws/user
    HttpMethod: POST
    Content-Type: text/xml
    ExchangeId: 3140c6ed-a420-4dee-b970-4f9452b17d17
    ServiceName: UserServiceService
    PortName: UserServicePort
    PortTypeName: UserService
    Headers: {SOAPAction="", Accept=*/*}
    Payload: 1

2020-11-26 20:22:01.272  INFO 8768 --- [nio-7070-exec-1] o.a.cxf.services.UserService.RESP_IN     : RESP_IN
    Address: http://localhost:8080/ws/user
    Content-Type: text/xml;charset=UTF-8
    ResponseCode: 200
    ExchangeId: 3140c6ed-a420-4dee-b970-4f9452b17d17
    ServiceName: UserServiceService
    PortName: UserServicePort
    PortTypeName: UserService
    Headers: {Keep-Alive=timeout=60, connection=keep-alive, content-type=text/xml;charset=UTF-8, Content-Length=220, Date=Thu, 26 Nov 2020 12:22:01 GMT}
    Payload: tom

服务端控制台输出如下:

Bash
2020-11-26 20:22:01.057  INFO 10604 --- [nio-8080-exec-4] o.a.cxf.services.UserService.REQ_IN      : REQ_IN
    Address: http://localhost:8080/ws/user
    HttpMethod: POST
    Content-Type: text/xml; charset=UTF-8
    ExchangeId: 3a1a8ecf-f857-4472-9f56-165b54144dae
    ServiceName: UserService
    PortName: UserServiceImplPort
    PortTypeName: UserService
    Headers: {SOAPAction="", Accept=*/*, host=localhost:8080, connection=keep-alive, content-type=text/xml; charset=UTF-8, cache-control=no-cache, Content-Length=202, pragma=no-cache, user-agent=Apache-CXF/3.4.1}
    Payload: 1

2020-11-26 20:22:01.108  INFO 10604 --- [nio-8080-exec-4] o.a.cxf.services.UserService.RESP_OUT    : RESP_OUT
    Address: http://localhost:8080/ws/user
    Content-Type: text/xml
    ResponseCode: 200
    ExchangeId: 3a1a8ecf-f857-4472-9f56-165b54144dae
    ServiceName: UserService
    PortName: UserServiceImplPort
    PortTypeName: UserService
    Headers: {}
    Payload: tom

从日志可以看出完整的请求链路信息

四、总结

通过本篇教材,您应该已掌握Spring Boot整合CXF实现服务端、客户端日志的配置与输出。整个过程可分为两步:

  1. 定义日志输入、输出拦截器;
  2. 在服务端EndPoint中设置日志拦截器/在客户端代理工厂JaxWsProxyFactoryBean中设置日志拦截器。

获取服务日志的问题已解决了,服务就这样能上线吗?尤其是在保险、银行等公司对接时。结果肯定是否定的,针对敏感信息的服务会话,至少得对会话内容进行加解密甚至签名和验签吧。

具体怎么实现会话的加解密、签名及验签将在下一篇教材中进行讲解。

点击这里复制本文地址 以上内容由nimo97整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

尼墨宝库 ©2025 All Rights Reserved.  蜀ICP备2024111239号-7