java - 带有内容过滤或屏蔽 soap 字段的 CXF 日志记录请求和响应

标签 java logging soap cxf filtering

我想记录来自某个特定端点的所有传入请求和响应,并进行内容过滤。 IE。当我有这样的请求时:

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">
<soap:Body>
  <m:ProcessPhoto xmlns:m="http://www.w3schools.com/photos">
    <m:Name>Apples</m:Name>
    <m:Description>Photo with some apples in it</m:Description>
    <!-- large encoded binary below -->
    <m:Photo>anVzdCBhIHJhbmRvbSB0ZXh0DQpqdXN0IGEgcmFuZG9tIHRleHQNCmp1c3QgYSByYW5kb20gdGV4dA0KanVzdCBhIHJhbmRvbSB0ZXh0DQpqdXN0IGEgcmFuZG9tIHRleHQNCmp1c3QgYSByYW5kb20gdGV4dA0KanVzdCBhIHJhbmRvbSB0ZXh0DQp3b3csIGkgZGlkbid0IHRob3VnaHQgdGhhdCBhbnlvbmUgd291bGQgYmUgaW50ZXJlc3RlZCBpbiBkZWNvZGluZyB0aGlzLiBjb25ncmF0cyE=</m:Photo>
  </m:ProcessPhoto>
</soap:Body>
</soap:Envelope>

我想过滤它,让它在日志中看起来像那样

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope">
<soap:Body>
  <m:ProcessPhoto xmlns:m="http://www.w3schools.com/photos">
    <m:Name>Apples</m:Name>
    <m:Description>Photo with some apples in it</m:Description>
    <m:Photo>hidden</m:Photo>
  </m:ProcessPhoto>
</soap:Body>
</soap:Envelope>

或者完全删除 m:Photo 元素。

我发现 CXF 有一些 LoggingInInterceptor 和 LoggingOutInterceptor,我可以编写我自己的拦截器来执行这些操作。然而,这需要做一些工作,所以我的问题是:您是否知道更好的开箱即用解决方案?

最佳答案

我遇到了类似的问题,我需要在输入请求中屏蔽密码。我对现有的 LogginInterceptor 和覆盖的格式方法做了一个小改动,并添加了我的方法来屏蔽密码。这是一个例子

public class CustomLogInInterceptor extends LoggingInInterceptor {

    @Override
    protected String formatLoggingMessage(LoggingMessage loggingMessage) {

        String str = loggingMessage.toString();

        String output = maskPasswords(str);
        return(output);
    }


    private String maskPasswords(String str) {

                final String[] keys = { "password", "passwords" };
                for (String key : keys) {
                    int beginIndex = 0;
                    int lastIndex = -1;
                    boolean emptyPass = false;
                    while (beginIndex != -1
                            && (beginIndex = StringUtils.indexOfIgnoreCase(str, key,
                                    beginIndex)) > 0) {

                        beginIndex = StringUtils.indexOf(str, ">", beginIndex);
                        if (beginIndex != -1) {
                            char ch = str.charAt(beginIndex - 1);
                            if (ch == '/') {
                                emptyPass = true;
                            }
                            if (!emptyPass) {
                                lastIndex = StringUtils.indexOf(str, "<", beginIndex);
                                if (lastIndex != -1) {
                                    String overlay = "*";
                                    String str2 = StringUtils.substring(str,
                                            beginIndex + 1, lastIndex);
                                    if (str2 != null && str2.length() > 1) {
                                        overlay = StringUtils.rightPad(overlay,
                                                str2.length(), "*");
                                        str = StringUtils.overlay(str, overlay,
                                                beginIndex + 1, lastIndex);
                                    }
                                }
                            }
                            if (emptyPass) {
                                emptyPass = false;
                                lastIndex = beginIndex + 1;
                            } else {
                                if (lastIndex != -1) {
                                    lastIndex = StringUtils
                                            .indexOf(str, ">", lastIndex);
                                }
                            }
                        }
                        beginIndex = lastIndex;
                    }
                }
                return str;

            }

}

