用途:本技能覆盖将 CometChat Android 集成从开发模式推进到生产环境的加固工作,包括把客户端 Auth Key 替换为服务端 Token 生成、用户管理 CRUD、ProGuard/R8 规则以及安全最佳实践。
适用场景:需要“设置生产认证”“用 Token 替换 Auth Key”“ProGuard 破坏 CometChat”“如何从后端创建 CometChat 用户”时使用。不适用于开发模式登录(用 cometchat-android-v5-core)、添加功能(用 cometchat-android-v5-features);推送通知生产配置见 cometchat-android-v5-push。
为何重要:开发模式下 CometChatUIKit.login(uid) 使用内嵌在 App 中的 Auth Key,任何人反编译 APK 后即可提取密钥并以任意用户身份登录,因此生产部署必须使用服务端生成 Token。
Token 认证流程:客户端 → 你的服务器 → CometChat REST API → auth token → 客户端,客户端调用 CometChatUIKit.loginWithAuthToken(token)。服务器调用 POST https://{APP_ID}.api-{REGION}.cometchat.io/v3/users/{uid}/auth_tokens,请求头为 appId 与 apiKey(REST API Key,不是 Auth Key)。
客户端实现:先从你自己的后端获取 token,再调用 CometChatUIKit.loginWithAuthToken(token, callback),在 onSuccess 中跳转聊天,在 onError 中处理错误。
ProGuard/R8:在 proguard-rules.pro 中加入 -keep class com.cometchat.** { ; }、-keep class com.cometchat.chatuikit.* { *; }、-dontwarn com.cometchat.**,否则 R8 会剥离运行时需要的 CometChat 类。
安全检查清单:客户端代码中移除 Auth Key;REST API Key 仅存于服务端;使用 loginWithAuthToken() 而非 login(uid);添加 ProGuard 规则;网络安全配置允许 CometChat 域名;登出时注销推送 token。
硬性规则:生产 APK 绝不携带 Auth Key,必须使用 loginWithAuthToken();REST API Key ≠ Auth Key,前者仅服务端使用,后者仅客户端开发用;必须添加 ProGuard keep 规则。资料未提供具体后端实现语言与示例,暂未提供。