Search code examples
spring-bootspring-securityopenid-connectspring-security-oauth2oauth2client

Is it possible to get refresh token using OidcUserRequest?


I am loading oidcUser from OidcUserRequest in my Oauth2UserService implementation class.

@Override
    public OidcUser loadUser(OidcUserRequest userRequest) throws OAuth2AuthenticationException {
        OidcUser user = delegate.loadUser(userRequest);
        List<GrantedAuthority> rolesAsAuthorities = getRolesAsAuthorities(user);
        
        CustomOidcUserDetailsImpl customUser = new CustomOidcUserDetailsImpl(user, rolesAsAuthorities);
        customUser.setFullName(getFullName(user));
        customUser.setTelephone(getTelephone(user));
        customUser.setEmail(getEmail(user));
        return customUser;
    }

The problem is that i just can get OauthAccessToken and IdToken from OidcUserRequest. Are there any ways of getting Oauth2RefreshToken in my service?
I get id,access,refresh tokens if i exchange authorization code for tokens manually.


Solution

  •     @Autowired
        private OAuth2AuthorizedClientService authorizedClientService;
    
        Authentication authentication =SecurityContextHolder.getContext().getAuthentication();
            OAuth2AuthorizedClient client = authorizedClientService
                    .loadAuthorizedClient(
                            "wso2", // client registrationId
                            authentication.getName());
        Oauth2RefreshToken refreshToken = client.getRefreshtoken();