Granted Authoritiesによるロールベース認可
データベースからユーザーを認証した後、ロールと権限を使って認可し、Spring Securityでエンドポイントとメソッドを保護する方法を学びます。
「Granted Authoritiesによるロールベース認可」はCoddyKit上の無料Spring Security 6 & JWT Authenticationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Security 6 & JWT Authentication学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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
よくある質問
「Granted Authoritiesによるロールベース認可」レッスンは無料ですか?
はい。「Granted Authoritiesによるロールベース認可」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Security 6 & JWT Authenticationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Security 6 & JWT Authenticationコースには全4レッスンが含まれています。
「Granted Authoritiesによるロールベース認可」で何を学びますか?
データベースからユーザーを認証した後、ロールと権限を使って認可し、Spring Securityでエンドポイントとメソッドを保護する方法を学びます。 ブラウザで直接実行するハンズオンコードでSpring Security 6 & JWT Authenticationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Spring Security 6 & JWT Authenticationを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSpring Security 6 & JWT Authenticationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Granted Authoritiesによるロールベース認可」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSpring Security 6 & JWT Authenticationレッスンでコードを書いて実行できますか?
はい。すべてのSpring Security 6 & JWT Authenticationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- カスタムUserDetailsServiceの実装
- パスワードエンコーダーの理解
- データベースとのユーザー管理統合
- Granted Authoritiesによるロールベース認可