
User15857131905347322946 (Customer) asked a question.
Hello!
I'm trying to find some example of how to get the user group name and roles in a Spring Boot application.
So far, I've gotten the user attributes and the Authority (which is alway "ROLE_USER"). But the part I'm missing is to how to get the list of groups the user belongs to?
Here's the code so far:
@GetMapping("/print-username")
public ResponseEntity<?> printUsername(@AuthenticationPrincipal OAuth2User principal) {
if (principal == null) {
return new ResponseEntity<>("User not authenticated", HttpStatus.OK);
} else {
Map<String, Object> attributes = principal.getAttributes();
// Print user attributes
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("User Attributes:\n");
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
stringBuilder.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
System.out.println(stringBuilder.toString());
// Print user roles
Collection<? extends GrantedAuthority> authorities = principal.getAuthorities();
for (GrantedAuthority authority : authorities) {
System.out.println("Authority: " + authority.getAuthority());
}
String username = (String) attributes.get("email"); // Assuming email is the user identifier
System.out.println("Username: " + username);
return new ResponseEntity<>("Username printed in the server console", HttpStatus.OK);
}
}
Any ideas?

Hi @User15857131905347322946 (Customer) , Thank you for reaching out to the Okta Community!
This question is more appropriate for our dedicated Okta Developer Forum.
My advice would be to reach out devforum.okta.com to take advantage of their expertise.
While we'll do our best to answer all of your questions here, this medium is more inclined towards Okta core products and features (non-developer work)
In the meantime, here are some older posts that might be relevant to your implementation:
https://devforum.okta.com/t/unable-to-find-the-group-information-of-the-logged-in-user-jwt-token-okta/15759
https://devforum.okta.com/t/list-users-and-groups-with-and-administrator-read-only-account-with-no-login-page-prompted/15925
If my answer helped, remember to mark it as best to increase its visibility for other members of the Okta Community who might have the same questions as you.
Hope my answer helps!
--------------------------------
💡 Community Moderator Tip: Join a group today and connect with other Okta customers by region or product.