วิธีหนึ่งในการปกป้องข้อมูลที่ละเอียดอ่อนหรือเนื้อหาพรีเมียมภายในแอปคือการขอการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริก เช่น การใช้การจดจำใบหน้าหรือการจดจำลายนิ้วมือ คู่มือนี้อธิบายวิธีรองรับโฟลว์การเข้าสู่ระบบด้วยข้อมูลไบโอเมตริกในแอป
โดยทั่วไป คุณควรใช้ Credential Manager สำหรับการลงชื่อเข้าใช้ครั้งแรกใน อุปกรณ์ ส่วนการให้สิทธิ์อีกครั้งในภายหลังสามารถทำได้ด้วย BiometricPrompt หรือ Credential Manager ข้อดีของการใช้ BiometricPrompt คือมีตัวเลือกการปรับแต่งมากกว่า ในขณะที่ Credential Manager มีการใช้งานเพียงครั้งเดียวในทั้ง 2 โฟลว์
ประกาศประเภทการตรวจสอบสิทธิ์ที่แอปของคุณรองรับ
หากต้องการกำหนดประเภทการตรวจสอบสิทธิ์ที่แอปของคุณรองรับ ให้ใช้
BiometricManager.Authenticators
อินเทอร์เฟซ ระบบจะให้คุณประกาศการตรวจสอบสิทธิ์ประเภทต่อไปนี้
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 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()
แสดงข้อความแจ้งให้เข้าสู่ระบบ
หากต้องการแสดงข้อความแจ้งของระบบที่ขอให้ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ ข้อมูลเข้าสู่ระบบไบโอเมตริก ให้ใช้ ไลบรารี Biometric กล่องโต้ตอบที่ระบบจัดเตรียมให้นี้มีความสอดคล้องกันในแอปที่ใช้กล่องโต้ตอบนี้ ซึ่งจะสร้างประสบการณ์การใช้งานที่น่าเชื่อถือมากขึ้น กล่องโต้ตอบตัวอย่างจะปรากฏในรูปที่ 1
หากต้องการเพิ่มการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกลงในแอปโดยใช้ไลบรารี Biometric ให้ทำตามขั้นตอนต่อไปนี้
ในไฟล์
build.gradleของโมดูลแอป เพิ่มทรัพยากร Dependency ในandroidx.biometricไลบรารีในกิจกรรมหรือ Fragment ที่โฮสต์กล่องโต้ตอบการเข้าสู่ระบบด้วยข้อมูลไบโอเมตริก ให้แสดงกล่องโต้ตอบโดยใช้ตรรกะที่แสดงในข้อมูลโค้ดต่อไปนี้
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
หลังจากที่ผู้ใช้ตรวจสอบสิทธิ์สำเร็จโดยใช้ BiometricPrompt แล้ว แอปของคุณจะดำเนินการเข้ารหัสลับได้ ตัวอย่างเช่น หากคุณตรวจสอบสิทธิ์โดยใช้ออบเจ็กต์
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);
}
ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบไบโอเมตริกเท่านั้น
หากแอปของคุณใช้คีย์ลับที่ต้องใช้ข้อมูลเข้าสู่ระบบไบโอเมตริกในการปลดล็อก ผู้ใช้ต้องตรวจสอบสิทธิ์ข้อมูลเข้าสู่ระบบไบโอเมตริก ทุกครั้ง ก่อนที่แอปของคุณจะเข้าถึงคีย์
หากต้องการเข้ารหัสข้อมูลที่ละเอียดอ่อนหลังจากที่ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบไบโอเมตริกแล้วเท่านั้น ให้ทำตามขั้นตอนต่อไปนี้
สร้างคีย์ที่ใช้การกำหนดค่าต่อไปนี้
KeyGenParameterSpecKotlin
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, รูปแบบ หรือรหัสผ่าน) เมื่อกำหนดค่าคีย์นี้ ให้ระบุระยะเวลาที่คีย์มีผล ในช่วงระยะเวลานี้ แอปของคุณจะดำเนินการเข้ารหัสลับได้หลายครั้งโดยที่ผู้ใช้ไม่ต้องตรวจสอบสิทธิ์อีกครั้ง
หากต้องการเข้ารหัสข้อมูลที่ละเอียดอ่อนหลังจากที่ผู้ใช้ตรวจสอบสิทธิ์โดยใช้ข้อมูลเข้าสู่ระบบไบโอเมตริกหรือข้อมูลเข้าสู่ระบบหน้าจอล็อกแล้ว ให้ทำตามขั้นตอนต่อไปนี้
สร้างคีย์ที่ใช้การกำหนดค่าต่อไปนี้
KeyGenParameterSpecKotlin
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()
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมเกี่ยวกับการตรวจสอบสิทธิ์ด้วยข้อมูลไบโอเมตริกใน Android ได้ที่แหล่งข้อมูลต่อไปนี้