الوصول إلى مستخدم OAuth2 المُصادَق عليه
تعلّم كيفية قراءة ملف المستخدم الذي سجّل الدخول وسماته من عملية تسجيل الدخول عبر OAuth2 في Spring Security باستخدام OAuth2User وOidcUser
الوصول إلى مستخدم OAuth2 المُصادَق عليه درس مجاني في Spring Security 6 & JWT Authentication على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Security 6 & JWT Authentication، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Security 6 & JWT Authentication 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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.
تعلم Java مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «الوصول إلى مستخدم OAuth2 المُصادَق عليه» مجاني؟
نعم — نص درس «الوصول إلى مستخدم OAuth2 المُصادَق عليه» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Security 6 & JWT Authentication، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Security 6 & JWT Authentication 4 دروس في المجموع.
ماذا ستتعلم في «الوصول إلى مستخدم OAuth2 المُصادَق عليه»؟
تعلّم كيفية قراءة ملف المستخدم الذي سجّل الدخول وسماته من عملية تسجيل الدخول عبر OAuth2 في Spring Security باستخدام OAuth2User وOidcUser تتمرن على Spring Security 6 & JWT Authentication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Spring Security 6 & JWT Authentication؟
لا تُشترط خبرة سابقة. Spring Security 6 & JWT Authentication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «الوصول إلى مستخدم OAuth2 المُصادَق عليه»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Spring Security 6 & JWT Authentication هذا؟
نعم. كل درس في Spring Security 6 & JWT Authentication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد عميل OAuth2
- تكامل تسجيل الدخول عبر الشبكات الاجتماعية
- معالج نجاح OAuth2 مخصّص
- الوصول إلى مستخدم OAuth2 المُصادَق عليه