แสดงกล่องโต้ตอบการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก

วิธีหนึ่งในการปกป้องข้อมูลที่ละเอียดอ่อนหรือเนื้อหาพรีเมียมภายในแอปคือการขอการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก เช่น การใช้การจดจำใบหน้าหรือการจดจำลายนิ้วมือ คู่มือนี้จะอธิบายวิธีรองรับขั้นตอนการเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกในแอป

ตามกฎทั่วไป คุณควรใช้เครื่องมือจัดการข้อมูลเข้าสู่ระบบสำหรับการลงชื่อเข้าใช้ครั้งแรก อุปกรณ์ การให้สิทธิ์ซ้ำครั้งต่อๆ ไปจะทำได้ด้วย Biometric Prompt หรือเครื่องมือจัดการข้อมูลเข้าสู่ระบบ ข้อดีของการใช้ Biometric Prompt คือ ตัวเลือกการปรับแต่งที่มากขึ้น ในขณะที่เครื่องมือจัดการข้อมูลเข้าสู่ระบบมี ในทั้ง 2 ขั้นตอน

ประกาศประเภทการตรวจสอบสิทธิ์ที่แอปของคุณรองรับ

หากต้องการกำหนดประเภทการตรวจสอบสิทธิ์ที่แอปของคุณรองรับ ให้ใช้ BiometricManager.Authenticators ของ Google ระบบช่วยให้คุณประกาศประเภท การตรวจสอบสิทธิ์:

BIOMETRIC_STRONG
การตรวจสอบสิทธิ์โดยใช้ข้อมูลไบโอเมตริกคลาส 3 ตามที่กำหนดไว้ใน ความเข้ากันได้กับ Android คำจำกัดความ
BIOMETRIC_WEAK
การตรวจสอบสิทธิ์โดยใช้ข้อมูลไบโอเมตริกระดับ 2 ตามที่ระบุไว้ในหน้าคำจำกัดความของความเข้ากันได้กับ Android
DEVICE_CREDENTIAL
การตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบของการล็อกหน้าจอ – PIN, รูปแบบ หรือ รหัสผ่าน

หากต้องการเริ่มใช้โปรแกรมตรวจสอบสิทธิ์ ผู้ใช้จะต้องสร้าง PIN, รูปแบบ หรือรหัสผ่าน หากยังไม่มี ขั้นตอนลงทะเบียนข้อมูลไบโอเมตริกจะแจ้งให้ผู้ใช้สร้างข้อมูลดังกล่าว

หากต้องการกำหนดประเภทการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกที่แอปยอมรับ ให้ส่งประเภทการตรวจสอบสิทธิ์หรือชุดค่าผสมแบบบิตของประเภทต่างๆ ไปยังเมธอด setAllowedAuthenticators() ข้อมูลโค้ดต่อไปนี้แสดงวิธีรองรับการตรวจสอบสิทธิ์โดยใช้ข้อมูลไบโอเมตริกระดับ 3 หรือข้อมูลเข้าสู่ระบบการล็อกหน้าจอ

Kotlin

// Lets the user authenticate using either a Class 3 biometric or
// their lock screen credential (PIN, pattern, or password).
promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setAllowedAuthenticators(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
        .build()

Java

// Lets user authenticate using either a Class 3 biometric or
// their lock screen credential (PIN, pattern, or password).
promptInfo = new BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setAllowedAuthenticators(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)
        .build();

ระบบไม่รองรับชุดค่าผสมของประเภทโปรแกรมตรวจสอบสิทธิ์ต่อไปนี้ใน Android 10 (API ระดับ 29) และต่ำกว่า DEVICE_CREDENTIAL และ BIOMETRIC_STRONG | DEVICE_CREDENTIAL หากต้องการตรวจสอบว่ามี PIN หรือไม่ หรือรหัสผ่านใน Android 10 และต่ำกว่า ให้ใช้ KeyguardManager.isDeviceSecure()

ตรวจสอบว่ามีการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก

หลังจากที่คุณเลือกองค์ประกอบการตรวจสอบสิทธิ์ที่แอปของคุณรองรับได้แล้ว ให้ตรวจสอบว่า องค์ประกอบเหล่านี้จะพร้อมใช้งาน โดยส่งการรวมประเภทแบบบิตต่อบิตเดียวกันกับที่คุณประกาศโดยใช้เมธอด setAllowedAuthenticators() ไปยังเมธอด canAuthenticate() หากจําเป็น ให้เรียกใช้การดำเนินการของ Intent ACTION_BIOMETRIC_ENROLL ในส่วน Intent เพิ่มเติม ให้ระบุชุด Authenticator ที่แอปของคุณ ยอมรับ Intent นี้จะแจ้งให้ผู้ใช้ลงทะเบียนข้อมูลเข้าสู่ระบบสำหรับโปรแกรมตรวจสอบสิทธิ์ที่แอปของคุณยอมรับ

Kotlin

val biometricManager = BiometricManager.from(this)
when (biometricManager.canAuthenticate(BIOMETRIC_STRONG or DEVICE_CREDENTIAL)) {
    BiometricManager.BIOMETRIC_SUCCESS ->
        Log.d("MY_APP_TAG", "App can authenticate using biometrics.")
    BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
        Log.e("MY_APP_TAG", "No biometric features available on this device.")
    BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->
        Log.e("MY_APP_TAG", "Biometric features are currently unavailable.")
    BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
        // Prompts the user to create credentials that your app accepts.
        val enrollIntent = Intent(Settings.ACTION_BIOMETRIC_ENROLL).apply {
            putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
                BIOMETRIC_STRONG or DEVICE_CREDENTIAL)
        }
        startActivityForResult(enrollIntent, REQUEST_CODE)
    }
}

