将opensaml Response对象转换为String,将其修改,然后再转换回响应对象

时间:2017-08-23 16:51:32

标签: testing marshalling saml opensaml

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

做一个.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

谢谢!

1 个答案:

答案 0 :(得分:0)

# 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);