目录

1.指纹认证

华为提供安全的指纹认证能力。如果系统安全存在问题,回调方法BioAuthnCallback.onAuthError(),会提供错误码BioAuthnPrompt.ERROR_SYS_INTEGRITY_FAILED。如果设备运行环境安全,则执行指纹认证。指纹认证的错误码详情,请参考接口文档。

注意:

· EMUI 4(API Level 23)及以上版本才支持指纹认证,另外请确保手机硬件支持指纹认证。

· 利用PromptInfo.Builder创建PromptInfo时,setDeviceCredentialAllowed(true)和setNegativeButtonText()互斥,只能二选一。setDeviceCredentialAllowed(true)允许在指纹认证页面,切换到其他认证方式,如锁屏密码。

· 使用BioAuthnPrompt.PromptInfo.Builder.setDeviceCredentialAllowed(true)注意事项:

1. 该功能不支持具有屏下指纹且运行EMUI 9.x系统的设备,例如Mate 20 Pro、P30、P30 Pro、Magic2,在这些设备上切换锁屏密码认证会立刻导致认证失败。

2. 在EMUI 9.x及以下版本,可能会出现仅能开启一次指纹认证的问题。解决措施如下(选择一种即可):

(1)Activity不重复创建BioAuthnPrompt对象。

(2)认证完成后,调用Activity.recreate()方法重绘界面。

(3)关闭Activity重新打开。

华为指纹认证能力使用方法,如以下代码片段所示:

  1. import android.view.View;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.core.content.ContextCompat;
  4. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnCallback;
  5. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnManager;
  6. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnPrompt;
  7. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnResult;
  8. public class MainActivity extends AppCompatActivity {
  9.    public void btnFingerAuthenticateWithoutCryptoObjectClicked(View view) {
  10.        // 是否支持指纹认证
  11.        BioAuthnManager bioAuthnManager = new BioAuthnManager(this);
  12.        int errorCode = bioAuthnManager.canAuth();
  13.        if (errorCode != 0) {
  14.            // 不支持指纹认证
  15.            return;
  16.        }
  17.        // 回调
  18.        BioAuthnCallback callback = new BioAuthnCallback() {
  19.            @Override
  20.            public void onAuthError(int errMsgId, CharSequence errString) {
  21.                // 认证错误
  22.            }
  23.            @Override
  24.            public void onAuthSucceeded(BioAuthnResult result) {
  25.                // 认证成功
  26.            }
  27.            @Override
  28.            public void onAuthFailed() {
  29.                // 认证失败
  30.            }
  31.        };
  32.        BioAuthnPrompt bioAuthnPrompt = new BioAuthnPrompt(this, ContextCompat.getMainExecutor(this), callback);
  33.        // 构建提示信息
  34.        BioAuthnPrompt.PromptInfo.Builder builder =
  35.            new BioAuthnPrompt.PromptInfo.Builder().setTitle("This is the title.")
  36.                .setSubtitle("This is the subtitle")
  37.                .setDescription("This is the description");
  38.        // 首先会提示用户使用指纹认证,但是也提供选项,可以使用PIN码、图形解锁或锁屏密码进行认证。
  39.        // 但是,如果这里设置为true,则不能设置setNegativeButtonText(CharSequence)。
  40.        builder.setDeviceCredentialAllowed(true);
  41.        // 设置取消按钮标题。如果设置了该值,则不能setDeviceCredentialAllowed(true)。
  42.        // builder.setNegativeButtonText("This is the 'Cancel' button.");
  43.        BioAuthnPrompt.PromptInfo info = builder.build();
  44.        bioAuthnPrompt.auth(info);
  45.    }
  46. }

2. 3D面容认证

华为提供安全的3D人脸认证能力。如果系统安全存在问题,回调函数BioAuthnCallback.onAuthError,会提供错误码FaceManager.FACE_ERROR_SYS_INTEGRITY_FAILED。如果设备运行环境安全,则执行3D面容认证。3D面容认证的错误码详情,参考接口文档。

注意:

1. EMUI 10(API Level 29)以上版本才支持面容认证,另外请确保设备硬件支持3D面容认证。

2. 使用CryptoObject对象认证时,使用KeyGenParameterSpec.Builder创建key时setUserAuthenticationRequired()必须为false。当前版本中,KeyStore没有与面容认证结果关联。

华为3D面容认证能力使用方法,如以下代码片段所示:

  1. import android.os.CancellationSignal;
  2. import android.os.Handler;
  3. import android.view.View;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnCallback;
  6. import com.huawei.hms.support.api.fido.bioauthn.BioAuthnResult;
  7. import com.huawei.hms.support.api.fido.bioauthn.CryptoObject;
  8. import com.huawei.hms.support.api.fido.bioauthn.FaceManager;
  9. public class MainActivity extends AppCompatActivity {
  10.    public void btnFaceAuthenticateWithoutCryptoObjectClicked(View view) {
  11.        // 回调
  12.        BioAuthnCallback callback = new BioAuthnCallback() {
  13.            @Override
  14.            public void onAuthError(int errMsgId, CharSequence errString) {
  15.                // 认证错误
  16.            }
  17.            @Override
  18.            public void onAuthHelp(int helpMsgId, CharSequence helpString) {
  19.                // 帮助
  20.            }
  21.            @Override
  22.            public void onAuthSucceeded(BioAuthnResult result) {
  23.                // 认证成功
  24.            }
  25.            @Override
  26.            public void onAuthFailed() {
  27.                // 认证失败
  28.            }
  29.        };
  30.        // 取消信号
  31.        CancellationSignal cancellationSignal = new CancellationSignal();
  32.        FaceManager faceManager = new FaceManager(this);
  33.        int errorCode = faceManager.canAuth();
  34.        if (errorCode != 0) {
  35.            // 不支持认证
  36.            return;
  37.        }
  38.        // Flags.
  39.        int flags = 0;
  40.        // 认证消息处理器
  41.        Handler handler = null;
  42.        // 推荐CryptoObject传null,当前版本,KeyStore没有和面容识别关联。
  43.        // KeyGenParameterSpec.Builder.setUserAuthenticationRequired()必须设置为false。
  44.        CryptoObject crypto = null;
  45.        faceManager.auth(crypto, cancellationSignal, flags, callback, handler);
  46.    }
  47. }
说明:
更多Sample信息请参考: fido-bioauthn-androidx-sample.zip。