我有类似的问题,我需要在输入请求中屏蔽密码。我对现有的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中添加了custtom cxf日志记录bean
<bean id="kpInInterceptor" /><cxf:bus> <cxf:inInterceptors> <ref bean="kpInInterceptor" /> </cxf:inInterceptors> <cxf:inFaultInterceptors> <ref bean="kpInInterceptor" /> </cxf:inFaultInterceptors></cxf:bus>
注意 我已经使用Apache commons-lang3 jar来进行String操作。同样,您也可以将其用于响应
更新
使用模式并使用属性使键可配置。
拦截器
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 id="kpInInterceptor" 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>



