How to revoke auth token in spring security?
If you need to revoke a token for another user than the current one (E.g. an admin wants to disable a user account), you can use this:
Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByClientIdAndUserName(
"my_oauth_client_id",
user.getUsername());
for (OAuth2AccessToken token : tokens) {
consumerTokenServices.revokeToken(token.getValue());
}
With tokenStore
being an org.springframework.security.oauth2.provider.token.TokenStore
and consumerTokenServices
being a org.springframework.security.oauth2.provider.token.ConsumerTokenServices
The class you're looking for is
DefaultServices
, method revokeToken(String tokenValue)
.
Here an exemple of a controller that revokes token, and here the oauth2 configuration with the DefaultServices
bean.