并在我的 cxf-bean.xml 中添加了自定义 cxf 日志 bean

<bean id="kpInInterceptor" class="com.kp.util.CustomLogInInterceptor" />

<cxf:bus>
        <cxf:inInterceptors>
            <ref bean="kpInInterceptor" />
        </cxf:inInterceptors>
        <cxf:inFaultInterceptors>
            <ref bean="kpInInterceptor" />
        </cxf:inFaultInterceptors>
</cxf:bus>

注意 我使用 Apache commons-lang3 jar 来处理字符串。同样,您也可以用于响应


更新

使用模式并使用属性使键可配置。

拦截器

public class CustomLogInInterceptor extends LoggingInInterceptor {

    private static final String MASK_PATTERN = "<\\s*{}\\s*>(.*)</\\s*{}\\s*>|<\\s*name\\s*>\\s*{}\\s*</\\s*name\\s*>\\s*<\\s*value\\s*>(.*)<";

    private Pattern pattern;

    private String[] keys;

    public void init() {
        StringBuilder builder = new StringBuilder();
        for (String str : keys) {
            builder.append(MASK_PATTERN.replace("{}", str));
            builder.append("|");
        }
        builder.setLength(builder.length()-1);
        pattern = Pattern.compile(builder.toString(), Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
    }

    public void setKeys(String[] keys) {
        this.keys = keys;
    }


    @Override
    protected String formatLoggingMessage(LoggingMessage loggingMessage) {

        String output = maskPasswords(loggingMessage.toString());
        return(output);
    }


    private String maskPasswords(String str) {

        Matcher matcher = pattern.matcher(str);
        final StringBuilder builder = new StringBuilder(str);
        while (matcher.find()) {

            int group = 1;
            while (group <= matcher.groupCount()) {
                if (matcher.group(group) != null) {
                    for (int i = matcher.start(group); i < matcher.end(group); i++) {
                        builder.setCharAt(i, '*');
                    }
                }
                group++;
            }
        }
        return builder.toString();

    }

}

Bean 创建

<bean id="kpInInterceptor" class="com.kp.util.CustomLogInInterceptor" init-method="init">
    <property name="keys">
        <array value-type="java.lang.String">
            <value>password</value>
            <value>accountId</value>
        </array>
    </property>
</bean>

示例输入

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <hello>adffas</hello>
    <vsdsd>dfsdf</vsdsd>
    <password>sdfsfs</password>
    <sdfsfsf>sdfsfsf</sdfsfsf>
    <password>3434</password>
    <name>password</name>
    <value>sdfsfs</value>
    <password />
    <name>password</name>
    <value />
    <accountId>123456</accountId>
    <hello>
        <inner1>
            <password>
                <password>sdfsfs</password>
            </password>
        </inner>
    </hello>
</test>

和输出

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <hello>adffas</hello>
    <vsdsd>dfsdf</vsdsd>
    <password>******</password>
    <sdfsfsf>sdfsfsf</sdfsfsf>
    <password>****</password>
    <name>password</name>
    <value>******</value>
    <password />
    <name>password</name>
    <value />
    <accountId>******</accountId>
    <hello>
        <inner1>
            <password>
                <password>******</password>
            </password>
        </inner>
    </hello>
</test>

关于java - 带有内容过滤或屏蔽 soap 字段的 CXF 日志记录请求和响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23212313/

相关文章:

PHP好的日志库?

.net - 无法为 SOAP 调用的 SSL/TLS 建立安全通道

c# - 使用 WCF Web 服务发送 1 个包含 100 万个整数的数组等于大超时?

php - 如何在php中解析soap xml?

java - 保存双向多对多

java - 扑克牌开关编程

java - JDBC ResultSet.getString 在结果集异常开始/结束之前/之后

java - log4j2.xml 未在 Eclipse 中加载

java - 如何删除主屏幕上的开关?

ruby - rack 复制每条日志消息