Java

BiometricManager biometricManager = BiometricManager.from(this);
switch (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL)) {
    case BiometricManager.BIOMETRIC_SUCCESS:
        Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
        break;
    case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
        Log.e("MY_APP_TAG", "No biometric features available on this device.");
        break;
    case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
        Log.e("MY_APP_TAG", "Biometric features are currently unavailable.");
        break;
    case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
        // Prompts the user to create credentials that your app accepts.
        final Intent enrollIntent = new Intent(Settings.ACTION_BIOMETRIC_ENROLL);
        enrollIntent.putExtra(Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED,
                BIOMETRIC_STRONG | DEVICE_CREDENTIAL);
        startActivityForResult(enrollIntent, REQUEST_CODE);
        break;
}

ระบุวิธีที่ผู้ใช้ตรวจสอบสิทธิ์

หลังจากผู้ใช้ตรวจสอบสิทธิ์แล้ว คุณสามารถตรวจสอบว่าผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบของอุปกรณ์หรือข้อมูลเข้าสู่ระบบไบโอเมตริกโดยเรียกใช้ getAuthenticationType()

แสดงข้อความแจ้งให้เข้าสู่ระบบ

หากต้องการแสดงข้อความแจ้งของระบบที่ขอให้ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบไบโอเมตริก ให้ใช้ไลบรารีข้อมูลไบโอเมตริก ช่วงเวลานี้ กล่องโต้ตอบที่ระบบมีให้สอดคล้องกันในทุกแอปที่ใช้กล่องโต้ตอบนั้น เพื่อประสบการณ์ของผู้ใช้ที่น่าเชื่อถือยิ่งขึ้น ตัวอย่างกล่องโต้ตอบจะปรากฏในรูปที่ 1

ภาพหน้าจอแสดงกล่องโต้ตอบ
รูปที่ 1. กล่องโต้ตอบของระบบที่ขอการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก

