Hi
We are using Spring Boot with Okta. I have configured application with OIDC and able to sign in with Okta. However, need to get access token for the logged in user. How can we get access token?
Spring Security is saving/maintaining idToken as jwt token but I am trying get accessToken value to use as bearer token for further API calls.
Thanks!
Srini
I’m using the following in a logout()
method to get an ID token. Maybe you can use something similar for the access token?
public ResponseEntity<?> logout(HttpServletRequest request,
@AuthenticationPrincipal(expression = "idToken") OidcIdToken idToken) {
String logoutUrl = this.registration.getProviderDetails()
.getConfigurationMetadata().get("end_session_endpoint").toString();
Map<String, String> logoutDetails = new HashMap<>();
logoutDetails.put("logoutUrl", logoutUrl);
logoutDetails.put("idToken", idToken.getTokenValue());
request.getSession().invalidate();
return ResponseEntity.ok().body(logoutDetails);
}
Thank for your quick reply. idToken does not have access token embedded in it. Seems like Spring is not saving access token on to the idToken. Any other ideas to get access token?
Used oAuth2AuthorizedClientService to get access token :
@GetMapping("jwt")
public OAuth2AccessToken jwt(OAuth2AuthenticationToken authentication) {
OAuth2AuthorizedClient client =
oAuth2AuthorizedClientService.loadAuthorizedClient(authentication.getAuthorizedClientRegistrationId(), authentication.getName());
return client.getAccessToken();
}
1 Like
Hi
is above code working??
Please advice the solution we are also facing same issue…
Thanks in advance…