【问题标题】:Transforming opensaml Response object to String, modifying it, then back into a response object将 opensaml 响应对象转换为字符串,对其进行修改,然后返回响应对象
【发布时间】:2018-02-01 09:03:05
【问题描述】:

我正在尝试修改 SAML 响应正文内容(以添加空格、额外属性等)以进行测试。但似乎无法找到一种方法将编组的对象转换为我可以修改的字符串,然后解组回响应对象。

执行 .toString()(正如这里提到的:I want to convert an output stream into String object)似乎只是获得了主节点,而不是全部。

基本上我一直在玩弄

        Element el = XMLObjectProviderRegistrySupport.getMarshallerFactory().getMarshaller(response).marshall(response);

尝试获取它,但我想我会在这里问,因为我在搜索中找不到它。一旦我可以将其转换为字符串或 XML 对象,似乎可以直接将其解组 (https://wiki.shibboleth.net/confluence/display/OpenSAML/OSTwoUsrManJavaCreateFromXML)

谢谢!

【问题讨论】:

标签: testing marshalling saml opensaml


【解决方案1】:
# Packages
import org.opensaml.xml.Configuration;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Element;


# Transform to w3c xml
Element xml = Configuration
               .getMarshallerFactory()
               .getMarshaller(authnRequest)
               .marshall(authnRequest);

# use referenced SO answer to write xml to a writer -  stackoverflow.com/questions/32739278/
StreamResult result = new StreamResult(new StringWriter());
TransformerFactory
  .newInstance()
  .newTransformer()
  .transform(new DOMSource(xml), result);

# Get the string from the writer
String samlRequest = result.getWriter().toString();

# et Voila
log.info("SAML request: {}", samlRequest);

【讨论】:

    猜你喜欢
    • 2017-03-03
    • 1970-01-01
    • 2017-10-17
    • 2017-01-17
    • 2022-11-19
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多