Using spring boot and spring security, the details of user are available in principal object. But it has only few methods to retrieve details, like getName().
How do I get other details out of it?
Currently my class looks like this
@SpringBootApplication
@RestController
public class DemoOAuth2Application {
@RequestMapping("/user")
public Principal user(Principal principal) {
return principal;
}
public static void main(String[] args) {
SpringApplication.run(DemoOAuth2Application.class, args);
}
}
It returns this,
{
"authorities": [
{
"authority": "ROLE_USER"
}
],
"details": {
"remoteAddress": "0:0:0:0:0:0:0:1",
"sessionId": "43Fxxxxxx",
"tokenValue": "ya29.xxxxxxxxx",
"tokenType": "Bearer",
"decodedDetails": null
},
"authenticated": true,
"userAuthentication": {
"authorities": [
{
"authority": "ROLE_USER"
}
],
"details": {
"id": "106xxxxx",
"email": "[email protected]",
"verified_email": true,
"name": "xxxx yyyyyy",
"given_name": "xxxxxx",
"family_name": "yyyyy",
"link": "https://plus.google.com/xxxxxxxxxx",
"picture": "https://lh5.googleusercontent.com/xxxxxx/photo.jpg",
"locale": "en"
},
"authenticated": true,
"principal": "106xxxxx",
"credentials": "N/A",
"name": "106xxxxxxx"
},
"principal": "106xxxxxxxxxxx",
"clientOnly": false,
"credentials": "",
"oauth2Request": {
"clientId": "xxxxxxxxx.apps.googleusercontent.com",
"scope": [],
"requestParameters": {},
"resourceIds": [],
"authorities": [],
"approved": true,
"refresh": false,
"redirectUri": null,
"responseTypes": [],
"extensions": {},
"refreshTokenRequest": null,
"grantType": null
},
"name": "106xxxxxxxxxx"
}
But instead of returning all the data, I'd like to return only specific data I need. How do I get that data(specifically email, name, link, picture).
import org.springframework.security.oauth2.provider.OAuth2Authentication;
@SpringBootApplication
@RestController
public class DemoOAuth2Application {
@RequestMapping("/user")
public Authentication user(OAuth2Authentication authentication) {
LinkedHashMap<String, Object> properties = (LinkedHashMap<String, Object>) authentication.getUserAuthentication().getDetails();
return properties.get("email");
}
public static void main(String[] args) {
SpringApplication.run(DemoOAuth2Application.class, args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With