Warm tip: This article is reproduced from serverfault.com, please click

Save oauth/token body into Java object

发布于 2020-11-26 22:21:24

After I successfully run the request to http://IP:port/oauth/ token to get the authorization token from oauth using spring framework, the response body looks something like this:

{
     access_token = jsjxjdnjf .... some_acces_token,
     token_type: bearer,
     (....) more fields
}

The client to acces this endpoint is a simple Java app using org.springframework.web.client.RestTeplate My question is: Is there a predefined class that allows me to encapsulate(map) that information and access it through getters? Or I have to implement it myself, which would look like this:

public class OauthTokeWrapper {
private String access_token;
(...)//getters,constructors...
}
Questioner
Jesus Hernandez Barrios
Viewed
0
Gary Archer 2021-03-07 17:54:06

It is not recommended to read access tokens in the OAuth Client, which should just treat the token as an opaque string to be sent to APIs. Access tokens are not always JWT format and reading tokens in the client could lead to future problems.

Instead it is typical to work with the API claims in a back end API. One option for doing this is via the Nimbus libraries. Here is some example API code of mine in case useful.