2
0

logout.adoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. [[servlet-saml2login-logout]]
  2. = Performing Single Logout
  3. Spring Security ships with support for RP- and AP-initiated SAML 2.0 Single Logout.
  4. Briefly, there are two use cases Spring Security supports:
  5. * **RP-Initiated** - Your application has an endpoint that, when POSTed to, will logout the user and send a `saml2:LogoutRequest` to the asserting party.
  6. Thereafter, the asserting party will send back a `saml2:LogoutResponse` and allow your application to respond
  7. * **AP-Initiated** - Your application has an endpoint that will receive a `saml2:LogoutRequest` from the asserting party.
  8. Your application will complete its logout at that point and then send a `saml2:LogoutResponse` to the asserting party.
  9. [NOTE]
  10. In the **AP-Initiated** scenario, any local redirection that your application would do post-logout is rendered moot.
  11. Once your application sends a `saml2:LogoutResponse`, it no longer has control of the browser.
  12. == Minimal Configuration for Single Logout
  13. To use Spring Security's SAML 2.0 Single Logout feature, you will need the following things:
  14. * First, the asserting party must support SAML 2.0 Single Logout
  15. * Second, the asserting party should be configured to sign and POST `saml2:LogoutRequest` s and `saml2:LogoutResponse` s your application's `/logout/saml2/slo` endpoint
  16. * Third, your application must have a PKCS#8 private key and X.509 certificate for signing `saml2:LogoutRequest` s and `saml2:LogoutResponse` s
  17. You can begin from the initial minimal example and add the following configuration:
  18. [source,java]
  19. ----
  20. @Value("${private.key}") RSAPrivateKey key;
  21. @Value("${public.certificate}") X509Certificate certificate;
  22. @Bean
  23. RelyingPartyRegistrationRepository registrations() {
  24. Saml2X509Credential credential = Saml2X509Credential.signing(key, certificate);
  25. RelyingPartyRegistration registration = RelyingPartyRegistrations
  26. .fromMetadataLocation("https://ap.example.org/metadata")
  27. .registrationId("id")
  28. .signingX509Credentials((signing) -> signing.add(credential)) <1>
  29. .build();
  30. return new InMemoryRelyingPartyRegistrationRepository(registration);
  31. }
  32. @Bean
  33. SecurityFilterChain web(HttpSecurity http, RelyingPartyRegistrationRepository registrations) throws Exception {
  34. http
  35. .authorizeRequests((authorize) -> authorize
  36. .anyRequest().authenticated()
  37. )
  38. .saml2Login(withDefaults())
  39. .saml2Logout(withDefaults()); <2>
  40. return http.build();
  41. }
  42. ----
  43. <1> - First, add your signing key to the `RelyingPartyRegistration` instance or to xref:servlet/saml2/login.adoc#servlet-saml2login-rpr-duplicated[multiple instances]
  44. <2> - Second, indicate that your application wants to use SAML SLO to logout the end user
  45. === Runtime Expectations
  46. Given the above configuration any logged in user can send a `POST /logout` to your application to perform RP-initiated SLO.
  47. Your application will then do the following:
  48. 1. Logout the user and invalidate the session
  49. 2. Use a `Saml2LogoutRequestResolver` to create, sign, and serialize a `<saml2:LogoutRequest>` based on the xref:servlet/saml2/login.adoc#servlet-saml2login-relyingpartyregistration[`RelyingPartyRegistration`] associated with the currently logged-in user.
  50. 3. Send a redirect or post to the asserting party based on the xref:servlet/saml2/login.adoc#servlet-saml2login-relyingpartyregistration[`RelyingPartyRegistration`]
  51. 4. Deserialize, verify, and process the `<saml2:LogoutResponse>` sent by the asserting party
  52. 5. Redirect to any configured successful logout endpoint
  53. Also, your application can participate in an AP-initiated logout when the asserting party sends a `<saml2:LogoutRequest>` to `/logout/saml2/slo`:
  54. 1. Use a `Saml2LogoutRequestHandler` to deserialize, verify, and process the `<saml2:LogoutRequest>` sent by the asserting party
  55. 2. Logout the user and invalidate the session
  56. 3. Create, sign, and serialize a `<saml2:LogoutResponse>` based on the xref:servlet/saml2/login.adoc#servlet-saml2login-relyingpartyregistration[`RelyingPartyRegistration`] associated with the just logged-out user
  57. 4. Send a redirect or post to the asserting party based on the xref:servlet/saml2/login.adoc#servlet-saml2login-relyingpartyregistration[`RelyingPartyRegistration`]
  58. == Configuring Logout Endpoints
  59. There are three behaviors that can be triggered by different endpoints:
  60. * RP-initiated logout, which allows an authenticated user to `POST` and trigger the logout process by sending the asserting party a `<saml2:LogoutRequest>`
  61. * AP-initiated logout, which allows an asserting party to send a `<saml2:LogoutRequest>` to the application
  62. * AP logout response, which allows an asserting party to send a `<saml2:LogoutResponse>` in response to the RP-initiated `<saml2:LogoutRequest>`
  63. The first is triggered by performing normal `POST /logout` when the principal is of type `Saml2AuthenticatedPrincipal`.
  64. The second is triggered by POSTing to the `/logout/saml2/slo` endpoint with a `SAMLRequest` signed by the asserting party.
  65. The third is triggered by POSTing to the `/logout/saml2/slo` endpoint with a `SAMLResponse` signed by the asserting party.
  66. Because the user is already logged in or the original Logout Request is known, the `registrationId` is already known.
  67. For this reason, `+{registrationId}+` is not part of these URLs by default.
  68. This URL is customizable in the DSL.
  69. For example, if you are migrating your existing relying party over to Spring Security, your asserting party may already be pointing to `GET /SLOService.saml2`.
  70. To reduce changes in configuration for the asserting party, you can configure the filter in the DSL like so:
  71. ====
  72. .Java
  73. [source,java,role="primary"]
  74. ----
  75. http
  76. .saml2Logout((saml2) -> saml2
  77. .logoutRequest((request) -> request.logoutUrl("/SLOService.saml2"))
  78. .logoutResponse((response) -> response.logoutUrl("/SLOService.saml2"))
  79. );
  80. ----
  81. ====
  82. You should also configure these endpoints in your `RelyingPartyRegistration`.
  83. == Customizing `<saml2:LogoutRequest>` Resolution
  84. It's common to need to set other values in the `<saml2:LogoutRequest>` than the defaults that Spring Security provides.
  85. By default, Spring Security will issue a `<saml2:LogoutRequest>` and supply:
  86. * The `Destination` attribute - from `RelyingPartyRegistration#getAssertingPartyDetails#getSingleLogoutServiceLocation`
  87. * The `ID` attribute - a GUID
  88. * The `<Issuer>` element - from `RelyingPartyRegistration#getEntityId`
  89. * The `<NameID>` element - from `Authentication#getName`
  90. To add other values, you can use delegation, like so:
  91. [source,java]
  92. ----
  93. @Bean
  94. Saml2LogoutRequestResolver logoutRequestResolver(RelyingPartyRegistrationResolver registrationResolver) {
  95. OpenSaml4LogoutRequestResolver logoutRequestResolver
  96. new OpenSaml4LogoutRequestResolver(registrationResolver);
  97. logoutRequestResolver.setParametersConsumer((parameters) -> {
  98. String name = ((Saml2AuthenticatedPrincipal) parameters.getAuthentication().getPrincipal()).getFirstAttribute("CustomAttribute");
  99. String format = "urn:oasis:names:tc:SAML:2.0:nameid-format:transient";
  100. LogoutRequest logoutRequest = parameters.getLogoutRequest();
  101. NameID nameId = logoutRequest.getNameID();
  102. nameId.setValue(name);
  103. nameId.setFormat(format);
  104. });
  105. return logoutRequestResolver;
  106. }
  107. ----
  108. Then, you can supply your custom `Saml2LogoutRequestResolver` in the DSL as follows:
  109. [source,java]
  110. ----
  111. http
  112. .saml2Logout((saml2) -> saml2
  113. .logoutRequest((request) -> request
  114. .logoutRequestResolver(this.logoutRequestResolver)
  115. )
  116. );
  117. ----
  118. == Customizing `<saml2:LogoutResponse>` Resolution
  119. It's common to need to set other values in the `<saml2:LogoutResponse>` than the defaults that Spring Security provides.
  120. By default, Spring Security will issue a `<saml2:LogoutResponse>` and supply:
  121. * The `Destination` attribute - from `RelyingPartyRegistration#getAssertingPartyDetails#getSingleLogoutServiceResponseLocation`
  122. * The `ID` attribute - a GUID
  123. * The `<Issuer>` element - from `RelyingPartyRegistration#getEntityId`
  124. * The `<Status>` element - `SUCCESS`
  125. To add other values, you can use delegation, like so:
  126. [source,java]
  127. ----
  128. @Bean
  129. public Saml2LogoutResponseResolver logoutResponseResolver(RelyingPartyRegistrationResolver registrationResolver) {
  130. OpenSaml4LogoutResponseResolver logoutRequestResolver =
  131. new OpenSaml3LogoutResponseResolver(relyingPartyRegistrationResolver);
  132. logoutRequestResolver.setParametersConsumer((parameters) -> {
  133. if (checkOtherPrevailingConditions(parameters.getRequest())) {
  134. parameters.getLogoutRequest().getStatus().getStatusCode().setCode(StatusCode.PARTIAL_LOGOUT);
  135. }
  136. });
  137. return logoutRequestResolver;
  138. }
  139. ----
  140. Then, you can supply your custom `Saml2LogoutResponseResolver` in the DSL as follows:
  141. [source,java]
  142. ----
  143. http
  144. .saml2Logout((saml2) -> saml2
  145. .logoutRequest((request) -> request
  146. .logoutRequestResolver(this.logoutRequestResolver)
  147. )
  148. );
  149. ----
  150. == Customizing `<saml2:LogoutRequest>` Authentication
  151. To customize validation, you can implement your own `Saml2LogoutRequestValidator`.
  152. At this point, the validation is minimal, so you may be able to first delegate to the default `Saml2LogoutRequestValidator` like so:
  153. [source,java]
  154. ----
  155. @Component
  156. public class MyOpenSamlLogoutRequestValidator implements Saml2LogoutRequestValidator {
  157. private final Saml2LogoutRequestValidator delegate = new OpenSamlLogoutRequestValidator();
  158. @Override
  159. public Saml2LogoutRequestValidator logout(Saml2LogoutRequestValidatorParameters parameters) {
  160. // verify signature, issuer, destination, and principal name
  161. Saml2LogoutValidatorResult result = delegate.authenticate(authentication);
  162. LogoutRequest logoutRequest = // ... parse using OpenSAML
  163. // perform custom validation
  164. }
  165. }
  166. ----
  167. Then, you can supply your custom `Saml2LogoutRequestValidator` in the DSL as follows:
  168. [source,java]
  169. ----
  170. http
  171. .saml2Logout((saml2) -> saml2
  172. .logoutRequest((request) -> request
  173. .logoutRequestAuthenticator(myOpenSamlLogoutRequestAuthenticator)
  174. )
  175. );
  176. ----
  177. == Customizing `<saml2:LogoutResponse>` Authentication
  178. To customize validation, you can implement your own `Saml2LogoutResponseValidator`.
  179. At this point, the validation is minimal, so you may be able to first delegate to the default `Saml2LogoutResponseValidator` like so:
  180. [source,java]
  181. ----
  182. @Component
  183. public class MyOpenSamlLogoutResponseValidator implements Saml2LogoutResponseValidator {
  184. private final Saml2LogoutResponseValidator delegate = new OpenSamlLogoutResponseValidator();
  185. @Override
  186. public Saml2LogoutValidatorResult logout(Saml2LogoutResponseValidatorParameters parameters) {
  187. // verify signature, issuer, destination, and status
  188. Saml2LogoutValidatorResult result = delegate.authenticate(parameters);
  189. LogoutResponse logoutResponse = // ... parse using OpenSAML
  190. // perform custom validation
  191. }
  192. }
  193. ----
  194. Then, you can supply your custom `Saml2LogoutResponseValidator` in the DSL as follows:
  195. [source,java]
  196. ----
  197. http
  198. .saml2Logout((saml2) -> saml2
  199. .logoutResponse((response) -> response
  200. .logoutResponseAuthenticator(myOpenSamlLogoutResponseAuthenticator)
  201. )
  202. );
  203. ----
  204. == Customizing `<saml2:LogoutRequest>` storage
  205. When your application sends a `<saml2:LogoutRequest>`, the value is stored in the session so that the `RelayState` parameter and the `InResponseTo` attribute in the `<saml2:LogoutResponse>` can be verified.
  206. If you want to store logout requests in some place other than the session, you can supply your custom implementation in the DSL, like so:
  207. [source,java]
  208. ----
  209. http
  210. .saml2Logout((saml2) -> saml2
  211. .logoutRequest((request) -> request
  212. .logoutRequestRepository(myCustomLogoutRequestRepository)
  213. )
  214. );
  215. ----