Accessing the Authenticated OAuth2 User
Learn how to read the logged-in user's profile and attributes from an OAuth2 login in Spring Security using OAuth2User and OidcUser.
Accessing the Authenticated OAuth2 User is a free Spring Security 6 & JWT Authentication lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Security 6 & JWT Authentication learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
After Login, Then What?
Once a user signs in through an OAuth2 provider, your app needs their profile: name, email, and provider id. Spring Security exposes this through a principal object you can inject anywhere.
The OAuth2User Principal
For plain OAuth2 logins, the authenticated principal is an OAuth2User. It holds the provider's attributes as a map plus the granted authorities.
public interface OAuth2User {
Map<String, Object> getAttributes();
Collection<? extends GrantedAuthority> getAuthorities();
String getName();
}Injecting the Principal
Use the @AuthenticationPrincipal annotation on a controller parameter to receive the current user directly.
@GetMapping('/me')
public Map<String,Object> me(@AuthenticationPrincipal OAuth2User user) {
return user.getAttributes();
}Reading Specific Attributes
Pull individual fields with getAttribute. The available keys depend on the provider, for example name and email from Google.
String email = user.getAttribute('email');
String name = user.getAttribute('name');OIDC Logins and OidcUser
When the provider uses OpenID Connect, the principal is an OidcUser, a richer type that also exposes the ID token and standardized claims.
@GetMapping('/profile')
public String profile(@AuthenticationPrincipal OidcUser user) {
return user.getFullName();
}Standard OIDC Claims
OidcUser gives typed access to standard claims like getEmail(), getPicture(), and getPreferredUsername(), so you do not have to know each provider's raw keys.
String pic = user.getPicture();
String sub = user.getSubject();Getting the User Elsewhere
Outside controllers, read the principal from the SecurityContext.
Authentication auth = SecurityContextHolder
.getContext().getAuthentication();
OAuth2User user = (OAuth2User) auth.getPrincipal();Mapping to a Local User
You usually want a local account record. On first login, look up the user by email or provider subject; if none exists, create one.
User local = repo.findByEmail(user.getAttribute('email'))
.orElseGet(() -> repo.save(fromOAuth(user)));Custom OAuth2UserService
To transform attributes or add roles at login time, extend DefaultOAuth2UserService and override loadUser. Return your own enriched principal.
public OAuth2User loadUser(OAuth2UserRequest req) {
OAuth2User user = super.loadUser(req);
return enrichWithRoles(user);
}Provider Differs by registrationId
The same callback can serve multiple providers. Read the registrationId (google, github, etc.) from the request to know which provider's attribute schema to use.
String provider = req.getClientRegistration()
.getRegistrationId();Don't Trust Blindly
Treat provider attributes as input. Verify the email is marked verified when the provider supports it, and avoid using a mutable display name as a primary key.
Quick Check
Test your understanding of accessing the OAuth2 user.
Recap
You learned to read the authenticated OAuth2 user:
- Inject
OAuth2UserorOidcUserwith@AuthenticationPrincipal - Read attributes with
getAttributeor typed OIDC accessors - Map provider data to a local account on first login
- Customize with a
DefaultOAuth2UserServicesubclass
This connects external identity to your application's own user model.
Frequently asked questions
Is the “Accessing the Authenticated OAuth2 User” lesson free?
Yes — the full text of “Accessing the Authenticated OAuth2 User” is free to read here on the web, and the Spring Security 6 & JWT Authentication course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Security 6 & JWT Authentication course, upgrade to CoddyKit PRO.
What will I learn in “Accessing the Authenticated OAuth2 User”?
Learn how to read the logged-in user's profile and attributes from an OAuth2 login in Spring Security using OAuth2User and OidcUser. You practise Spring Security 6 & JWT Authentication with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Security 6 & JWT Authentication?
No prior experience is required. Spring Security 6 & JWT Authentication on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Accessing the Authenticated OAuth2 User” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Security 6 & JWT Authentication lesson?
Yes. Every Spring Security 6 & JWT Authentication lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- OAuth2 Client Setup
- Social Login Integration
- Custom OAuth2 Success Handler
- Accessing the Authenticated OAuth2 User