Granted Authorities ile Role Dayalı Yetkilendirme
Kullanıcıları bir veritabanında kimlik doğrulamasından geçirdikten sonra Spring Security'de uç noktaları ve yöntemleri güvenceye almak için rolleri ve yetkileri kullanarak yetkilendirmeyi öğrenin.
Granted Authorities ile Role Dayalı Yetkilendirme, CoddyKit'te ücretsiz bir Spring Security 6 & JWT Authentication dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Spring Security 6 & JWT Authentication öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Security 6 & JWT Authentication kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Authentication vs Authorization
You can now load users from a database and verify passwords. That is authentication (who you are). The next question is authorization (what you may do), driven by roles and authorities.
Authorities and Roles
Spring represents permissions as GrantedAuthority objects. A role is just an authority with a ROLE_ prefix, e.g. ROLE_ADMIN.
Assigning Authorities to a User
When building your UserDetails, attach the authorities the user holds.
User.withUsername('alice')
.password(encoded)
.roles('ADMIN', 'USER')
.build();Securing URLs by Role
In the filter chain, restrict paths with hasRole. Spring adds the ROLE_ prefix for you here.
http.authorizeHttpRequests(a -> a
.requestMatchers('/admin/**').hasRole('ADMIN')
.anyRequest().authenticated());Requiring Specific Authorities
For finer control use hasAuthority, which matches the authority string exactly with no prefix added.
http.authorizeHttpRequests(a -> a
.requestMatchers('/reports/**').hasAuthority('REPORT_READ'));Multiple Allowed Roles
hasAnyRole permits access if the user has at least one of several roles.
http.authorizeHttpRequests(a -> a
.requestMatchers('/staff/**').hasAnyRole('ADMIN', 'MANAGER'));Method-Level Security
Enable annotation-based security to protect service methods, not just URLs.
@EnableMethodSecurity
@Configuration
public class SecurityConfig { }Using @PreAuthorize
@PreAuthorize runs a SpEL expression before the method executes, blocking unauthorized callers.
@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(Long id) { }Checking the Current User
SpEL can reference the authenticated principal, e.g. to allow users to edit only their own data.
@PreAuthorize("#username == authentication.name")
public void updateProfile(String username) { }Mapping DB Roles to Authorities
In your UserDetailsService, convert role rows from the database into SimpleGrantedAuthority objects so authorization rules apply.
var auths = roles.stream()
.map(r -> new SimpleGrantedAuthority('ROLE_' + r))
.toList();Putting It Together
The full picture: authenticate from the DB, map roles to authorities, secure URLs with hasRole/hasAuthority, and protect methods with @PreAuthorize.
Quick Check
What is the difference between hasRole('ADMIN') and hasAuthority('ADMIN')?
Recap
You can now control what authenticated users may do:
- Roles are authorities with a
ROLE_prefix hasRole/hasAnyRolevs exacthasAuthority@EnableMethodSecurity+@PreAuthorizefor method-level rules- Map DB roles to
SimpleGrantedAuthorityin your UserDetailsService
Sıkça Sorulan Sorular
“Granted Authorities ile Role Dayalı Yetkilendirme” dersi ücretsiz mi?
Evet — “Granted Authorities ile Role Dayalı Yetkilendirme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Spring Security 6 & JWT Authentication kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Security 6 & JWT Authentication kursu toplamda 4 dersten oluşur.
“Granted Authorities ile Role Dayalı Yetkilendirme” dersinde ne öğreneceğim?
Kullanıcıları bir veritabanında kimlik doğrulamasından geçirdikten sonra Spring Security'de uç noktaları ve yöntemleri güvenceye almak için rolleri ve yetkileri kullanarak yetkilendirmeyi öğrenin. Spring Security 6 & JWT Authentication ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Spring Security 6 & JWT Authentication öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Spring Security 6 & JWT Authentication, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Granted Authorities ile Role Dayalı Yetkilendirme” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Spring Security 6 & JWT Authentication dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Spring Security 6 & JWT Authentication dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Özel UserDetailsService Uygulaması
- Parola Kodlayıcılarını Anlama
- Veritabanı Kullanıcı Yönetimi Entegrasyonu
- Granted Authorities ile Role Dayalı Yetkilendirme