หากต้องการเพิ่มการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกลงในแอปโดยใช้คลังข้อมูลไบโอเมตริก ให้ทำตามขั้นตอนต่อไปนี้

  1. ในไฟล์ build.gradle ของโมดูลแอป ให้เพิ่ม Dependency ของ androidx.biometric ไลบรารี

  2. ในกิจกรรมหรือส่วนย่อยที่โฮสต์กล่องโต้ตอบการเข้าสู่ระบบด้วยข้อมูลไบโอเมตริก โดยใช้ตรรกะที่แสดงในข้อมูลโค้ดต่อไปนี้

    Kotlin

    private lateinit var executor: Executor
    private lateinit var biometricPrompt: BiometricPrompt
    private lateinit var promptInfo: BiometricPrompt.PromptInfo
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        executor = ContextCompat.getMainExecutor(this)
        biometricPrompt = BiometricPrompt(this, executor,
                object : BiometricPrompt.AuthenticationCallback() {
            override fun onAuthenticationError(errorCode: Int,
                    errString: CharSequence) {
                super.onAuthenticationError(errorCode, errString)
                Toast.makeText(applicationContext,
                    "Authentication error: $errString", Toast.LENGTH_SHORT)
                    .show()
            }
    
            override fun onAuthenticationSucceeded(
                    result: BiometricPrompt.AuthenticationResult) {
                super.onAuthenticationSucceeded(result)
                Toast.makeText(applicationContext,
                    "Authentication succeeded!", Toast.LENGTH_SHORT)
                    .show()
            }
    
            override fun onAuthenticationFailed() {
                super.onAuthenticationFailed()
                Toast.makeText(applicationContext, "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show()
            }
        })
    
        promptInfo = BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric login for my app")
                .setSubtitle("Log in using your biometric credential")
                .setNegativeButtonText("Use account password")
                .build()
    
        // Prompt appears when user clicks "Log in".
        // Consider integrating with the keystore to unlock cryptographic operations,
        // if needed by your app.
        val biometricLoginButton =
                findViewById<Button>(R.id.biometric_login)
        biometricLoginButton.setOnClickListener {
            biometricPrompt.authenticate(promptInfo)
        }
    }

    Java

    private Executor executor;
    private BiometricPrompt biometricPrompt;
    private BiometricPrompt.PromptInfo promptInfo;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        executor = ContextCompat.getMainExecutor(this);
        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);
                Toast.makeText(getApplicationContext(),
                    "Authentication succeeded!", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                Toast.makeText(getApplicationContext(), "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show();
            }
        });
    
        promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle("Biometric login for my app")
                .setSubtitle("Log in using your biometric credential")
                .setNegativeButtonText("Use account password")
                .build();
    
        // Prompt appears when user clicks "Log in".
        // Consider integrating with the keystore to unlock cryptographic operations,
        // if needed by your app.
        Button biometricLoginButton = findViewById(R.id.biometric_login);
        biometricLoginButton.setOnClickListener(view -> {
                biometricPrompt.authenticate(promptInfo);
        });
    }

ใช้โซลูชันการเข้ารหัสลับที่ขึ้นอยู่กับการตรวจสอบสิทธิ์

หากต้องการปกป้องข้อมูลที่ละเอียดอ่อนภายในแอป คุณสามารถผสานรวม วิทยาการเข้ารหัสลงในเวิร์กโฟลว์การตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกโดยใช้อินสแตนซ์ CryptoObject เฟรมเวิร์กรองรับออบเจ็กต์การเข้ารหัสต่อไปนี้ Signature, Cipher และ Mac

หลังจากผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อความแจ้งข้อมูลไบโอเมตริกสำเร็จแล้ว แอปของคุณจะดำเนินการเข้ารหัสได้ ตัวอย่างเช่น หากคุณตรวจสอบสิทธิ์โดยใช้ออบเจ็กต์ Cipher แอปจะเข้ารหัสและถอดรหัสได้โดยใช้ออบเจ็กต์ SecretKey

ส่วนต่อไปนี้จะแสดงตัวอย่างการใช้ออบเจ็กต์ Cipher และออบเจ็กต์ SecretKey เพื่อเข้ารหัสข้อมูล ตัวอย่างแต่ละรายการใช้วิธีการต่อไปนี้

Kotlin

private fun generateSecretKey(keyGenParameterSpec: KeyGenParameterSpec) {
    val keyGenerator = KeyGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
    keyGenerator.init(keyGenParameterSpec)
    keyGenerator.generateKey()
}

private fun getSecretKey(): SecretKey {
    val keyStore = KeyStore.getInstance("AndroidKeyStore")

    // Before the keystore can be accessed, it must be loaded.
    keyStore.load(null)
    return keyStore.getKey(KEY_NAME, null) as SecretKey
}

private fun getCipher(): Cipher {
    return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
            + KeyProperties.BLOCK_MODE_CBC + "/"
            + KeyProperties.ENCRYPTION_PADDING_PKCS7)
}

Java

private void generateSecretKey(KeyGenParameterSpec keyGenParameterSpec) {
    KeyGenerator keyGenerator = KeyGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    keyGenerator.init(keyGenParameterSpec);
    keyGenerator.generateKey();
}

private SecretKey getSecretKey() {
    KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");

    // Before the keystore can be accessed, it must be loaded.
    keyStore.load(null);
    return ((SecretKey)keyStore.getKey(KEY_NAME, null));
}

private Cipher getCipher() {
    return Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
            + KeyProperties.BLOCK_MODE_CBC + "/"
            + KeyProperties.ENCRYPTION_PADDING_PKCS7);
}

ตรวจสอบสิทธิ์โดยใช้เฉพาะข้อมูลเข้าสู่ระบบทางไบโอเมตริก

