gpt4 book ai didi

java - SAML LogoutRequest 到 xml 表示

转载 作者:行者123 更新时间:2023-12-01 12:25:51 25 4
gpt4 key购买 nike

我像这样形成LogoutRequest

我初始化NameID

NameID nameId = buildSAMLObjectWithDefaultName(NameID.class);
nameId.setFormat("urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress");
nameId.setValue("xxxxxx");
String sessionIndex = "idxxxx";
LogoutRequest logoutRequest = genererateLogoutRequest(nameId, sessionIndex, metaData);

方法generateLogoutRequest

private static LogoutRequest genererateLogoutRequest(final NameID nameId,
final String sessionIndex, final SAMLMetaData metaData)
throws Exception {
LogoutRequest logoutRequest = buildSAMLObjectWithDefaultName(LogoutRequest.class);

logoutRequest.setID("SOME RANDOM ID");

logoutRequest.setDestination("xxxxxxx/sso/saml");
logoutRequest.setIssueInstant(new DateTime());

Issuer issuer = buildSAMLObjectWithDefaultName(Issuer.class);
issuer.setValue("xxxxx");
logoutRequest.setIssuer(issuer);

SessionIndex sessionIndexElement = buildSAMLObjectWithDefaultName(SessionIndex.class);

sessionIndexElement.setSessionIndex(sessionIndex);
logoutRequest.getSessionIndexes().add(sessionIndexElement);

logoutRequest.setNameID(nameId);
return logoutRequest;
}

其中buildSAMLObjectWithDefaultName

public static <T> T buildSAMLObjectWithDefaultName(final Class<T> clazz)
throws IllegalArgumentException, IllegalAccessException,
NoSuchFieldException, SecurityException {
XMLObjectBuilderFactory builderFactory = Configuration
.getBuilderFactory();

QName defaultElementName = (QName) clazz.getDeclaredField(
"DEFAULT_ELEMENT_NAME").get(null);
T object = (T) builderFactory.getBuilder(defaultElementName)
.buildObject(defaultElementName);

return object;
}

但是如何在控制台中显示 LogoutRequestxml 表示形式?当我这样做的时候

System.out.println(logoutRequest.getDOM().toString());

它抛出NullPointerException

最佳答案

我使用了这个设施

Marshaller marshaller = org.opensaml.Configuration
.getMarshallerFactory().getMarshaller(logoutRequest);
org.w3c.dom.Element authDOM = marshaller.marshall(logoutRequest);
StringWriter rspWrt = new StringWriter();
XMLHelper.writeNode(authDOM, rspWrt);
System.out.println(rspWrt.toString());

关于java - SAML LogoutRequest 到 xml 表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26340934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com