【问题标题】:Spring SOAP WebService-Masking elements of XML Request payload in Log日志中 XML 请求有效负载的 Spring SOAP WebService-Masking 元素
【发布时间】:2021-02-07 01:26:25
【问题描述】:

作为安全要求的一部分,我需要在使用 log4j 记录 XML SOAP 请求负载时屏蔽敏感字段值,例如信用卡号等。 目前我正在使用以下代码来记录 XML 请求负载:

public void printDebugXMLPayload(MyWSRequest request) throws JAXBException
{
     StringWriter sw = new StringWriter();
    JAXBContext jaxbContext = JAXBContext.newInstance(MyWSRequest .class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(request,  new StreamResult(sw));
    logger.debug(sw.toString());
  
}

上面的代码记录了完整的 XML 请求负载,但我需要屏蔽敏感字段。 能否请您指导。非常感谢! 注意:我在 @XmlElement 级别找不到任何配置来屏蔽字段

【问题讨论】:

    标签: java xml spring soap jaxb


    【解决方案1】:

    一种方法是创建自定义注释并使用反射类来查看是否需要屏蔽字段。 这是一个快速示例。

    @Retention(RetentionPolicy.RUNTIME)
    public @interface Mask {
    }
    
    public class MyWSRequest {
        @Mask
        private String item1;
    }
    
    public void printDebugXMLPayload(MyWSRequest request) throws JAXBException {
        for (Field f : request.getClass().getDeclaredFields()) {
            if (f.isAnnotationPresent(Mask.class)) {
                logger.debug("***");
            } else {
                f.setAccessible(true);
                logger.debug(f.get(request));
            }
        } 
    }
    

    【讨论】:

    • 谢谢,请告诉我们 jaxb 中是否有使用 xmlelement 屏蔽字段的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多