logout.adoc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. = OIDC Logout
  2. Once an end user is able to login to your application, it's important to consider how they will log out.
  3. Generally speaking, there are three use cases for you to consider:
  4. 1. I want to perform only a local logout
  5. 2. I want to log out both my application and the OIDC Provider, initiated by my application
  6. 3. I want to log out both my application and the OIDC Provider, initiated by the OIDC Provider
  7. [[configure-local-logout]]
  8. == Local Logout
  9. To perform a local logout, no special OIDC configuration is needed.
  10. Spring Security automatically stands up a local logout endpoint, which you can xref:servlet/authentication/logout.adoc[configure through the `logout()` DSL].
  11. [[configure-client-initiated-oidc-logout]]
  12. == OpenID Connect 1.0 Client-Initiated Logout
  13. OpenID Connect Session Management 1.0 allows the ability to log out the end user at the Provider by using the Client.
  14. One of the strategies available is https://openid.net/specs/openid-connect-rpinitiated-1_0.html[RP-Initiated Logout].
  15. If the OpenID Provider supports both Session Management and https://openid.net/specs/openid-connect-discovery-1_0.html[Discovery], the client can obtain the `end_session_endpoint` `URL` from the OpenID Provider's https://openid.net/specs/openid-connect-session-1_0.html#OPMetadata[Discovery Metadata].
  16. You can do so by configuring the `ClientRegistration` with the `issuer-uri`, as follows:
  17. [source,yaml]
  18. ----
  19. spring:
  20. security:
  21. oauth2:
  22. client:
  23. registration:
  24. okta:
  25. client-id: okta-client-id
  26. client-secret: okta-client-secret
  27. ...
  28. provider:
  29. okta:
  30. issuer-uri: https://dev-1234.oktapreview.com
  31. ----
  32. Also, you should configure `OidcClientInitiatedLogoutSuccessHandler`, which implements RP-Initiated Logout, as follows:
  33. [tabs]
  34. ======
  35. Java::
  36. +
  37. [source,java,role="primary"]
  38. ----
  39. @Configuration
  40. @EnableWebSecurity
  41. public class OAuth2LoginSecurityConfig {
  42. @Autowired
  43. private ClientRegistrationRepository clientRegistrationRepository;
  44. @Bean
  45. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  46. http
  47. .authorizeHttpRequests(authorize -> authorize
  48. .anyRequest().authenticated()
  49. )
  50. .oauth2Login(withDefaults())
  51. .logout(logout -> logout
  52. .logoutSuccessHandler(oidcLogoutSuccessHandler())
  53. );
  54. return http.build();
  55. }
  56. private LogoutSuccessHandler oidcLogoutSuccessHandler() {
  57. OidcClientInitiatedLogoutSuccessHandler oidcLogoutSuccessHandler =
  58. new OidcClientInitiatedLogoutSuccessHandler(this.clientRegistrationRepository);
  59. // Sets the location that the End-User's User Agent will be redirected to
  60. // after the logout has been performed at the Provider
  61. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}");
  62. return oidcLogoutSuccessHandler;
  63. }
  64. }
  65. ----
  66. Kotlin::
  67. +
  68. [source,kotlin,role="secondary"]
  69. ----
  70. @Configuration
  71. @EnableWebSecurity
  72. class OAuth2LoginSecurityConfig {
  73. @Autowired
  74. private lateinit var clientRegistrationRepository: ClientRegistrationRepository
  75. @Bean
  76. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  77. http {
  78. authorizeHttpRequests {
  79. authorize(anyRequest, authenticated)
  80. }
  81. oauth2Login { }
  82. logout {
  83. logoutSuccessHandler = oidcLogoutSuccessHandler()
  84. }
  85. }
  86. return http.build()
  87. }
  88. private fun oidcLogoutSuccessHandler(): LogoutSuccessHandler {
  89. val oidcLogoutSuccessHandler = OidcClientInitiatedLogoutSuccessHandler(clientRegistrationRepository)
  90. // Sets the location that the End-User's User Agent will be redirected to
  91. // after the logout has been performed at the Provider
  92. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
  93. return oidcLogoutSuccessHandler
  94. }
  95. }
  96. ----
  97. ======
  98. [NOTE]
  99. ====
  100. `OidcClientInitiatedLogoutSuccessHandler` supports the `+{baseUrl}+` placeholder.
  101. If used, the application's base URL, such as `https://app.example.org`, replaces it at request time.
  102. ====
  103. [[configure-provider-initiated-oidc-logout]]
  104. == OpenID Connect 1.0 Back-Channel Logout
  105. OpenID Connect Session Management 1.0 allows the ability to log out the end user at the Client by having the Provider make an API call to the Client.
  106. This is referred to as https://openid.net/specs/openid-connect-backchannel-1_0.html[OIDC Back-Channel Logout].
  107. To enable this, you can stand up the Back-Channel Logout endpoint in the DSL like so:
  108. [tabs]
  109. ======
  110. Java::
  111. +
  112. [source,java,role="primary"]
  113. ----
  114. @Bean
  115. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  116. http
  117. .authorizeHttpRequests((authorize) -> authorize
  118. .anyRequest().authenticated()
  119. )
  120. .oauth2Login(withDefaults())
  121. .oidcLogout((logout) -> logout
  122. .backChannel(Customizer.withDefaults())
  123. );
  124. return http.build();
  125. }
  126. ----
  127. Kotlin::
  128. +
  129. [source,kotlin,role="secondary"]
  130. ----
  131. @Bean
  132. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  133. http {
  134. authorizeRequests {
  135. authorize(anyRequest, authenticated)
  136. }
  137. oauth2Login { }
  138. oidcLogout {
  139. backChannel { }
  140. }
  141. }
  142. return http.build()
  143. }
  144. ----
  145. ======
  146. Then, you need a way listen to events published by Spring Security to remove old `OidcSessionInformation` entries, like so:
  147. [tabs]
  148. ======
  149. Java::
  150. +
  151. [source,java,role="primary"]
  152. ----
  153. @Bean
  154. public HttpSessionEventPublisher sessionEventPublisher() {
  155. return new HttpSessionEventPublisher();
  156. }
  157. ----
  158. Kotlin::
  159. +
  160. [source,kotlin,role="secondary"]
  161. ----
  162. @Bean
  163. open fun sessionEventPublisher(): HttpSessionEventPublisher {
  164. return HttpSessionEventPublisher()
  165. }
  166. ----
  167. ======
  168. This will make so that if `HttpSession#invalidate` is called, then the session is also removed from memory.
  169. And that's it!
  170. This will stand up the endpoint `+/logout/connect/back-channel/{registrationId}+` which the OIDC Provider can request to invalidate a given session of an end user in your application.
  171. [NOTE]
  172. `oidcLogout` requires that `oauth2Login` also be configured.
  173. [NOTE]
  174. `oidcLogout` requires that the session cookie be called `JSESSIONID` in order to correctly log out each session through a backchannel.
  175. === Back-Channel Logout Architecture
  176. Consider a `ClientRegistration` whose identifier is `registrationId`.
  177. The overall flow for a Back-Channel logout is like this:
  178. 1. At login time, Spring Security correlates the ID Token, CSRF Token, and Provider Session ID (if any) to your application's session id in its `OidcSessionStrategy` implementation.
  179. 2. Then at logout time, your OIDC Provider makes an API call to `/logout/connect/back-channel/registrationId` including a Logout Token that indicates either the `sub` (the End User) or the `sid` (the Provider Session ID) to logout.
  180. 3. Spring Security validates the token's signature and claims.
  181. 4. If the token contains a `sid` claim, then only the Client's session that correlates to that provider session is terminated.
  182. 5. Otherwise, if the token contains a `sub` claim, then all that Client's sessions for that End User are terminated.
  183. [NOTE]
  184. Remember that Spring Security's OIDC support is multi-tenant.
  185. This means that it will only terminate sessions whose Client matches the `aud` claim in the Logout Token.
  186. === Customizing the OIDC Provider Session Strategy
  187. By default, Spring Security stores in-memory all links between the OIDC Provider session and the Client session.
  188. There are a number of circumstances, like a clustered application, where it would be nice to store this instead in a separate location, like a database.
  189. You can achieve this by configuring a custom `OidcSessionStrategy`, like so:
  190. [tabs]
  191. ======
  192. Java::
  193. +
  194. [source,java,role="primary"]
  195. ----
  196. @Component
  197. public final class MySpringDataOidcSessionStrategy implements OidcSessionStrategy {
  198. private final OidcProviderSessionRepository sessions;
  199. // ...
  200. @Override
  201. public void saveSessionInformation(OidcSessionInformation info) {
  202. this.sessions.save(info);
  203. }
  204. @Override
  205. public OidcSessionInformation(String clientSessionId) {
  206. return this.sessions.removeByClientSessionId(clientSessionId);
  207. }
  208. @Override
  209. public Iterable<OidcSessionInformation> removeSessionInformation(OidcLogoutToken token) {
  210. return token.getSessionId() != null ?
  211. this.sessions.removeBySessionIdAndIssuerAndAudience(...) :
  212. this.sessions.removeBySubjectAndIssuerAndAudience(...);
  213. }
  214. }
  215. ----
  216. Kotlin::
  217. +
  218. [source,kotlin,role="secondary"]
  219. ----
  220. @Component
  221. class MySpringDataOidcSessionStrategy: OidcSessionStrategy {
  222. val sessions: OidcProviderSessionRepository
  223. // ...
  224. @Override
  225. fun saveSessionInformation(info: OidcSessionInformation) {
  226. this.sessions.save(info)
  227. }
  228. @Override
  229. fun removeSessionInformation(clientSessionId: String): OidcSessionInformation {
  230. return this.sessions.removeByClientSessionId(clientSessionId);
  231. }
  232. @Override
  233. fun removeSessionInformation(token: OidcLogoutToken): Iterable<OidcSessionInformation> {
  234. return token.getSessionId() != null ?
  235. this.sessions.removeBySessionIdAndIssuerAndAudience(...) :
  236. this.sessions.removeBySubjectAndIssuerAndAudience(...);
  237. }
  238. }
  239. ----
  240. ======