Android生物认证Biometric 四十行代码轻松实现面部识别、指纹认证

Biometric

Biometric是谷歌官方提供的生物识别验证类库,能调用包括目前Android设备上搭载的指纹、人脸、虹膜等系统级的生物认证。(目前大多数的国内定制ROM可能因为安全问题,仅支持指纹)

要使用 Biometric 库添加生物识别身份验证,需要添加 Biometric 库的依赖项:

    dependencies {
        implementation 'androidx.biometric:biometric:1.0.0-beta01'
    }

检查生物识别身份验证是否可用

检查生物识别是否可用:通过在 BiometricManager 类中使用 canAuthenticate() 方法,在调用 BiometricPrompt 之前检查设备是否支持生物识别身份验证。

    BiometricManager biometricManager = BiometricManager.from(this);
    switch (biometricManager.canAuthenticate()) {
        case BiometricManager.BIOMETRIC_SUCCESS:
            Log.d("应用可以进行生物识别技术进行身份验证。");
            break;
        case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
            Log.e("该设备上没有搭载可用的生物特征功能。");
            break;
        case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
            Log.e("生物识别功能当前不可用。");
            break;
        case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
            Log.e("用户没有录入生物识别数据。");
            break;
    }

弹出对话框进行生物验证

在需要弹出对话框的 Activity 或 Fragment 中,使用以下代码段中所示的逻辑来显示对话框(机型为三星S8):

    private Handler handler = new Handler();

    private Executor executor = new Executor() {
        @Override
        public void execute(Runnable command) {
            handler.post(command);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //点击按钮弹出生物验证
        Button biometricLoginButton = findViewById(R.id.biometric_login);
        biometricLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBiometricPrompt();
            }
        });
    }

    //生物认证的setting
    private void showBiometricPrompt() {
        BiometricPrompt.PromptInfo promptInfo =
                new BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric login for my app") //设置大标题
                .setSubtitle("Log in using your biometric credential") // 设置标题下的提示
                .setNegativeButtonText("Cancel") //设置取消按钮
                .build();
                
    //需要提供的参数callback
    BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this,
                executor, new BiometricPrompt.AuthenticationCallback() {
            //各种异常的回调
            @Override
            public void onAuthenticationError(int errorCode,
                    @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(getApplicationContext(),
                    "Authentication error: " + errString, Toast.LENGTH_SHORT)
                    .show();
            }

            //认证成功的回调
            @Override
            public void onAuthenticationSucceeded(
                    @NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                BiometricPrompt.CryptoObject authenticatedCryptoObject =
                        result.getCryptoObject();
                // User has verified the signature, cipher, or message
                // authentication code (MAC) associated with the crypto object,
                // so you can use it in your app's crypto-driven workflows.
            }

            //认证失败的回调
            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                Toast.makeText(getApplicationContext(), "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show();
            }
        });

        // 显示认证对话框
        biometricPrompt.authenticate(promptInfo);
    }
    

生物认证对话框

允许使用密码、图案、PIN码等非生物识别认证

设置PromptInfo时调用setDeviceCredentialAllowed(true),允许利用设备 PIN 码、图案或密码进行身份验证。代码:

BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric login for my app")
            .setSubtitle("Log in using your biometric credential")
            .setDeviceCredentialAllowed(true)
            .build();
            

允许使用非生物认证


官方文档中同时使用了取消按钮 setDeviceCredentialAllowed(true)setNegativeButtonText("Cancel") 非生物认证
实测会报错 Can't have both negative button behavior and device credential enabled 所以没有添加取消按钮,问题待定

参考Android开发者文档(可能需要科学上网)

Android开源>AOSP>安全性>生物识别
Android开发者文档>显示生物识别身份验证对话框

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值