2
0

logout.adoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. [NOTE]
  105. ====
  106. By default, `OidcClientInitiatedServerLogoutSuccessHandler` redirects to the logout URL using a standard HTTP redirect with the `GET` method.
  107. To perform the logout using a `POST` request, set the redirect strategy to `FormPostServerRedirectStrategy`, for example with `OidcClientInitiatedServerLogoutSuccessHandler.setRedirectStrategy(new ServerFormPostRedirectStrategy())`.
  108. ====
  109. [[configure-provider-initiated-oidc-logout]]
  110. == OpenID Connect 1.0 Back-Channel Logout
  111. 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.
  112. This is referred to as https://openid.net/specs/openid-connect-backchannel-1_0.html[OIDC Back-Channel Logout].
  113. To enable this, you can stand up the Back-Channel Logout endpoint in the DSL like so:
  114. [tabs]
  115. ======
  116. Java::
  117. +
  118. [source,java,role="primary"]
  119. ----
  120. @Bean
  121. OidcBackChannelServerLogoutHandler oidcLogoutHandler() {
  122. return new OidcBackChannelServerLogoutHandler();
  123. }
  124. @Bean
  125. public SecurityWebFilterChain filterChain(ServerHttpSecurity http) throws Exception {
  126. http
  127. .authorizeExchange((authorize) -> authorize
  128. .anyExchange().authenticated()
  129. )
  130. .oauth2Login(withDefaults())
  131. .oidcLogout((logout) -> logout
  132. .backChannel(Customizer.withDefaults())
  133. );
  134. return http.build();
  135. }
  136. ----
  137. Kotlin::
  138. +
  139. [source,kotlin,role="secondary"]
  140. ----
  141. @Bean
  142. fun oidcLogoutHandler(): OidcBackChannelLogoutHandler {
  143. return OidcBackChannelLogoutHandler()
  144. }
  145. @Bean
  146. open fun filterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  147. http {
  148. authorizeExchange {
  149. authorize(anyExchange, authenticated)
  150. }
  151. oauth2Login { }
  152. oidcLogout {
  153. backChannel { }
  154. }
  155. }
  156. return http.build()
  157. }
  158. ----
  159. ======
  160. And that's it!
  161. 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.
  162. [NOTE]
  163. `oidcLogout` requires that `oauth2Login` also be configured.
  164. [NOTE]
  165. `oidcLogout` requires that the session cookie be called `JSESSIONID` in order to correctly log out each session through a backchannel.
  166. === Back-Channel Logout Architecture
  167. Consider a `ClientRegistration` whose identifier is `registrationId`.
  168. The overall flow for a Back-Channel logout is like this:
  169. 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.
  170. 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.
  171. 3. Spring Security validates the token's signature and claims.
  172. 4. If the token contains a `sid` claim, then only the Client's session that correlates to that provider session is terminated.
  173. 5. Otherwise, if the token contains a `sub` claim, then all that Client's sessions for that End User are terminated.
  174. [NOTE]
  175. Remember that Spring Security's OIDC support is multi-tenant.
  176. This means that it will only terminate sessions whose Client matches the `aud` claim in the Logout Token.
  177. === Customizing the Session Logout Endpoint
  178. With `OidcBackChannelServerLogoutHandler` published, the session logout endpoint is `+{baseUrl}+/logout/connect/back-channel/+{registrationId}+`.
  179. 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.
  180. In the event that you need to customize the endpoint, you can provide the URL as follows:
  181. [tabs]
  182. ======
  183. Java::
  184. +
  185. [source=java,role="primary"]
  186. ----
  187. http
  188. // ...
  189. .oidcLogout((oidc) -> oidc
  190. .backChannel((backChannel) -> backChannel
  191. .logoutUri("http://localhost:9000/logout/connect/back-channel/+{registrationId}+")
  192. )
  193. );
  194. ----
  195. Kotlin::
  196. +
  197. [source=kotlin,role="secondary"]
  198. ----
  199. http {
  200. oidcLogout {
  201. backChannel {
  202. logoutUri = "http://localhost:9000/logout/connect/back-channel/+{registrationId}+"
  203. }
  204. }
  205. }
  206. ----
  207. ======
  208. === Customizing the Session Logout Cookie Name
  209. By default, the session logout endpoint uses the `JSESSIONID` cookie to correlate the session to the corresponding `OidcSessionInformation`.
  210. However, the default cookie name in Spring Session is `SESSION`.
  211. You can configure Spring Session's cookie name in the DSL like so:
  212. [tabs]
  213. ======
  214. Java::
  215. +
  216. [source=java,role="primary"]
  217. ----
  218. @Bean
  219. OidcBackChannelServerLogoutHandler oidcLogoutHandler(ReactiveOidcSessionRegistry sessionRegistry) {
  220. OidcBackChannelServerLogoutHandler logoutHandler = new OidcBackChannelServerLogoutHandler(sessionRegistry);
  221. logoutHandler.setSessionCookieName("SESSION");
  222. return logoutHandler;
  223. }
  224. ----
  225. Kotlin::
  226. +
  227. [source=kotlin,role="secondary"]
  228. ----
  229. @Bean
  230. open fun oidcLogoutHandler(val sessionRegistry: ReactiveOidcSessionRegistry): OidcBackChannelServerLogoutHandler {
  231. val logoutHandler = OidcBackChannelServerLogoutHandler(sessionRegistry)
  232. logoutHandler.setSessionCookieName("SESSION")
  233. return logoutHandler
  234. }
  235. ----
  236. ======
  237. [[oidc-backchannel-logout-session-registry]]
  238. === Customizing the OIDC Provider Session Registry
  239. By default, Spring Security stores in-memory all links between the OIDC Provider session and the Client session.
  240. 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.
  241. You can achieve this by configuring a custom `ReactiveOidcSessionRegistry`, like so:
  242. [tabs]
  243. ======
  244. Java::
  245. +
  246. [source,java,role="primary"]
  247. ----
  248. @Component
  249. public final class MySpringDataOidcSessionRegistry implements ReactiveOidcSessionRegistry {
  250. private final OidcProviderSessionRepository sessions;
  251. // ...
  252. @Override
  253. public Mono<void> saveSessionInformation(OidcSessionInformation info) {
  254. return this.sessions.save(info);
  255. }
  256. @Override
  257. public Mono<OidcSessionInformation> removeSessionInformation(String clientSessionId) {
  258. return this.sessions.removeByClientSessionId(clientSessionId);
  259. }
  260. @Override
  261. public Flux<OidcSessionInformation> removeSessionInformation(OidcLogoutToken token) {
  262. return token.getSessionId() != null ?
  263. this.sessions.removeBySessionIdAndIssuerAndAudience(...) :
  264. this.sessions.removeBySubjectAndIssuerAndAudience(...);
  265. }
  266. }
  267. ----
  268. Kotlin::
  269. +
  270. [source,kotlin,role="secondary"]
  271. ----
  272. @Component
  273. class MySpringDataOidcSessionRegistry: ReactiveOidcSessionRegistry {
  274. val sessions: OidcProviderSessionRepository
  275. // ...
  276. @Override
  277. fun saveSessionInformation(info: OidcSessionInformation): Mono<Void> {
  278. return this.sessions.save(info)
  279. }
  280. @Override
  281. fun removeSessionInformation(clientSessionId: String): Mono<OidcSessionInformation> {
  282. return this.sessions.removeByClientSessionId(clientSessionId);
  283. }
  284. @Override
  285. fun removeSessionInformation(token: OidcLogoutToken): Flux<OidcSessionInformation> {
  286. return token.getSessionId() != null ?
  287. this.sessions.removeBySessionIdAndIssuerAndAudience(...) :
  288. this.sessions.removeBySubjectAndIssuerAndAudience(...);
  289. }
  290. }
  291. ----
  292. ======