วิธีหนึ่งในการปกป้องข้อมูลที่ละเอียดอ่อนหรือเนื้อหาพรีเมียมภายในแอปคือการขอการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก เช่น การใช้การจดจำใบหน้าหรือการจดจำลายนิ้วมือ คู่มือนี้อธิบายวิธีรองรับขั้นตอนการเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกในแอป
โดยทั่วไป คุณควรใช้ Credential Manager สำหรับการลงชื่อเข้าใช้ครั้งแรกในอุปกรณ์ การให้สิทธิ์อีกครั้งในภายหลังสามารถทำได้โดยใช้ข้อความแจ้งข้อมูลไบโอเมตริกหรือ Credential Manager ข้อดีของการใช้ข้อความแจ้งข้อมูลไบโอเมตริกคือมีตัวเลือกการปรับแต่งมากกว่า ส่วน Credential Manager มีการใช้งานเดียวในทั้ง 2 ขั้นตอน
ประกาศประเภทการตรวจสอบสิทธิ์ที่แอปของคุณรองรับ
หากต้องการกำหนดประเภทการตรวจสอบสิทธิ์ที่แอปรองรับ ให้ใช้อินเทอร์เฟซ BiometricManager.Authenticators ระบบช่วยให้คุณประกาศการตรวจสอบสิทธิ์ประเภทต่อไปนี้ได้
BIOMETRIC_STRONG- การตรวจสอบสิทธิ์โดยใช้ข้อมูลไบโอเมตริกคลาส 3 ตามที่ระบุไว้ในหน้าคำจำกัดความความเข้ากันได้ของ Android
BIOMETRIC_WEAK- การตรวจสอบสิทธิ์โดยใช้ข้อมูลไบโอเมตริกคลาส 2 ตามที่ระบุไว้ในหน้าคำจำกัดความความเข้ากันได้ของ Android
DEVICE_CREDENTIAL- การตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบการล็อกหน้าจอ ซึ่งได้แก่ PIN, รูปแบบ หรือรหัสผ่านของผู้ใช้
หากต้องการเริ่มใช้โปรแกรมตรวจสอบสิทธิ์ ผู้ใช้ต้องสร้าง PIN, รูปแบบ หรือรหัสผ่าน หากผู้ใช้ยังไม่มี 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();
ระบบไม่รองรับการใช้ประเภท Authenticator ต่อไปนี้ร่วมกันใน Android 10 (ระดับ API 29) และต่ำกว่า: DEVICE_CREDENTIAL และ BIOMETRIC_STRONG | DEVICE_CREDENTIAL หากต้องการตรวจสอบว่ามี PIN, รูปแบบ หรือรหัสผ่านใน Android 10 และต่ำกว่าหรือไม่ ให้ใช้เมธอด KeyguardManager.isDeviceSecure()
ตรวจสอบว่าการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกพร้อมใช้งาน
หลังจากตัดสินใจเลือกองค์ประกอบการตรวจสอบสิทธิ์ที่แอปรองรับแล้ว ให้ตรวจสอบว่าองค์ประกอบเหล่านี้พร้อมใช้งานหรือไม่ โดยส่งชุดค่าผสมประเภทระดับบิตเดียวกันกับที่คุณประกาศโดยใช้เมธอด setAllowedAuthenticators() ไปยังเมธอด canAuthenticate()
หากจำเป็น ให้เรียกใช้การดำเนินการตาม Intent ACTION_BIOMETRIC_ENROLL ใน Intent Extra ให้ระบุชุดโปรแกรมตรวจสอบสิทธิ์ที่แอปของคุณยอมรับ 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
หากต้องการเพิ่มการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกลงในแอปโดยใช้ไลบรารี Biometric ให้ทำตามขั้นตอนต่อไปนี้
ในไฟล์
build.gradleของโมดูลแอป ให้เพิ่มทรัพยากร Dependency ในไลบรารีandroidx.biometricในกิจกรรมหรือส่วนย่อยที่โฮสต์กล่องโต้ตอบการเข้าสู่ระบบด้วยข้อมูลไบโอเมตริก ให้แสดงกล่องโต้ตอบโดยใช้ตรรกะที่แสดงในข้อมูลโค้ดต่อไปนี้
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 และ
KeyAgreement
หลังจากผู้ใช้ตรวจสอบสิทธิ์สำเร็จโดยใช้ข้อความแจ้งข้อมูลไบโอเมตริก แอปจะดำเนินการเข้ารหัสได้ ตัวอย่างเช่น หากคุณตรวจสอบสิทธิ์โดยใช้ออบเจ็กต์ 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);
}
ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกเท่านั้น
หากแอปใช้คีย์ลับที่ต้องใช้ข้อมูลเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกเพื่อปลดล็อก ผู้ใช้ต้องตรวจสอบสิทธิ์ข้อมูลเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกทุกครั้งก่อนที่แอปจะเข้าถึงคีย์
หากต้องการเข้ารหัสข้อมูลที่ละเอียดอ่อนหลังจากที่ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกเท่านั้น ให้ทำตามขั้นตอนต่อไปนี้
สร้างคีย์ที่ใช้การกำหนดค่า
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());เริ่มเวิร์กโฟลว์การตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกที่รวมการเข้ารหัสไว้ด้วย โดยทำดังนี้
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)); });ภายใน Callback การตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก ให้ใช้คีย์ลับเพื่อเข้ารหัสข้อมูลที่ละเอียดอ่อน ดังนี้
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, รูปแบบ หรือรหัสผ่าน) เมื่อกำหนดค่าคีย์นี้ ให้ระบุระยะเวลาที่คีย์มีผล ในช่วงระยะเวลานี้ แอปของคุณจะดำเนินการเข้ารหัสหลายรายการได้โดยที่ผู้ใช้ไม่ต้องตรวจสอบสิทธิ์อีกครั้ง
หากต้องการเข้ารหัสข้อมูลที่ละเอียดอ่อนหลังจากที่ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกหรือหน้าจอล็อก ให้ทำตามขั้นตอนต่อไปนี้
สร้างคีย์ที่ใช้การกำหนดค่า
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());ภายในระยะเวลา
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 กับคีย์การตรวจสอบสิทธิ์ต่อการใช้งาน ให้เพิ่มโค้ดที่คล้ายกับโค้ดต่อไปนี้
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();
ตรวจสอบสิทธิ์โดยไม่ต้องมีการดำเนินการจากผู้ใช้อย่างชัดเจน
โดยค่าเริ่มต้น ระบบจะกำหนดให้ผู้ใช้ดำเนินการบางอย่าง เช่น กดปุ่ม หลังจากที่ยอมรับข้อมูลเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกแล้ว การกำหนดค่านี้เหมาะอย่างยิ่งหากแอปของคุณแสดงกล่องโต้ตอบเพื่อยืนยันการดำเนินการที่มีความละเอียดอ่อนหรือมีความเสี่ยงสูง เช่น การซื้อ
อย่างไรก็ตาม หากแอปแสดงกล่องโต้ตอบการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกสำหรับการดำเนินการที่มีความเสี่ยงต่ำ คุณสามารถระบุคำแนะนำแก่ระบบว่าผู้ใช้ไม่จำเป็นต้องยืนยันการตรวจสอบสิทธิ์ คำแนะนำนี้ช่วยให้ผู้ใช้ดูเนื้อหาในแอปได้เร็วขึ้นหลังจากตรวจสอบสิทธิ์อีกครั้งโดยใช้รูปแบบพาสซีฟ เช่น การจดจำใบหน้าหรือม่านตา หากต้องการระบุคำแนะนำนี้ ให้ส่ง false ไปยังเมธอด setConfirmationRequired()
รูปที่ 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()
createConfirmDeviceCredentialIntent() เลิกใช้งานแล้วในระดับ API 29 และ setDeviceCredentialAllowed() เลิกใช้งานแล้วใน Biometric 1.1.0
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกใน Android ได้ที่แหล่งข้อมูลต่อไปนี้