saml2.adoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. = SAML 2.0 Migrations
  2. == Expect `<saml2:LogoutResponse>` When `<saml2:LogoutRequest>` Validation Fails
  3. SAML identity providers expect service providers to return an error `<saml2:LogoutResponse>` if it fails to process the `<saml2:LogoutRequest>`.
  4. Past versions of Spring Security returned a 401 in some cases, breaking the chain of logout requests and responses from each relying party.
  5. In Spring Security 7, this behavior is repaired, and you need do nothing.
  6. However, if this gives you trouble, you can revert back to the old behavior by publishing a `Saml2LogoutRequestResolver` that returns `null` when an error `<saml2:LogoutRequest>` is needed.
  7. You can create a delegate like this one:
  8. [tabs]
  9. ======
  10. Java::
  11. +
  12. [source,java,role="primary"]
  13. ----
  14. @Bean
  15. Saml2LogoutResponseResolver logoutResponseResolver(RelyingPartyRegistrationRepository registrations) {
  16. OpenSaml5LogoutResponseResolver delegate = new OpenSaml5LogoutResponseResolver(registrations);
  17. return new Saml2LogoutResponseResolver() {
  18. @Override
  19. public void resolve(HttpServletRequest request, Authentication authentication) {
  20. delegate.resolve(request, authentication);
  21. }
  22. @Override
  23. public void resolve(HttpServletRequest request, Authentication authentication, Saml2AuthenticationException error) {
  24. return null;
  25. }
  26. };
  27. }
  28. ----
  29. Kotlin::
  30. +
  31. [source,kotlin,role="secondary"]
  32. ----
  33. @Bean
  34. fun logoutResponseResolver(registrations: RelyingPartyRegistrationRepository?): Saml2LogoutResponseResolver {
  35. val delegate = OpenSaml5LogoutResponseResolver(registrations)
  36. return object : Saml2LogoutResponseResolver() {
  37. override fun resolve(request: HttpServletRequest?, authentication: Authentication?) {
  38. delegate.resolve(request, authentication)
  39. }
  40. override fun resolve(request: HttpServletRequest?, authentication: Authentication?, error: Saml2AuthenticationException?) {
  41. return null
  42. }
  43. }
  44. }
  45. ----
  46. ======
  47. == Favor `Saml2ResponseAuthenticationAccessor` over `Saml2AuthenticatedPrincipal`
  48. Spring Security 7 separates `<saml2:Assertion>` details from the principal.
  49. This allows Spring Security to retrieve needed assertion details to perform Single Logout.
  50. This deprecates `Saml2AuthenticatedPrincipal`.
  51. You no longer need to implement it to use `Saml2Authentication`.
  52. Instead, the credential implements `Saml2ResponseAssertionAccessor`, which Spring Security 7 favors when determining the appropriate action based on the authentication.
  53. This change is made automatically for you when using the defaults.
  54. If this causes you trouble when upgrading, you can publish a custom `ResponseAuhenticationConverter` to return a `Saml2Authentication` instead of returning a `Saml2AssertionAuthentication` like so:
  55. [tabs]
  56. ======
  57. Java::
  58. +
  59. [source,java,role="primary"]
  60. ----
  61. @Bean
  62. OpenSaml5AuthenticationProvider authenticationProvider() {
  63. OpenSaml5AuthenticationProvider authenticationProvider =
  64. new OpenSaml5AuthenticationProvider();
  65. ResponseAuthenticationConverter defaults = new ResponseAuthenticationConverter();
  66. authenticationProvider.setResponseAuthenticationConverter(
  67. defaults.andThen((authentication) -> new Saml2Authentication(
  68. authentication.getPrincipal(),
  69. authentication.getSaml2Response(),
  70. authentication.getAuthorities())));
  71. return authenticationProvider;
  72. }
  73. ----
  74. Kotlin::
  75. +
  76. [source,kotlin,role="secondary"]
  77. ----
  78. @Bean
  79. fun authenticationProvider(): OpenSaml5AuthenticationProvider {
  80. val authenticationProvider = OpenSaml5AuthenticationProvider()
  81. val defaults = ResponseAuthenticationConverter()
  82. authenticationProvider.setResponseAuthenticationConverter(
  83. defaults.andThen { authentication ->
  84. Saml2Authentication(authentication.getPrincipal(),
  85. authentication.getSaml2Response(),
  86. authentication.getAuthorities())
  87. })
  88. return authenticationProvider
  89. }
  90. ----
  91. ======
  92. If you are constructing a `Saml2Authentication` instance yourself, consider changing to `Saml2AssertionAuthentication` to get the same benefit as the current default.
  93. == Do Not Process `<saml2:Response>` GET Requests with `Saml2AuthenticationTokenConverter`
  94. Spring Security does not support processing `<saml2:Response>` payloads over GET as this is not supported by the SAML 2.0 spec.
  95. To better comply with this, `Saml2AuthenticationTokenConverter` and `OpenSaml5AuthenticationTokenConverter` will not process GET requests by default as of Spring Security 8.
  96. To prepare for this, the property `shouldConvertGetRequests` is available.
  97. To use it, publish your own converter like so:
  98. [tabs]
  99. ======
  100. Java::
  101. +
  102. [source,java,role="primary"]
  103. ----
  104. @Bean
  105. OpenSaml5AuthenticationTokenConverter authenticationConverter(RelyingPartyRegistrationRepository registrations) {
  106. OpenSaml5AuthenticationTokenConverter authenticationConverter = new OpenSaml5AuthenticationTokenConverter(registrations);
  107. authenticationConverter.setShouldConvertGetRequests(false);
  108. return authenticationConverter;
  109. }
  110. ----
  111. Kotlin::
  112. +
  113. [source,kotlin,role="secondary"]
  114. ----
  115. @Bean
  116. fun authenticationConverter(val registrations: RelyingPartyRegistrationRepository): Saml2AuthenticationTokenConverter {
  117. val authenticationConverter = Saml2AuthenticationTokenConverter(registrations)
  118. authenticationConverter.setShouldConvertGetRequests(false)
  119. return authenticationConverter
  120. }
  121. ----
  122. ======
  123. If you must continue using `Saml2AuthenticationTokenConverter` or `OpenSaml5AuthenticationTokenConverter` to process GET requests, you can call `setShouldConvertGetRequests` to `true.`