“密码智能锁”已于 2022 年弃用,现已从 Google Play 服务身份验证 SDK (com.google.android.gms:play-services-auth
) 中移除。为最大程度地减少可能影响现有集成的破坏性变更,Play 商店中所有现有应用的智能锁功能将继续正常运行。使用更新后的 SDK (com.google.android.gms:play-services-auth:21.0.0
) 编译的新应用版本将无法再访问密码智能锁 API,并且无法成功构建。所有开发者都应尽快将其 Android 项目迁移到使用凭据管理器。
迁移到凭据管理器 API 的优势
凭据管理器提供了一个简单统一的 API,可支持现代功能和实践,同时提升用户的身份验证体验。
- 凭据管理器全面支持密码保存和身份验证。这意味着您的应用从“密码智能锁”迁移到凭据管理器时,用户不会中断。
- 凭据管理器集成了对多种登录方式的支持,包括通行密钥和通过 Google 登录等联合登录方式,以提高安全性并在您将来计划支持这些方式时促成转化。
- 从 Android 14 开始,凭据管理器支持第三方密码和通行密钥提供方。
- 凭据管理器为所有身份验证方法提供了一致统一的用户体验。
迁移选项:
用例 | 建议 |
---|---|
保存密码并使用已保存的密码登录 | 使用使用凭据管理器登录用户指南中的密码选项。密码保存和身份验证的详细步骤稍后会介绍。 |
通过 Google 登录 | 遵循将凭据管理器与“通过 Google 登录”集成指南。 |
向用户显示手机号码提示 | 使用 Google Identity Services 中的 Phone Number Hint API。 |
使用凭据管理器通过密码登录
要使用凭据管理器 API,请完成凭据管理器指南的前提条件部分中列出的步骤,并确保您执行以下操作:
- 添加所需的依赖项
- 在 ProGuard 文件中保留类
- 添加对数字资产链接的支持(如果您使用的是“密码智能锁”,则应已实现此步骤)
- 配置凭据管理器
- 指明凭据字段
- 保存用户的密码
使用已保存的密码登录
要检索与用户账号关联的密码选项,请完成以下步骤:
1. 初始化密码和身份验证选项
Kotlin
// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()
Java
// Retrieves the user's saved password for your app from their
// password provider.
GetPasswordOption getPasswordOption = new GetPasswordOption();
2. 使用上一步检索到的选项构建登录请求
Kotlin
val getCredRequest = GetCredentialRequest(
listOf(getPasswordOption)
)
Java
GetCredentialRequest getCredRequest = new GetCredentialRequest.Builder()
.addCredentialOption(getPasswordOption)
.build();
3. 启动登录流程
Kotlin
coroutineScope.launch {
try {
// Attempt to retrieve the credential from the Credential Manager.
val result = credentialManager.getCredential(
// Use an activity-based context to avoid undefined system UI
// launching behavior.
context = activityContext,
request = getCredRequest
)
// Process the successfully retrieved credential.
handleSignIn(result)
} catch (e: GetCredentialException) {
// Handle any errors that occur during the credential retrieval
// process.
handleFailure(e)
}
}
fun handleSignIn(result: GetCredentialResponse) {
// Extract the credential from the response.
val credential = result.credential
// Determine the type of credential and handle it accordingly.
when (credential) {
is PasswordCredential -> {
val username = credential.id
val password = credential.password
// Use the extracted username and password to perform
// authentication.
}
else -> {
// Handle unrecognized credential types.
Log.e(TAG, "Unexpected type of credential")
}
}
}
private fun handleFailure(e: GetCredentialException) {
// Handle specific credential retrieval errors.
when (e) {
is GetCredentialCancellationException -> {
/* This exception is thrown when the user intentionally cancels
the credential retrieval operation. Update the application's state
accordingly. */
}
is GetCredentialCustomException -> {
/* This exception is thrown when a custom error occurs during the
credential retrieval flow. Refer to the documentation of the
third-party SDK used to create the GetCredentialRequest for
handling this exception. */
}
is GetCredentialInterruptedException -> {
/* This exception is thrown when an interruption occurs during the
credential retrieval flow. Determine whether to retry the
operation or proceed with an alternative authentication method. */
}
is GetCredentialProviderConfigurationException -> {
/* This exception is thrown when there is a mismatch in
configurations for the credential provider. Verify that the
provider dependency is included in the manifest and that the
required system services are enabled. */
}
is GetCredentialUnknownException -> {
/* This exception is thrown when the credential retrieval
operation fails without providing any additional details. Handle
the error appropriately based on the application's context. */
}
is GetCredentialUnsupportedException -> {
/* This exception is thrown when the device does not support the
Credential Manager feature. Inform the user that credential-based
authentication is unavailable and guide them to an alternative
authentication method. */
}
is NoCredentialException -> {
/* This exception is thrown when there are no viable credentials
available for the user. Prompt the user to sign up for an account
or provide an alternative authentication method. Upon successful
authentication, store the login information using
androidx.credentials.CredentialManager.createCredential to
facilitate easier sign-in the next time. */
}
else -> {
// Handle unexpected exceptions.
Log.w(TAG, "Unexpected exception type: ${e::class.java.name}")
}
}
}
Java
credentialManager.getCredentialAsync(
// Use activity based context to avoid undefined
// system UI launching behavior
activity,
getCredRequest,
cancellationSignal,
<executor>,
new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
@Override
public void onSuccess(GetCredentialResponse result) {
handleSignIn(result);
}
@Override
public void onFailure(GetCredentialException e) {
handleFailure(e);
}
}
);
public void handleSignIn(GetCredentialResponse result) {
// Handle the successfully returned credential.
Credential credential = result.getCredential();
if (credential instanceof PasswordCredential) {
String username = ((PasswordCredential) credential).getId();
String password = ((PasswordCredential) credential).getPassword();
// Use ID and password to send to your server to validate and
// authenticate
} else {
// Catch any unrecognized credential type here.
Log.e(TAG, "Unexpected type of credential");
}
}
public void handleFailure(GetCredentialException e) {
if (e instanceof GetCredentialCancellationException) {
/* During the get credential flow, this is thrown when a user
intentionally cancels an operation. When this happens, the application
should handle logic accordingly, typically under indication the user
does not want to see Credential Manager anymore. */
} else if (e instanceof GetCredentialCustomException) {
/* Represents a custom error thrown during a get flow with
CredentialManager. If you get this custom exception, you should match
its type against exception constants defined in any third-party sdk
with which you used to make the
androidx.credentials.GetCredentialRequest, and then handle it
according to the sdk recommendation. */
} else if (e instanceof GetCredentialInterruptedException) {
/* During the get credential flow, this is thrown when some
interruption occurs that may warrant retrying or at least does not
indicate a purposeful desire to close or tap away from Credential
Manager. */
} else if (e instanceof GetCredentialProviderConfigurationException) {
/* During the get credential flow, this is thrown when configurations
are mismatched for the provider, typically indicating the provider
dependency is missing in the manifest or some system service is not
enabled. */
} else if (e instanceof GetCredentialUnknownException) {
// This is thrown when the get credential operation failed with no
// more details.
} else if (e instanceof GetCredentialUnsupportedException) {
/* During the get credential flow, this is thrown when credential
manager is unsupported, typically because the device has disabled it
or did not ship with this feature enabled. */
} else if (e instanceof NoCredentialException) {
/* During the get credential flow, this is returned when no viable
credential is available for the user. This can be caused by various
scenarios such as that the user doesn't have any credential or the
user doesn't grant consent to using any available credential. Upon
this exception, your app should navigate to use the regular app
sign-up or sign-in screen. When that succeeds, you should invoke
androidx.credentials.CredentialManager.createCredentialAsync to store
the login info, so that your user can sign in more easily through
Credential Manager the next time. */
} else {
Log.w(TAG, "Unexpected exception type " + e.getClass().getName());
}
}