spring-projects / spring-projects/spring-security
Add support for removing OIDC user session with Spring Session in OIDC backchannel logout
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.6k
- Forks
- 6.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 52
Description
Implementing OIDC backchannel logout with Spring Security 6.4.2 in a clustered Spring JDBC Session context, I need a way to remove the OIDC user session saved by the OidcSessionRegistry, when the OidcBackChannelLogoutHandler performs a POST request for logging out the user. The issue is not with the registry, as I've implemented storing of the OIDC user sessions with JDBC. The issue is to automatically call the OidcSessionRegistry#removeSessionInformation when the Spring Session gets invalidated.
As mentioned in https://docs.spring.io/spring-security/reference/servlet/oauth2/login/logout.html#configure-provider-initiated-oidc-logout "you need a way listen to events published by Spring Security to remove old OidcSessionInformation entries [..]" by declaring a HttpSessionEventPublisher as a Bean. This works fine in a none-Spring Session context, but when using Spring Session, to support a clustered setup, the event is not picked up, and the OIDC user session is thus not removed.
As such, this is a request to add support in Spring Security to remove the OIDC user session from the OidcSessionRegistry when a session is invalidated in a Spring Session context.
To reproduce
- Prepare an application which uses Spring Session stored in JDBC + OIDC backchannel logout configured
- Log in to the application using OIDC integration
- Trigger OIDC back channel logout
Expected Behavior
- The OIDC user session in the OidcSessionRegistry is removed when the Spring Session is invalidated.
Current Behavior
- The Spring Session gets invalidated, but the OIDC user session remains in the OidcSessionRegistry.
Context
Using Spring Boot 3.4.2, Spring Session (JDBC) 3.4.1, and Spring Security 6.4.2.
My current workaround is to define a LogoutHandler, which makes sure to remove the OIDC user session from the registry, if a valid session is present. Minimal example:
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http, OidcSessionLogoutHandler oidcSessionLogoutHandler, ...) {
return http
...
.logout(logout -> logout
.addLogoutHandler(oidcSessionLogoutHandler)
...
)
...
.build();
}
@Slf4j
@Component
public class OidcSessionLogoutHandler implements LogoutHandler {
private final OidcSessionRegistry oidcSessionRegistry;
public OidcSessionLogoutHandler(OidcSessionRegistry oidcSessionRegistry) {
this.oidcSessionRegistry = oidcSessionRegistry;
}
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
var session = request.getSession(false);
if (session == null) {
log.debug("No valid session found. Ignoring OIDC Session logout.");
return;
}
var removedSession = oidcSessionRegistry.removeSessionInformation(session.getId());
if (removedSession == null) {
log.trace("No OIDC session found for id {}. Could be caused by an OIDC Back Channel Logout, " +
"where the session info is already removed", session.getId());
} else {
log.trace("Removed OIDC session with id {}", removedSession.getSessionId());
}
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading OidcSessionRegistry, OidcBackChannelLogoutHandler, and the Spring Session invalidation flow, comparing it with HttpSessionEventPublisher. Reproduce the JDBC-backed session scenario described in the issue; done means invalidating a Spring Session removes the corresponding OIDC session information from the registry during backchannel logout.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- authentication, security
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100