metadata.adoc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. [[servlet-saml2login-metadata]]
  2. = Saml 2.0 Metadata
  3. Spring Security can <<parsing-asserting-party-metadata,parse asserting party metadata>> to produce an `AssertingPartyDetails` instance as well as <<publishing-relying-party-metadata,publish relying party metadata>> from a `RelyingPartyRegistration` instance.
  4. [[parsing-asserting-party-metadata]]
  5. == Parsing `<saml2:IDPSSODescriptor>` metadata
  6. You can parse an asserting party's metadata xref:servlet/saml2/login/overview.adoc#servlet-saml2login-relyingpartyregistrationrepository[using `RelyingPartyRegistrations`].
  7. When using the OpenSAML vendor support, the resulting `AssertingPartyDetails` will be of type `OpenSamlAssertingPartyDetails`.
  8. This means you'll be able to do get the underlying OpenSAML XMLObject by doing the following:
  9. ====
  10. .Java
  11. [source,java,role="primary"]
  12. ----
  13. OpenSamlAssertingPartyDetails details = (OpenSamlAssertingPartyDetails)
  14. registration.getAssertingPartyDetails();
  15. EntityDescriptor openSamlEntityDescriptor = details.getEntityDescriptor();
  16. ----
  17. .Kotlin
  18. [source,kotlin,role="secondary"]
  19. ----
  20. val details: OpenSamlAssertingPartyDetails =
  21. registration.getAssertingPartyDetails() as OpenSamlAssertingPartyDetails;
  22. val openSamlEntityDescriptor: EntityDescriptor = details.getEntityDescriptor();
  23. ----
  24. ====
  25. [[publishing-relying-party-metadata]]
  26. == Producing `<saml2:SPSSODescriptor>` Metadata
  27. You can publish a metadata endpoint by adding the `Saml2MetadataFilter` to the filter chain, as you'll see below:
  28. ====
  29. .Java
  30. [source,java,role="primary"]
  31. ----
  32. DefaultRelyingPartyRegistrationResolver relyingPartyRegistrationResolver =
  33. new DefaultRelyingPartyRegistrationResolver(this.relyingPartyRegistrationRepository);
  34. Saml2MetadataFilter filter = new Saml2MetadataFilter(
  35. relyingPartyRegistrationResolver,
  36. new OpenSamlMetadataResolver());
  37. http
  38. // ...
  39. .saml2Login(withDefaults())
  40. .addFilterBefore(filter, Saml2WebSsoAuthenticationFilter.class);
  41. ----
  42. .Kotlin
  43. [source,kotlin,role="secondary"]
  44. ----
  45. val relyingPartyRegistrationResolver: Converter<HttpServletRequest, RelyingPartyRegistration> =
  46. DefaultRelyingPartyRegistrationResolver(this.relyingPartyRegistrationRepository)
  47. val filter = Saml2MetadataFilter(
  48. relyingPartyRegistrationResolver,
  49. OpenSamlMetadataResolver()
  50. )
  51. http {
  52. //...
  53. saml2Login { }
  54. addFilterBefore<Saml2WebSsoAuthenticationFilter>(filter)
  55. }
  56. ----
  57. ====
  58. You can use this metadata endpoint to register your relying party with your asserting party.
  59. This is often as simple as finding the correct form field to supply the metadata endpoint.
  60. By default, the metadata endpoint is `+/saml2/service-provider-metadata/{registrationId}+`.
  61. You can change this by calling the `setRequestMatcher` method on the filter:
  62. ====
  63. .Java
  64. [source,java,role="primary"]
  65. ----
  66. filter.setRequestMatcher(new AntPathRequestMatcher("/saml2/metadata/{registrationId}", "GET"));
  67. ----
  68. .Kotlin
  69. [source,kotlin,role="secondary"]
  70. ----
  71. filter.setRequestMatcher(AntPathRequestMatcher("/saml2/metadata/{registrationId}", "GET"))
  72. ----
  73. ====
  74. Or, if you have registered a custom relying party registration resolver in the constructor, then you can specify a path without a `registrationId` hint, like so:
  75. ====
  76. .Java
  77. [source,java,role="primary"]
  78. ----
  79. filter.setRequestMatcher(new AntPathRequestMatcher("/saml2/metadata", "GET"));
  80. ----
  81. .Kotlin
  82. [source,kotlin,role="secondary"]
  83. ----
  84. filter.setRequestMatcher(AntPathRequestMatcher("/saml2/metadata", "GET"))
  85. ----
  86. ====
  87. == Changing the Way a `RelyingPartyRegistration` Is Looked Up
  88. To apply a custom `RelyingPartyRegistrationResolver` to the metadata endpoint, you can provide it directly in the filter constructor like so:
  89. ====
  90. .Java
  91. [source,java,role="primary"]
  92. ----
  93. RelyingPartyRegistrationResolver myRegistrationResolver = ...;
  94. Saml2MetadataFilter metadata = new Saml2MetadataFilter(myRegistrationResolver, new OpenSamlMetadataResolver());
  95. // ...
  96. http.addFilterBefore(metadata, BasicAuthenticationFilter.class);
  97. ----
  98. .Kotlin
  99. ----
  100. val myRegistrationResolver: RelyingPartyRegistrationResolver = ...;
  101. val metadata = new Saml2MetadataFilter(myRegistrationResolver, OpenSamlMetadataResolver());
  102. // ...
  103. http.addFilterBefore(metadata, BasicAuthenticationFilter::class.java);
  104. ----
  105. ====
  106. In the event that you are applying a `RelyingPartyRegistrationResolver` to remove the `registrationId` from the URI, you must also change the URI in the filter like so:
  107. ====
  108. .Java
  109. [source,java,role="primary"]
  110. ----
  111. metadata.setRequestMatcher("/saml2/metadata")
  112. ----
  113. .Kotlin
  114. ----
  115. metadata.setRequestMatcher("/saml2/metadata")
  116. ----
  117. ====