0Pricing
Spring Security 6 & JWT Authentication · Ders

JWT Bildirimlerini Spring Yetkilerine Eşleme

Bir kaynak sunucusunun, ayrıntılı erişim denetimi için JwtAuthenticationConverter kullanarak JWT bildirimlerini Spring Security GrantedAuthorities değerlerine nasıl dönüştürdüğünü öğrenin.

JWT Bildirimlerini Spring Yetkilerine Eşleme, 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.

From Claims to Authorities

A resource server validates a JWT, but to enforce access it needs Spring GrantedAuthority objects. The bridge between raw claims and authorities is the JwtAuthenticationConverter.

The Default Scope Mapping

By default Spring reads the scope or scp claim, splits it on spaces, and prefixes each value with SCOPE_. So a scope of read becomes the authority SCOPE_read.

// scope: 'read write'  ->  SCOPE_read, SCOPE_write

Checking Scope Authorities

You can require these authorities in your security config or with annotations.

http.authorizeHttpRequests(auth -> auth
    .requestMatchers('/api/data').hasAuthority('SCOPE_read'));

The Problem with Roles

Many identity providers put roles in a custom claim like roles or realm_access.roles, not in scope. The default converter ignores those, so you must customize it.

Building a Custom Converter

Create a JwtGrantedAuthoritiesConverter and point it at the claim that holds your roles.

JwtGrantedAuthoritiesConverter c = new JwtGrantedAuthoritiesConverter();
c.setAuthoritiesClaimName('roles');
c.setAuthorityPrefix('ROLE_');

Wrapping in JwtAuthenticationConverter

Wrap the authorities converter inside a JwtAuthenticationConverter, which produces the final authentication token.

JwtAuthenticationConverter conv = new JwtAuthenticationConverter();
conv.setJwtGrantedAuthoritiesConverter(c);

Registering the Converter

Tell the resource server to use your converter inside the JWT configuration.

http.oauth2ResourceServer(o -> o
    .jwt(j -> j.jwtAuthenticationConverter(conv)));

Nested Claims

Some providers nest roles, e.g. Keycloak uses realm_access.roles. The simple converter cannot read nested paths, so write a lambda converter that drills into the structure.

Converter<Jwt, Collection<GrantedAuthority>> conv = jwt -> {
    Map<String,Object> realm = jwt.getClaim('realm_access');
    List<String> roles = (List<String>) realm.get('roles');
    return roles.stream()
        .map(r -> new SimpleGrantedAuthority('ROLE_' + r))
        .collect(Collectors.toList());
};

Combining Scopes and Roles

You may want both scope-based and role-based authorities. Merge two converters' results so a single principal carries both SCOPE_ and ROLE_ authorities.

Customizing the Principal Name

By default the principal name is the sub claim. Override setPrincipalClaimName if you prefer to identify users by, say, preferred_username.

conv.setPrincipalClaimName('preferred_username');

Verifying the Mapping

Test with a mock JWT that carries the roles claim and assert the request succeeds only when the expected authority is present.

mockMvc.perform(get('/api/admin')
    .with(jwt().authorities(new SimpleGrantedAuthority('ROLE_admin'))))
    .andExpect(status().isOk());

Quick Check

Test your understanding of claim-to-authority mapping.

Recap

You learned to map JWT claims to Spring authorities:

  • Default mapping turns scope into SCOPE_ authorities
  • Use JwtGrantedAuthoritiesConverter to read custom role claims
  • Write a lambda converter for nested claims like realm_access.roles
  • Register it via jwtAuthenticationConverter

This gives your resource server precise, claim-driven access control.

Sıkça Sorulan Sorular

“JWT Bildirimlerini Spring Yetkilerine Eşleme” dersi ücretsiz mi?

Evet — “JWT Bildirimlerini Spring Yetkilerine Eşleme” 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.

“JWT Bildirimlerini Spring Yetkilerine Eşleme” dersinde ne öğreneceğim?

Bir kaynak sunucusunun, ayrıntılı erişim denetimi için JwtAuthenticationConverter kullanarak JWT bildirimlerini Spring Security GrantedAuthorities değerlerine nasıl dönüştürdüğünü öğ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.

“JWT Bildirimlerini Spring Yetkilerine Eşleme” 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

  1. Kaynak sunucusu kurulumu
  2. JWT'lerin kodunu çözme ve doğrulama
  3. Kapsam ve taleplerin uygulanması
  4. JWT Bildirimlerini Spring Yetkilerine Eşleme
← Spring Security 6 & JWT Authentication Sayfasına Dön