android 生物识别
本文的重点 (The Takeaway From This Article)
Biometric authentication is an extension of fingerprint authentication. A biometric API is far more advanced and straightforward to integrate when compared to its predecessor. In this article, you’re going to learn how to integrate the biometric API and how to use it. Read on to learn about it.
生物特征认证是指纹认证的扩展。 与以前的版本相比,生物识别API更加先进且易于集成。 在本文中,您将学习如何集成生物识别API以及如何使用它。 继续阅读以了解它。
介绍 (Introduction)
In recent years, the Android team’s focus has been mostly on improving security and privacy. Pattern/pin lock has been around for years in Android, and as an enhancement, fingerprint authentication was introduced a couple of years ago. Now it’s time to make enhancements to fingerprint authentication, and that’s known as biometric authentication.
近年来,Android团队的重点主要放在改善安全性和隐私性上。 模式/密码锁在Android中已经存在了多年,并且作为一项增强功能,指纹认证是在几年前引入的。 现在是时候增强指纹身份验证了,这就是所谓的生物特征认证。
Biometric authentication is more secure and easier to use, which comes in handy in many situations, such as authorization for payments, secure and simple login, authentication while accessing sensitive data, and more.
生物特征认证更安全且更易于使用,在许多情况下都很方便,例如付款授权,安全简单的登录,访问敏感数据时进行认证等等。
In addition to that, the biometric API also provides a way to authenticate with a device password or pin without any extra work for developers.
除此之外,生物识别API还提供了一种使用设备密码或密码进行身份验证的方法,而无需开发人员进行任何额外的工作。
检查可用性 (Check Availability)
As I said, the Android security system started with password and pin locks, so some of the devices won’t have the capability of a fingerprint or face unlock. Before enabling this type of authentication for the user, it’s a good practice to check whether the device is compatible with biometric authentication.
就像我说的那样,Android安全系统以密码和密码锁开始,因此某些设备不具备指纹或面部解锁功能。 在为用户启用这种身份验证之前,最好先检查设备是否与生物特征认证兼容。
To check the availability first, we need to get the instance of the system BiometricManager
class, as shown below:
要首先检查可用性,我们需要获取系统BiometricManager
类的实例,如下所示:
val biometricManager = BiometricManager.from(this)
With the BiometricManager
instance, we need to access the canAuthenticate()
public function, which results in an integer.
使用BiometricManager
例如,我们需要访问canAuthenticate()
公共函数,该函数将产生一个整数。
canAuthenticate()
has four possible outcomes:
canAuthenticate()
有四个可能的结果:
BIOMETRIC_SUCCESS
: The device can work with biometric authentication.BIOMETRIC_SUCCESS
:设备可以使用生物特征认证。BIOMETRIC_ERROR_NO_HARDWARE
: No biometric features are available on this device.BIOMETRIC_ERROR_NO_HARDWARE
:此设备上没有生物特征。BIOMETRIC_ERROR_HW_UNAVAILABLE
: Biometric features are currently unavailable in the device.BIOMETRIC_ERROR_HW_UNAVAILABLE
:设备中当前不提供生物识别功能。BIOMETRIC_ERROR_NONE_ENROLLED
: The user hasn’t associated any biometric credentials in the device yet.BIOMETRIC_ERROR_NONE_ENROLLED
:用户尚未在设备中关联任何生物特征凭证。
Use the Kotlin mighty when
to deal with different use cases. Have a look:
when
使用Kotlin来应对不同的用例。 看一看:
积分 (Integration)
Biometric library integration is simple. We need to add the following line under the dependencies tag in build.gradle
at the app level. Have a look:
生物识别库集成非常简单。 我们需要在应用程序级别的build.gradle
中的依赖性标签下添加以下行。 看一看:
dependencies {
implementation 'androidx.biometric:biometric:1.0.1'
}
Have a look at the latest version.
看一下最新版本 。
使用生物识别库 (Work With Biometric Library)
This is the exciting part. First, we need to create an instance of an Executor
, as shown below:
这是令人兴奋的部分。 首先,我们需要创建一个Executor
的实例,如下所示:
val executor = ContextCompat.getMainExecutor(this)
getMainExecutor
returns an Executor
that runs enqueued tasks on the main thread associated with this context. This is the thread used to dispatch calls to application components (activities, services, etc.). This Executor
is used to create a bridge between the biometric services and your application components.
getMainExecutor
返回一个Executor
,该执行程序在与此上下文关联的主线程上运行排队的任务。 这是用于将调用分派到应用程序组件(活动,服务等)的线程。 该Executor
程序用于在生物识别服务和您的应用程序组件之间建立桥梁。
Next, we need to create an instance of BiometricPrompt
, as shown below, to prompt the user with authentication. It has three arguments.
接下来,我们需要创建一个BiometricPrompt
实例,如下所示,以提示用户进行身份验证。 它有三个参数。
- An instance of the Android component (activity/fragment) Android组件的实例(活动/片段)
An
Executor
, which we created above我们在上面创建的
Executor
A lambda expression with three callbacks, known as
Biometric.AuthenticationCallback
具有三个回调的lambda表达式,称为
Biometric.AuthenticationCallback
It’s time to post necessary information to the prompt through PromptInfo
builder, as shown below:
现在是时候通过PromptInfo
将必要的信息发布到提示了 生成器,如下所示:
All that’s left is to show the biometric dialog using authenticate
on the BiometricPrompt
instance by passing promptInfo
as a parameter.
剩下的就是通过将promptInfo
作为参数在BiometricPrompt
实例上使用authenticate
来显示生物识别对话框。
biometricPrompt.authenticate(promptInfo)
That’s all; the rest of the work will be taken care of by the biometric API and will provide the appropriate result through callbacks.
就这样; 其余的工作将由生物识别API处理,并将通过回调提供适当的结果。
To understand it a bit more clearly, see the following code, where all the pieces are put together:
为了更清楚地理解它,请参阅以下代码,其中所有部分都放在一起:
启用密码选项 (Enable Password Option)
There will be times when your fingerprint option or face detection might have problems. For instance, when there is insufficient light for face detection, it might not work as it’s supposed to. In situations like this, the user should be able to use other modes of authentication, like pin/password/pattern.
有时您的指纹选项或面部检测可能会出现问题。 例如,当光线不足以进行人脸检测时,它可能无法正常工作。 在这种情况下,用户应该能够使用其他身份验证模式,例如密码/密码/模式。
To enable that, the biometric library provides a function setDeviceCredentialAllowed
on the BiometricPrompt.PromptInfo
instance by passing true
as a parameter. Have a look:
为此,生物特征库通过将true
作为参数传递来在BiometricPrompt.PromptInfo
实例上提供函数setDeviceCredentialAllowed
。 看一看:
奖金 (Bonus)
To learn more about security in Android, read the following article:
要了解有关Android安全性的更多信息,请阅读以下文章:
Thank you for reading.
感谢您的阅读。
翻译自: https://medium.com/better-programming/how-to-set-up-biometric-authentication-in-android-672688afcaae
android 生物识别
所有评论(0)