사용자 지정 클레임 및 보안 규칙
사용자 지정 클레임을 정의하고 Firebase 보안 규칙과 통합하여 리소스에 대한 액세스를 제어합니다.
사용자 지정 클레임 및 보안 규칙은(는) CoddyKit의 무료 Firebase Auth & Realtime Database Apps 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Firebase Auth & Realtime Database Apps 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Firebase Auth & Realtime Database Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Intro to Custom Claims
Beyond basic user authentication, Firebase allows you to define Custom Claims. These are key-value pairs that you can add to a user's ID token, providing extra information about the user.
Think of them as custom labels or badges attached to a user's identity.
Why Use Custom Claims?
Custom claims are powerful for implementing role-based access control (RBAC) or granting specific permissions within your app. Instead of just knowing 'who' the user is, you can know 'what' they are allowed to do.
- Designate users as 'admin', 'editor', or 'subscriber'.
- Grant access to premium features or content.
- Control data access based on custom attributes.
Setting Claims (Server-Side)
Custom claims are sensitive and must be set by a trusted environment, like your backend server, using the Firebase Admin SDK. This prevents malicious users from giving themselves elevated privileges.
When claims are set, the user's ID token is updated. Clients need to refresh their token to receive the new claims.
Simulating Claim Setting
This conceptual example shows how claims work. On a real backend, the Admin SDK would update a user's profile, and these claims would then be available in their ID token.
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String userId = "user123";
Map<String, Object> claims = new HashMap<>();
// --- Server-side action (simulated) ---
System.out.println("Server sets claims for " + userId);
claims.put("role", "admin");
claims.put("level", "premium");
// --- Client-side action (simulated after token refresh) ---
System.out.println("\nClient receives ID token with claims:");
System.out.println("User ID: " + userId);
System.out.println("Claims: " + claims);
// Client checks for specific claim
if (claims.containsKey("role") && claims.get("role").equals("admin")) {
System.out.println("Access check: User is an admin.");
} else {
System.out.println("Access check: Not an admin.");
}
}
}Accessing Claims (Client-Side)
Once a user is logged in and their ID token is refreshed (e.g., after login or explicitly refreshing), your client-side application can read these custom claims from the token.
The claims are embedded within the ID token, which is a JWT (JSON Web Token).
How Claims Power Rules
The true power of custom claims comes when you combine them with Firebase Security Rules. Any custom claim you set on a user's ID token is automatically available within your security rules.
This allows you to create highly specific and dynamic access control logic for your Realtime Database or Cloud Firestore.
Rule Example: Admin Access
Here's how a Firebase Realtime Database Security Rule might use a custom admin: true claim to restrict access to a specific data path.
Only users with this claim in their token would be able to read or write to /adminContent.
{
"rules": {
"adminContent": {
// Only users with 'admin: true' claim can read/write
".read": "auth.token.admin === true",
".write": "auth.token.admin === true"
},
"publicContent": {
// Anyone authenticated can read, no special claims needed
".read": "auth != null",
".write": "false"
}
}
}Rule Example: Premium Content
You can also use claims for different levels of access. This rule grants read access to /premiumContent only if the user has a level: 'premium' claim.
This is much more flexible than just checking if a user is logged in.
{
"rules": {
"premiumContent": {
// Only users with 'level: premium' claim can read
".read": "auth.token.level === 'premium'",
".write": "false"
},
"users": {
"$uid": {
".read": "auth.uid === $uid",
".write": "auth.uid === $uid"
}
}
}
}Best Practices for Claims
To ensure efficient and secure use of custom claims:
- Keep claims small: ID tokens have size limits.
- Don't store sensitive data: Claims are base64 encoded, not encrypted.
- Use for authorization: Not for general data storage.
- Token refresh: Remind users to refresh their ID token if claims change.
Claims Quiz
You've learned how custom claims enhance user roles and security rules. Which statement accurately describes a key aspect of Firebase Custom Claims?
Recap: Custom Claims & Rules
This lesson covered Firebase Custom Claims, a powerful feature for advanced user management.
- Custom claims allow you to add custom attributes to user ID tokens.
- They are set securely using the Firebase Admin SDK on your backend.
- These claims are seamlessly integrated with Firebase Security Rules, enabling robust, role-based access control for your app's resources.
- Always remember to refresh the client's ID token for changes to take effect.
자주 묻는 질문
“사용자 지정 클레임 및 보안 규칙” 강의는 무료인가요?
네 — “사용자 지정 클레임 및 보안 규칙” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Firebase Auth & Realtime Database Apps 강의 전체를 잠금 해제할 수 있습니다. Firebase Auth & Realtime Database Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
“사용자 지정 클레임 및 보안 규칙”에서 뭘 배우나요?
사용자 지정 클레임을 정의하고 Firebase 보안 규칙과 통합하여 리소스에 대한 액세스를 제어합니다. 브라우저에서 직접 실행하는 실습 코드로 Firebase Auth & Realtime Database Apps을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Firebase Auth & Realtime Database Apps을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Firebase Auth & Realtime Database Apps은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“사용자 지정 클레임 및 보안 규칙” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Firebase Auth & Realtime Database Apps 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Firebase Auth & Realtime Database Apps 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 전화번호 인증
- 다중 요소 인증(MFA)
- 사용자 지정 클레임 및 보안 규칙
- 계정 연결 및 인증 제공자 관리