หากแอปใช้คีย์ลับที่ต้องใช้ข้อมูลเข้าสู่ระบบไบโอเมตริกเพื่อปลดล็อก ผู้ใช้จะต้องตรวจสอบสิทธิ์ข้อมูลเข้าสู่ระบบไบโอเมตริกทุกครั้งก่อนที่แอปจะเข้าถึงคีย์

หากต้องการเข้ารหัสข้อมูลที่ละเอียดอ่อนหลังจากที่ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบไบโอเมตริกเท่านั้น ให้ทำตามขั้นตอนต่อไปนี้

  1. สร้างคีย์ที่ใช้สิ่งต่อไปนี้ KeyGenParameterSpec การกำหนดค่า:

    Kotlin

    generateSecretKey(KeyGenParameterSpec.Builder(
            KEY_NAME,
            KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
            .setUserAuthenticationRequired(true)
            // Invalidate the keys if the user has registered a new biometric
            // credential, such as a new fingerprint. Can call this method only
            // on Android 7.0 (API level 24) or higher. The variable
            // "invalidatedByBiometricEnrollment" is true by default.
            .setInvalidatedByBiometricEnrollment(true)
            .build())

    Java

    generateSecretKey(new KeyGenParameterSpec.Builder(
            KEY_NAME,
            KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
            .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
            .setUserAuthenticationRequired(true)
            // Invalidate the keys if the user has registered a new biometric
            // credential, such as a new fingerprint. Can call this method only
            // on Android 7.0 (API level 24) or higher. The variable
            // "invalidatedByBiometricEnrollment" is true by default.
            .setInvalidatedByBiometricEnrollment(true)
            .build());
  2. เริ่มเวิร์กโฟลว์การตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกที่ใช้การเข้ารหัส โดยทำดังนี้

    Kotlin

    biometricLoginButton.setOnClickListener {
        // Exceptions are unhandled within this snippet.
        val cipher = getCipher()
        val secretKey = getSecretKey()
        cipher.init(Cipher.ENCRYPT_MODE, secretKey)
        biometricPrompt.authenticate(promptInfo,
                BiometricPrompt.CryptoObject(cipher))
    }

    Java

    biometricLoginButton.setOnClickListener(view -> {
        // Exceptions are unhandled within this snippet.
        Cipher cipher = getCipher();
        SecretKey secretKey = getSecretKey();
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        biometricPrompt.authenticate(promptInfo,
                new BiometricPrompt.CryptoObject(cipher));
    });
  3. ภายในการเรียกกลับการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก ให้ใช้คีย์ลับเพื่อเข้ารหัสข้อมูลที่ละเอียดอ่อน ดังนี้

    Kotlin

    override fun onAuthenticationSucceeded(
            result: BiometricPrompt.AuthenticationResult) {
        val encryptedInfo: ByteArray = result.cryptoObject.cipher?.doFinal(
            // plaintext-string text is whatever data the developer would like
            // to encrypt. It happens to be plain-text in this example, but it
            // can be anything
                plaintext-string.toByteArray(Charset.defaultCharset())
        )
        Log.d("MY_APP_TAG", "Encrypted information: " +
                Arrays.toString(encryptedInfo))
    }

    Java

    @Override
    public void onAuthenticationSucceeded(
            @NonNull BiometricPrompt.AuthenticationResult result) {
        // NullPointerException is unhandled; use Objects.requireNonNull().
        byte[] encryptedInfo = result.getCryptoObject().getCipher().doFinal(
            // plaintext-string text is whatever data the developer would like
            // to encrypt. It happens to be plain-text in this example, but it
            // can be anything
                plaintext-string.getBytes(Charset.defaultCharset()));
        Log.d("MY_APP_TAG", "Encrypted information: " +
                Arrays.toString(encryptedInfo));
    }

ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบแบบไบโอเมตริกหรือหน้าจอล็อก

คุณสามารถใช้คีย์ลับที่อนุญาตให้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบไบโอเมตริกหรือข้อมูลเข้าสู่ระบบหน้าจอล็อก (PIN, รูปแบบ หรือรหัสผ่าน) เมื่อกําหนดค่าคีย์นี้ ให้ระบุระยะเวลาที่ใช้งานได้ ในระหว่างนี้ แอปของคุณจะดำเนินการทางวิทยาการเข้ารหัสได้หลายรายการโดยไม่ต้องให้ผู้ใช้ตรวจสอบสิทธิ์อีกครั้ง

ในการเข้ารหัสข้อมูลที่ละเอียดอ่อนหลังจากที่ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลไบโอเมตริกหรือ ข้อมูลเข้าสู่ระบบในหน้าจอล็อก ให้ทําตามขั้นตอนต่อไปนี้

  1. สร้างคีย์ที่ใช้สิ่งต่อไปนี้ KeyGenParameterSpec การกำหนดค่า:

    Kotlin

    generateSecretKey(KeyGenParameterSpec.Builder(
        KEY_NAME,
        KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
        .setUserAuthenticationRequired(true)
        .setUserAuthenticationParameters(VALIDITY_DURATION_SECONDS,
                ALLOWED_AUTHENTICATORS)
        .build())

    Java

    generateSecretKey(new KeyGenParameterSpec.Builder(
        KEY_NAME,
        KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
        .setUserAuthenticationRequired(true)
        .setUserAuthenticationParameters(VALIDITY_DURATION_SECONDS,
                ALLOWED_AUTHENTICATORS)
        .build());
  2. ภายในระยะเวลา VALIDITY_DURATION_SECONDS หลังจากที่ผู้ใช้ ตรวจสอบสิทธิ์และเข้ารหัสข้อมูลที่ละเอียดอ่อน

    Kotlin

    private fun encryptSecretInformation() {
        // Exceptions are unhandled for getCipher() and getSecretKey().
        val cipher = getCipher()
        val secretKey = getSecretKey()
        try {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey)
            val encryptedInfo: ByteArray = cipher.doFinal(
                // plaintext-string text is whatever data the developer would
                // like to encrypt. It happens to be plain-text in this example,
                // but it can be anything
                    plaintext-string.toByteArray(Charset.defaultCharset()))
            Log.d("MY_APP_TAG", "Encrypted information: " +
                    Arrays.toString(encryptedInfo))
        } catch (e: InvalidKeyException) {
            Log.e("MY_APP_TAG", "Key is invalid.")
        } catch (e: UserNotAuthenticatedException) {
            Log.d("MY_APP_TAG", "The key's validity timed out.")
            biometricPrompt.authenticate(promptInfo)
        }

    Java

    private void encryptSecretInformation() {
        // Exceptions are unhandled for getCipher() and getSecretKey().
        Cipher cipher = getCipher();
        SecretKey secretKey = getSecretKey();
        try {
            // NullPointerException is unhandled; use Objects.requireNonNull().
            ciper.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedInfo = cipher.doFinal(
                // plaintext-string text is whatever data the developer would
                // like to encrypt. It happens to be plain-text in this example,
                // but it can be anything
                    plaintext-string.getBytes(Charset.defaultCharset()));
        } catch (InvalidKeyException e) {
            Log.e("MY_APP_TAG", "Key is invalid.");
        } catch (UserNotAuthenticatedException e) {
            Log.d("MY_APP_TAG", "The key's validity timed out.");
            biometricPrompt.authenticate(promptInfo);
        }
    }

ตรวจสอบสิทธิ์โดยใช้คีย์การตรวจสอบสิทธิ์ต่อการใช้งาน

คุณสามารถให้การสนับสนุนคีย์การตรวจสอบสิทธิ์ต่อการใช้งานภายในอินสแตนซ์ของ BiometricPrompt คีย์ดังกล่าว กำหนดให้ผู้ใช้แสดงเอกสารรับรองไบโอเมตริกหรืออุปกรณ์ ข้อมูลเข้าสู่ระบบทุกครั้งที่แอปของคุณต้องการเข้าถึงข้อมูลที่ได้รับการปกป้องโดย คีย์นั้น คีย์การตรวจสอบสิทธิ์ต่อการใช้งานอาจมีประโยชน์สำหรับธุรกรรมที่มีมูลค่าสูง เช่น ชำระเงินจำนวนมากหรืออัปเดตบันทึกข้อมูลสุขภาพของบุคคล

หากต้องการเชื่อมโยงออบเจ็กต์ BiometricPrompt กับคีย์ auth-per-use ให้เพิ่มโค้ดที่คล้ายกับตัวอย่างต่อไปนี้

Kotlin

val authPerOpKeyGenParameterSpec =
        KeyGenParameterSpec.Builder("myKeystoreAlias", key-purpose)
    // Accept either a biometric credential or a device credential.
    // To accept only one type of credential, include only that type as the
    // second argument.
    .setUserAuthenticationParameters(0 /* duration */,
            KeyProperties.AUTH_BIOMETRIC_STRONG or
            KeyProperties.AUTH_DEVICE_CREDENTIAL)
    .build()

Java

KeyGenParameterSpec authPerOpKeyGenParameterSpec =
        new KeyGenParameterSpec.Builder("myKeystoreAlias", key-purpose)
    // Accept either a biometric credential or a device credential.
    // To accept only one type of credential, include only that type as the
    // second argument.
    .setUserAuthenticationParameters(0 /* duration */,
            KeyProperties.AUTH_BIOMETRIC_STRONG |
            KeyProperties.AUTH_DEVICE_CREDENTIAL)
    .build();

ตรวจสอบสิทธิ์โดยที่ผู้ใช้ไม่ต้องดำเนินการอย่างชัดแจ้ง

โดยค่าเริ่มต้น ระบบจะกำหนดให้ผู้ใช้ดำเนินการบางอย่าง เช่น การกดปุ่ม หลังจากที่ยอมรับข้อมูลเข้าสู่ระบบไบโอเมตริกแล้ว เราขอแนะนำให้ใช้การกำหนดค่านี้หากแอปแสดงกล่องโต้ตอบเพื่อยืนยันการดำเนินการที่มีความละเอียดอ่อนหรือมีความเสี่ยงสูง เช่น การซื้อ

หากแอปแสดงกล่องโต้ตอบการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกสำหรับการดำเนินการที่มีความเสี่ยงต่ำ แต่คุณสามารถให้คำแนะนำเกี่ยวกับระบบ ที่ผู้ใช้ไม่จำเป็นต้อง ยืนยันการตรวจสอบสิทธิ์ คำแนะนำนี้ช่วยให้ผู้ใช้ดูเนื้อหาในแอปของคุณได้ ได้รวดเร็วยิ่งขึ้นหลังจากตรวจสอบสิทธิ์ซ้ำโดยใช้วิธีการแบบแพสซีฟ เช่น Face- หรือ การจดจำแบบไอริส หากต้องการให้คำแนะนำนี้ ให้ส่ง false ไปยัง setConfirmationRequired()

รูปที่ 2 แสดงกล่องโต้ตอบเดียวกัน 2 เวอร์ชัน เวอร์ชันหนึ่งกำหนดให้ผู้ใช้ดำเนินการอย่างชัดแจ้ง แต่อีกเวอร์ชันหนึ่งไม่กำหนด

ภาพหน้าจอของกล่องโต้ตอบ จับภาพหน้าจอของกล่องโต้ตอบ
รูปที่ 2 การตรวจสอบสิทธิ์ด้วยใบหน้าแบบไม่ยืนยันผู้ใช้ (ด้านบน) และแบบยืนยันผู้ใช้ (ด้านล่าง)

ข้อมูลโค้ดต่อไปนี้แสดงวิธีแสดงกล่องโต้ตอบที่ไม่กำหนดให้ผู้ใช้ดำเนินการอย่างชัดแจ้งเพื่อดำเนินการตามกระบวนการตรวจสอบสิทธิ์ให้เสร็จสมบูรณ์

Kotlin

// Lets the user authenticate without performing an action, such as pressing a
// button, after their biometric credential is accepted.
promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .setConfirmationRequired(false)
        .build()

Java

// Lets the user authenticate without performing an action, such as pressing a
// button, after their biometric credential is accepted.
promptInfo = new BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .setConfirmationRequired(false)
        .build();

อนุญาตให้ใช้ข้อมูลเข้าสู่ระบบที่ไม่ใช่ข้อมูลไบโอเมตริกสำรอง

หากต้องการให้แอปอนุญาตการตรวจสอบสิทธิ์โดยใช้ข้อมูลไบโอเมตริกหรือข้อมูลเข้าสู่ระบบของอุปกรณ์ คุณสามารถประกาศว่าแอปรองรับข้อมูลเข้าสู่ระบบของอุปกรณ์โดยใส่ DEVICE_CREDENTIAL ไว้ในชุดค่าที่คุณส่งไปยัง setAllowedAuthenticators()

หากแอปของคุณใช้อยู่ createConfirmDeviceCredentialIntent() หรือ setDeviceCredentialAllowed() หากต้องการมอบความสามารถนี้ ให้เปลี่ยนไปใช้ setAllowedAuthenticators()

แหล่งข้อมูลเพิ่มเติม

ดูข้อมูลเพิ่มเติมเกี่ยวกับการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกใน Android ได้ที่บทความต่อไปนี้ ที่ไม่ซับซ้อน

บล็อกโพสต์