diff --git a/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/metadata/OpenSamlMetadataResolver.java b/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/metadata/OpenSamlMetadataResolver.java index a8137fb93f..e4b23b5000 100644 --- a/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/metadata/OpenSamlMetadataResolver.java +++ b/saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/metadata/OpenSamlMetadataResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,6 +72,8 @@ public final class OpenSamlMetadataResolver implements Saml2MetadataResolver { private Consumer entityDescriptorCustomizer = (parameters) -> { }; + private boolean usePrettyPrint = true; + public OpenSamlMetadataResolver() { this.entityDescriptorMarshaller = (EntityDescriptorMarshaller) XMLObjectProviderRegistrySupport .getMarshallerFactory() @@ -123,6 +125,15 @@ public final class OpenSamlMetadataResolver implements Saml2MetadataResolver { this.entityDescriptorCustomizer = entityDescriptorCustomizer; } + /** + * Configure whether to pretty-print the metadata XML. This can be helpful when + * signing the metadata payload. + * @since 6.2 + **/ + public void setUsePrettyPrint(boolean usePrettyPrint) { + this.usePrettyPrint = usePrettyPrint; + } + private SPSSODescriptor buildSpSsoDescriptor(RelyingPartyRegistration registration) { SPSSODescriptor spSsoDescriptor = build(SPSSODescriptor.DEFAULT_ELEMENT_NAME); spSsoDescriptor.addSupportedProtocol(SAMLConstants.SAML20P_NS); @@ -204,7 +215,10 @@ public final class OpenSamlMetadataResolver implements Saml2MetadataResolver { private String serialize(EntityDescriptor entityDescriptor) { try { Element element = this.entityDescriptorMarshaller.marshall(entityDescriptor); - return SerializeSupport.prettyPrintXML(element); + if (this.usePrettyPrint) { + return SerializeSupport.prettyPrintXML(element); + } + return SerializeSupport.nodeToString(element); } catch (Exception ex) { throw new Saml2Exception(ex); @@ -214,7 +228,10 @@ public final class OpenSamlMetadataResolver implements Saml2MetadataResolver { private String serialize(EntitiesDescriptor entities) { try { Element element = this.entitiesDescriptorMarshaller.marshall(entities); - return SerializeSupport.prettyPrintXML(element); + if (this.usePrettyPrint) { + return SerializeSupport.prettyPrintXML(element); + } + return SerializeSupport.nodeToString(element); } catch (Exception ex) { throw new Saml2Exception(ex);