SME*_*SME 4 spring-security firebase firebase-authentication
我正在开发一个需要通过服务器应用程序进行身份验证的 Android 应用程序。服务器应用程序是一个使用 Spring Security 的 Spring Boot 应用程序。服务器应用程序使用自定义身份验证提供程序为用户设置授予权限。
这是我用来检索 idToken 的代码:
FirebaseToken firebaseToken = null;
try {
FirebaseApp app = FirebaseUtil.getFirebaseApp();
firebaseToken = FirebaseAuth.getInstance(app).verifyIdTokenAsync(authenticationToken.getIdToken()).get();
} catch ( InterruptedException | ExecutionException | CancellationException e ) {
throw new AuthenticationServiceException(e.getMessage());
}
return firebaseToken;
Run Code Online (Sandbox Code Playgroud)
一个我有 Firebase 令牌,我可以像这样检索声明:
Map<String, Object> claims = token.getClaims();
Run Code Online (Sandbox Code Playgroud)
然后我可以迭代声明并创建权限,如下所示:
List<GrantedAuthority> authorities = Lists.newArrayList();
authorities.add(new SimpleGrantedAuthority("some_role"));
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用 Firebase 控制台创建声明。这可能吗?我怀疑我需要使用 Firebase 数据库,但在 firebase 文档中找不到我正在寻找的内容。
Firebase 身份验证的自定义声明目前只能通过 Firebase Admin SDK 创建。从创建自定义声明的文档中:
Run Code Online (Sandbox Code Playgroud)// Set admin privilege on the user corresponding to uid. admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => { // The new custom claims will propagate to the user's ID token the // next time a new one is issued. });
允许从控制台创建声明也不是一个坏主意,因此我建议您提交功能请求。
小智 5
如果您一直在本地主机上使用 Firebase Emulator Suite,您可能已经注意到,您可以直接在该控制台中编辑用户以添加自定义声明,例如 {"role":"admin"}。只需转到“身份验证”选项卡,单击特定用户的溢出菜单,选择“编辑用户”,然后在文本框中设置自定义声明。
您来到这里可能是因为您发现“编辑用户”选项没有出现在生产 Firebase 控制台中,因此您需要弄清楚如何使用代码执行相同的操作。以下是我使用 Python、服务帐户和 Firebase 管理工具从 Windows 设置自定义声明所采取的步骤。Linux 或 OSX 上的步骤应该类似。这假设您已经安装了 python 和 pip。
https://console.firebase.google.com/project/<your_project>/settings/serviceaccounts/adminsdk
pip 安装 firebase-admin
python通过从命令行运行打开 python 终端并执行以下代码行: 导入 firebase_admin
从 firebase_admin 导入凭据、身份验证
cred = credentials.Certificate("c:\\Users\\<path_to_my_credentials_file>.json")
default_app = firebase_admin.initialize_app(cred)
user = auth.get_user_by_email('<用户电子邮件地址>')
auth.set_custom_user_claims(user.uid, {'角色': '管理员'})
# 验证更改是否有效:
user = auth.get_user_by_email('<用户电子邮件地址>')
打印(用户.custom_claims)
| 归档时间: |
|
| 查看次数: |
2720 次 |
| 最近记录: |