logout.adoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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:reactive/authentication/logout.adoc[configure through the `logout()` DSL].
  11. [[configure-client-initiated-oidc-logout]]
  12. [[oauth2login-advanced-oidc-logout]]
  13. == OpenID Connect 1.0 Client-Initiated Logout
  14. OpenID Connect Session Management 1.0 allows the ability to log out the end user at the Provider by using the Client.
  15. One of the strategies available is https://openid.net/specs/openid-connect-rpinitiated-1_0.html[RP-Initiated Logout].
  16. 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].
  17. You can do so by configuring the `ClientRegistration` with the `issuer-uri`, as follows:
  18. [source,yaml]
  19. ----
  20. spring:
  21. security:
  22. oauth2:
  23. client:
  24. registration:
  25. okta:
  26. client-id: okta-client-id
  27. client-secret: okta-client-secret
  28. ...
  29. provider:
  30. okta:
  31. issuer-uri: https://dev-1234.oktapreview.com
  32. ----
  33. Also, you should configure `OidcClientInitiatedServerLogoutSuccessHandler`, which implements RP-Initiated Logout, as follows:
  34. [tabs]
  35. ======
  36. Java::
  37. +
  38. [source,java,role="primary"]
  39. ----
  40. @Configuration
  41. @EnableWebFluxSecurity
  42. public class OAuth2LoginSecurityConfig {
  43. @Autowired
  44. private ReactiveClientRegistrationRepository clientRegistrationRepository;
  45. @Bean
  46. public SecurityWebFilterChain filterChain(ServerHttpSecurity http) throws Exception {
  47. http
  48. .authorizeExchange((authorize) -> authorize
  49. .anyExchange().authenticated()
  50. )
  51. .oauth2Login(withDefaults())
  52. .logout((logout) -> logout
  53. .logoutSuccessHandler(oidcLogoutSuccessHandler())
  54. );
  55. return http.build();
  56. }
  57. private ServerLogoutSuccessHandler oidcLogoutSuccessHandler() {
  58. OidcClientInitiatedServerLogoutSuccessHandler oidcLogoutSuccessHandler =
  59. new OidcClientInitiatedServerLogoutSuccessHandler(this.clientRegistrationRepository);
  60. // Sets the location that the End-User's User Agent will be redirected to
  61. // after the logout has been performed at the Provider
  62. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}");
  63. return oidcLogoutSuccessHandler;
  64. }
  65. }
  66. ----
  67. Kotlin::
  68. +
  69. [source,kotlin,role="secondary"]
  70. ----
  71. @Configuration
  72. @EnableWebFluxSecurity
  73. class OAuth2LoginSecurityConfig {
  74. @Autowired
  75. private lateinit var clientRegistrationRepository: ReactiveClientRegistrationRepository
  76. @Bean
  77. open fun filterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  78. http {
  79. authorizeExchange {
  80. authorize(anyExchange, authenticated)
  81. }
  82. oauth2Login { }
  83. logout {
  84. logoutSuccessHandler = oidcLogoutSuccessHandler()
  85. }
  86. }
  87. return http.build()
  88. }
  89. private fun oidcLogoutSuccessHandler(): ServerLogoutSuccessHandler {
  90. val oidcLogoutSuccessHandler = OidcClientInitiatedServerLogoutSuccessHandler(clientRegistrationRepository)
  91. // Sets the location that the End-User's User Agent will be redirected to
  92. // after the logout has been performed at the Provider
  93. oidcLogoutSuccessHandler.setPostLogoutRedirectUri("{baseUrl}")
  94. return oidcLogoutSuccessHandler
  95. }
  96. }
  97. ----
  98. ======
  99. [NOTE]
  100. ====
  101. `OidcClientInitiatedServerLogoutSuccessHandler` supports the `+{baseUrl}+` placeholder.
  102. If used, the application's base URL, such as `https://app.example.org`, replaces it at request time.
  103. ====
  104. [[configure-provider-initiated-oidc-logout]]
  105. == OpenID Connect 1.0 Back-Channel Logout
  106. 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.
  107. This is referred to as https://openid.net/specs/openid-connect-backchannel-1_0.html[OIDC Back-Channel Logout].
  108. To enable this, you can stand up the Back-Channel Logout endpoint in the DSL like so:
  109. [tabs]
  110. ======
  111. Java::
  112. +
  113. [source,java,role="primary"]
  114. ----
  115. @Bean
  116. OidcBackChannelServerLogoutHandler oidcLogoutHandler() {
  117. return new OidcBackChannelServerLogoutHandler();
  118. }
  119. @Bean
  120. public SecurityWebFilterChain filterChain(ServerHttpSecurity http) throws Exception {
  121. http
  122. .authorizeExchange((authorize) -> authorize
  123. .anyExchange().authenticated()
  124. )
  125. .oauth2Login(withDefaults())
  126. .oidcLogout((logout) -> logout
  127. .backChannel(Customizer.withDefaults())
  128. );
  129. return http.build();
  130. }
  131. ----
  132. Kotlin::
  133. +
  134. [source,kotlin,role="secondary"]
  135. ----
  136. @Bean
  137. fun oidcLogoutHandler(): OidcBackChannelLogoutHandler {
  138. return OidcBackChannelLogoutHandler()
  139. }
  140. @Bean
  141. open fun filterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  142. http {
  143. authorizeExchange {
  144. authorize(anyExchange, authenticated)
  145. }
  146. oauth2Login { }
  147. oidcLogout {
  148. backChannel { }
  149. }
  150. }
  151. return http.build()
  152. }
  153. ----
  154. ======
  155. And that's it!
  156. 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.
  157. [NOTE]
  158. `oidcLogout` requires that `oauth2Login` also be configured.
  159. [NOTE]
  160. `oidcLogout` requires that the session cookie be called `JSESSIONID` in order to correctly log out each session through a backchannel.
  161. === Back-Channel Logout Architecture
  162. Consider a `ClientRegistration` whose identifier is `registrationId`.
  163. The overall flow for a Back-Channel logout is like this:
  164. 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 `ReactiveOidcSessionRegistry` implementation.
  165. 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.
  166. 3. Spring Security validates the token's signature and claims.
  167. 4. If the token contains a `sid` claim, then only the Client's session that correlates to that provider session is terminated.
  168. 5. Otherwise, if the token contains a `sub` claim, then all that Client's sessions for that End User are terminated.
  169. [NOTE]
  170. Remember that Spring Security's OIDC support is multi-tenant.
  171. This means that it will only terminate sessions whose Client matches the `aud` claim in the Logout Token.
  172. === Customizing the Session Logout Endpoint
  173. With `OidcBackChannelServerLogoutHandler` published, the session logout endpoint is `+{baseUrl}+/logout/connect/back-channel/+{registrationId}+`.
  174. If `OidcBackChannelServerLogoutHandler` is not wired, then the URL is `+{baseUrl}+/logout/connect/back-channel/+{registrationId}+`, which is not recommended since it requires passing a CSRF token, which can be challenging depending on the kind of repository your application uses.
  175. In the event that you need to customize the endpoint, you can provide the URL as follows:
  176. [tabs]
  177. ======
  178. Java::
  179. +
  180. [source=java,role="primary"]
  181. ----
  182. http
  183. // ...
  184. .oidcLogout((oidc) -> oidc
  185. .backChannel((backChannel) -> backChannel
  186. .logoutUri("http://localhost:9000/logout/connect/back-channel/+{registrationId}+")
  187. )
  188. );
  189. ----
  190. Kotlin::
  191. +
  192. [source=kotlin,role="secondary"]
  193. ----
  194. http {
  195. oidcLogout {
  196. backChannel {
  197. logoutUri = "http://localhost:9000/logout/connect/back-channel/+{registrationId}+"
  198. }
  199. }
  200. }
  201. ----
  202. ======
  203. === Customizing the Session Logout Cookie Name
  204. By default, the session logout endpoint uses the `JSESSIONID` cookie to correlate the session to the corresponding `OidcSessionInformation`.
  205. However, the default cookie name in Spring Session is `SESSION`.
  206. You can configure Spring Session's cookie name in the DSL like so:
  207. [tabs]
  208. ======
  209. Java::
  210. +
  211. [source=java,role="primary"]
  212. ----
  213. @Bean
  214. OidcBackChannelServerLogoutHandler oidcLogoutHandler(ReactiveOidcSessionRegistry sessionRegistry) {
  215. OidcBackChannelServerLogoutHandler logoutHandler = new OidcBackChannelServerLogoutHandler(sessionRegistry);
  216. logoutHandler.setSessionCookieName("SESSION");
  217. return logoutHandler;
  218. }
  219. ----
  220. Kotlin::
  221. +
  222. [source=kotlin,role="secondary"]
  223. ----
  224. @Bean
  225. open fun oidcLogoutHandler(val sessionRegistry: ReactiveOidcSessionRegistry): OidcBackChannelServerLogoutHandler {
  226. val logoutHandler = OidcBackChannelServerLogoutHandler(sessionRegistry)
  227. logoutHandler.setSessionCookieName("SESSION")
  228. return logoutHandler
  229. }
  230. ----
  231. ======
  232. [[oidc-backchannel-logout-session-registry]]
  233. === Customizing the OIDC Provider Session Registry
  234. By default, Spring Security stores in-memory all links between the OIDC Provider session and the Client session.
  235. 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.
  236. You can achieve this by configuring a custom `ReactiveOidcSessionRegistry`, like so:
  237. [tabs]
  238. ======
  239. Java::
  240. +
  241. [source,java,role="primary"]
  242. ----
  243. @Component
  244. public final class MySpringDataOidcSessionRegistry implements ReactiveOidcSessionRegistry {
  245. private final OidcProviderSessionRepository sessions;
  246. // ...
  247. @Override
  248. public Mono<void> saveSessionInformation(OidcSessionInformation info) {
  249. return this.sessions.save(info);
  250. }
  251. @Override
  252. public Mono<OidcSessionInformation> removeSessionInformation(String clientSessionId) {
  253. return this.sessions.removeByClientSessionId(clientSessionId);
  254. }
  255. @Override
  256. public Flux<OidcSessionInformation> removeSessionInformation(OidcLogoutToken token) {
  257. return token.getSessionId() != null ?
  258. this.sessions.removeBySessionIdAndIssuerAndAudience(...) :
  259. this.sessions.removeBySubjectAndIssuerAndAudience(...);
  260. }
  261. }
  262. ----
  263. Kotlin::
  264. +
  265. [source,kotlin,role="secondary"]
  266. ----
  267. @Component
  268. class MySpringDataOidcSessionRegistry: ReactiveOidcSessionRegistry {
  269. val sessions: OidcProviderSessionRepository
  270. // ...
  271. @Override
  272. fun saveSessionInformation(info: OidcSessionInformation): Mono<Void> {
  273. return this.sessions.save(info)
  274. }
  275. @Override
  276. fun removeSessionInformation(clientSessionId: String): Mono<OidcSessionInformation> {
  277. return this.sessions.removeByClientSessionId(clientSessionId);
  278. }
  279. @Override
  280. fun removeSessionInformation(token: OidcLogoutToken): Flux<OidcSessionInformation> {
  281. return token.getSessionId() != null ?
  282. this.sessions.removeBySessionIdAndIssuerAndAudience(...) :
  283. this.sessions.removeBySubjectAndIssuerAndAudience(...);
  284. }
  285. }
  286. ----
  287. ======