Saml 2.0 元数据

Spring Security 可以解析断言方元数据以生成实例,以及从实例发布依赖方元数据AssertingPartyMetadataRelyingPartyRegistrationspring-doc.cn

解析元数据<saml2:IDPSSODescriptor>

您可以使用 RelyingPartyRegistrations 解析断言方的元数据。spring-doc.cn

使用 OpenSAML 供应商支持时,结果将为 . 这意味着您将能够通过执行以下操作来获取底层 OpenSAML XMLObject:AssertingPartyMetadataOpenSamlAssertingPartyDetailsspring-doc.cn

OpenSamlAssertingPartyDetails details = (OpenSamlAssertingPartyDetails)
        registration.getAssertingPartyMetadata();
EntityDescriptor openSamlEntityDescriptor = details.getEntityDescriptor();
val details: OpenSamlAssertingPartyDetails =
        registration.getAssertingPartyMetadata() as OpenSamlAssertingPartyDetails
val openSamlEntityDescriptor: EntityDescriptor = details.getEntityDescriptor()

AssertingPartyMetadataRepository

与使用 相比,您还可以更具针对性,这是一个只允许检索断言方元数据的接口。RelyingPartyRegistrationsAssertingPartyMetadataRepositoryspring-doc.cn

这允许三个有价值的功能:spring-doc.cn

  • 实现可以以过期感知方式刷新断言方元数据spring-doc.cn

  • 的实现可以更轻松地阐明依赖方与其一个或多个相应的断言方之间的关系RelyingPartyRegistrationRepositoryspring-doc.cn

  • 实现可以验证元数据签名spring-doc.cn

例如,使用 OpenSAML 的 和 API,其实施会定期以过期感知方式刷新底层元数据。OpenSaml4AssertingPartyMetadataRepositoryMetadataResolverspring-doc.cn

这意味着您现在只需几行代码即可创建可刷新对象:RelyingPartyRegistrationRepositoryspring-doc.cn

@Component
public class RefreshableRelyingPartyRegistrationRepository
        implements IterableRelyingPartyRegistrationRepository {

	private final AssertingPartyMetadataRepository metadata =
            OpenSamlAssertingPartyMetadataRepository
                .fromTrustedMetadataLocation("https://idp.example.org/metadata").build();

	@Override
    public RelyingPartyRegistration findByRegistrationId(String registrationId) {
		AssertingPartyMetadata metadata = this.metadata.findByEntityId(registrationId);
        if (metadata == null) {
            return null;
        }
		return applyRelyingParty(metadata);
    }

	@Override
    public Iterator<RelyingPartyRegistration> iterator() {
		return StreamSupport.stream(this.metadata.spliterator(), false)
            .map(this::applyRelyingParty).iterator();
    }

	private RelyingPartyRegistration applyRelyingParty(AssertingPartyMetadata metadata) {
		return RelyingPartyRegistration.withAssertingPartyMetadata(metadata)
            // apply any relying party configuration
            .build();
	}

}
@Component
class RefreshableRelyingPartyRegistrationRepository : IterableRelyingPartyRegistrationRepository {

    private val metadata: AssertingPartyMetadataRepository =
        OpenSamlAssertingPartyMetadataRepository.fromTrustedMetadataLocation(
            "https://idp.example.org/metadata").build()

    fun findByRegistrationId(registrationId:String?): RelyingPartyRegistration {
        val metadata = this.metadata.findByEntityId(registrationId)
        if (metadata == null) {
            return null
        }
        return applyRelyingParty(metadata)
    }

    fun iterator(): Iterator<RelyingPartyRegistration> {
        return StreamSupport.stream(this.metadata.spliterator(), false)
            .map(this::applyRelyingParty).iterator()
    }

    private fun applyRelyingParty(metadata: AssertingPartyMetadata): RelyingPartyRegistration {
        val details: AssertingPartyMetadata = metadata as AssertingPartyMetadata
        return RelyingPartyRegistration.withAssertingPartyMetadata(details)
            // apply any relying party configuration
            .build()
    }
 }
OpenSaml4AssertingPartyMetadataRepository还附带了一个构造函数,因此您可以提供自定义 .由于底层正在执行过期和刷新,因此如果您直接使用构造函数,则只能通过提供执行此操作的实现来获得这些功能。MetadataResolverMetadataResolver

验证元数据签名

您还可以通过提供适当的 s 集来验证元数据签名,如下所示:OpenSaml4AssertingPartyMetadataRepositorySaml2X509Credentialspring-doc.cn

OpenSamlAssertingPartyMetadataRepository.withMetadataLocation("https://idp.example.org/metadata")
    .verificationCredentials((c) -> c.add(myVerificationCredential))
    .build();
OpenSamlAssertingPartyMetadataRepository.withMetadataLocation("https://idp.example.org/metadata")
    .verificationCredentials({ c : Collection<Saml2X509Credential> ->
        c.add(myVerificationCredential) })
    .build()
如果未提供凭据,则组件将不会执行签名验证。

生成元数据<saml2:SPSSODescriptor>

您可以使用 DSL 方法发布元数据终端节点,如下所示:saml2Metadataspring-doc.cn

http
    // ...
    .saml2Login(withDefaults())
    .saml2Metadata(withDefaults());
http {
    //...
    saml2Login { }
    saml2Metadata { }
}

您可以使用此元数据终端节点向断言方注册您的信赖方。 这通常就像找到正确的表单字段来提供元数据端点一样简单。spring-doc.cn

默认情况下,元数据端点为 ,尽管它也响应 和 。/saml2/metadata/saml2/metadata/{registrationId}/saml2/service-provider-metadata/{registrationId}spring-doc.cn

您可以通过调用 DSL 中的方法来更改此设置:metadataUrlspring-doc.cn

.saml2Metadata((saml2) -> saml2.metadataUrl("/saml/metadata"))
saml2Metadata {
	metadataUrl = "/saml/metadata"
}

更改查找 A 的方式RelyingPartyRegistration

如果你有不同的策略来确定要使用的策略,你可以配置自己的策略,如下所示:RelyingPartyRegistrationSaml2MetadataResponseResolverspring-doc.cn

@Bean
Saml2MetadataResponseResolver metadataResponseResolver(RelyingPartyRegistrationRepository registrations) {
	RequestMatcherMetadataResponseResolver metadata = new RequestMatcherMetadataResponseResolver(
			(id) -> registrations.findByRegistrationId("relying-party"));
	metadata.setMetadataFilename("metadata.xml");
	return metadata;
}
@Bean
fun metadataResponseResolver(val registrations: RelyingPartyRegistrationRepository): Saml2MetadataResponseResolver {
    val metadata = new RequestMatcherMetadataResponseResolver(
			id: String -> registrations.findByRegistrationId("relying-party"))
	metadata.setMetadataFilename("metadata.xml")
	return metadata
}