はじめに
以前の記事でSpring FrameworkにAuth0を導入してログイン機能を作ってみました。
本記事では、この続きで、ログアウト機能を実装していきたいと思います。
ログアウト機能実装
LogoutHandler
ログアウトを実行するためのハンドラーを作成します。
(これはAuth0の公式サイトでも公開されているのでそちらでも確認してみてください。)
package com.nothingbehind.auth0_sample.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Needed to perform SSO logout with Auth0. By default, Spring will clear the SecurityContext and the session.
* This controller will also log users out of Auth0 by calling the Auth0 logout endpoint.
*/
@Controller
public class LogoutHandler extends SecurityContextLogoutHandler {
private final ClientRegistrationRepository clientRegistrationRepository;
/**
* Create a new instance with a {@code ClientRegistrationRepository}, so that we can look up information about the
* configured provider to call the Auth0 logout endpoint. Called by the Spring framework.
*
* @param clientRegistrationRepository the {@code ClientRegistrationRepository} for this application.
*/
@Autowired
public LogoutHandler(ClientRegistrationRepository clientRegistrationRepository) {
this.clientRegistrationRepository = clientRegistrationRepository;
}
/**
* Delegates to {@linkplain SecurityContextLogoutHandler} to log the user out of the application, and then logs
* the user out of Auth0.
*
* @param httpServletRequest the request.
* @param httpServletResponse the response.
* @param authentication the current authentication.
*/
@Override
public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
Authentication authentication) {
// Invalidate the session and clear the security context
super.logout(httpServletRequest, httpServletResponse, authentication);
// Build the URL to log the user out of Auth0 and redirect them to the home page.
// URL will look like https://YOUR-DOMAIN/v2/logout?clientId=YOUR-CLIENT-ID&returnTo=http://localhost:3000
String issuer = (String) getClientRegistration().getProviderDetails().getConfigurationMetadata().get("issuer");
String clientId = getClientRegistration().getClientId();
String returnTo = ServletUriComponentsBuilder.fromCurrentContextPath().build().toString();
String logoutUrl = UriComponentsBuilder
.fromHttpUrl(issuer + "v2/logout?client_id={clientId}&returnTo={returnTo}")
.encode()
.buildAndExpand(clientId, returnTo)
.toUriString();
try {
httpServletResponse.sendRedirect(logoutUrl);
} catch (IOException ioe) {
// Handle or log error redirecting to logout URL
}
}
/**
* Gets the Spring ClientRegistration, which we use to get the registered client ID and issuer for building the
* {@code returnTo} query parameter when calling the Auth0 logout API.
*
* @return the {@code ClientRegistration} for this application.
*/
private ClientRegistration getClientRegistration() {
return this.clientRegistrationRepository.findByRegistrationId("auth0");
}
}
このハンドラークラスが何をやっているかというと、logout
というURLパスが入ってきたらログアウトを実行する、というものになります。
SecurityConfigクラスを作成
package com.nothingbehind.auth0_sample;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import com.nothingbehind.auth0_sample.controllers.LogoutHandler;
@EnableWebSecurity
public class SecurityConfig {
private final LogoutHandler logoutHandler;
public SecurityConfig(LogoutHandler logoutHandler) {
this.logoutHandler = logoutHandler;
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.oauth2Login()
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.addLogoutHandler(logoutHandler)
.and().build();
}
}
テンプレートを修正
index.html
でログアウトを実行できるように修正します。
ログアウトテキストリンクをクリックすると、ログアウトが実行されます。
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<body>
<div sec:authorize="!isAuthenticated()">
<a th:href="@{/oauth2/authorization/auth0}">Log In</a>
</div>
<div sec:authorize="isAuthenticated()">
<p>You are logged in!</p>
<a th:href="@{/logout}">Log Out</a>
</div>
</body>
</html>
これで準備完了です。
試してみましょう!
これはログインしている状態です。ここでLog Out
をクリックしてみましょう。


Log In
のリンクが表示されています。ということはログインできていない状態になった、ということになります。
成功です 👍