안드로이드에서 제공하는 생체 인증을 하는 방식에 대해 정리 한다.

 

안드로이드 버전

한국어명

영어명

버전

릴리즈

API 레벨

안드로이드 6 마시멜로우

Marshmallow

6.0-6.0.1

2015

23

안드로이드 7 누가

Nougat

7.0-7.1

2016-08-22

24-25

안드로이드 8 오레오

Oreo

8.0-8.1

2017-08-21

26-27

안드로이드 9 파이

Pie

9.0

2018-08-06

28

안드로이드 10

Android 10

10.0

2019-09-03

29

안드로이드 11

Android 11

11.0

TBD

-

생체 인식 기능은 Marshmallow부터 지원 가능.

 

Marshmallow ~ Oreo : Fingerprint

Pie~ Android 11 : BiometricPrompt

 

1) Fingerprint를 이용한 생체 인증

 

API Level 23~ API Level 27 지문인식을 제공해 주는 API

 

Android 6.0 API(API Level 23) 부터 지문 인증과 관련하여 지문 스캔을 사용하여 사용자를 인증하는 새로운 API를 제공

 

참고

https://developer.android.com/about/versions/marshmallow/android-6.0

 

Android 6.0 API  |  Android 개발자  |  Android Developers

Get to know the new developer features in Android 6.0 Marshmallow.

developer.android.com

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dreamsecurity.bioauth.sample">
    
    <uses-permission android:name="android.permission.USE_FINGERPRINT" />

 

 

현재 Android studio에서는 권한을 추가하면 USE_FINGERPRINT is deprecated 경고가 발생

API Level 29 부터는 BiometricPrompt가 권장되기 때문이다.

 

 

생체 인증 가능 여부확인

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {   // Marshmallow부터 지원 가능 체크
     fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);

     if (fingerprintManager.isHardwareDetected() == false) { //Manifest에 Fingerprint 퍼미션을 추가해야 사용이 가능함.
            Toast.makeText(this, "지문인식을 사용할수 없는 기기입니다.", Toast.LENGTH_LONG).show();
     } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "지문 사용을 여부를 허용해 주세요.", Toast.LENGTH_LONG).show();
     } else if (fingerprintManager.hasEnrolledFingerprints() == false) {
            Toast.makeText(this, "등록된 지문정보가 없습니다.", Toast.LENGTH_LONG).show();
     } else {    //  생체 인증 사용가능
            Toast.makeText(this, "지문인식을 해주세요.", Toast.LENGTH_LONG).show();
     }
}

 

 

생체 인증 등록

private CancellationSignal cancellationSignal = null;
private Context context;

public FingerprintHandler(Context context) {
      this.context = context;
}

public void startAuth(FingerprintManager fingerprintManager, FingerprintManager.CryptoObject cryptoObject) {
      cancellationSignal = new CancellationSignal();
      fingerprintManager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
}

 

 

생체 인식 Callback

   @Override
   public void onAuthenticationError(int errorCode, CharSequence errString) {
        this.update("인증 에러 발생" + errString, false);
   }

   @Override
    public void onAuthenticationFailed() {
        this.update("인증 실패", false);
   }

   @Override
   public void onAuthenticationHelp(int errorCode, CharSequence error) {
        this.update("Error: " + error, false);
   }

    @Override
    public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
        this.update("앱 접근이 허용", true);
    }

    public void stopFingerAuth() {
        if (cancellationSignal != null && !cancellationSignal.isCanceled()) {
            cancellationSignal.cancel();
        }
    }

    private void update(String result, boolean isResult) {
        if (isResult == false) {
            Toast.makeText(MainActivity.this, "지문 인식 실패", Toast.LENGTH_LONG).show();
         } else {//지문인증 성공
            Toast.makeText(MainActivity.this, "지문 인식 성공. \nresult : "+result, Toast.LENGTH_LONG).show();
         }
    }

 

 

2) BiometricPrompt를 이용한 생체 인증

 

참고 URL : https://developer.android.com/training/sign-in/biometric-auth

API Level 29 부터는 BiometricPrompt 제공

 

build.gradle dependencies 추가

dependencies {
    ...
    implementation 'androidx.biometric:biometric:1.1.0'
}

 

permission 추가

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
           package="com.dreamsecurity.bioauth.sample">

   <uses-permission android:name="android.permission.USE_BIOMETRIC"/>

 

생체 인증 가능 여부확인

	switch (manager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK)) {
		case BiometricManager.BIOMETRIC_SUCCESS: {   //  생체 인증 가능
            Log.d("MainActivity", "Application can authenticate with biometrics.");
          	break;
		}
		case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE: { //  기기에서 생체 인증을 지원하지 않는 경우
			Log.d("MainActivity", "Biometric facility is not available in this device.");
			break;
		}
		case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE: {  
			Log.d("MainActivity", "Biometric facility is currently not available");
			break;
		}
		case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED: {   //  생체 인식 정보가 등록되지 않은 경우
			Log.d("MainActivity", "Any biometric credential is not added in this device.");
			break;
		}
		default: {   //   기타 실패
			Log.d("MainActivity", "Fail Biometric facility");
			break;
		}
	}

 

생체 인증 등록

얼굴 인식의 경우는 Android 11 (API level 30)부터 지원

BiometricPrompt.PromptInfo.Builder promptBuilder = new BiometricPrompt.PromptInfo.Builder();

promptBuilder.setTitle("Biometric login for my app");
promptBuilder.setSubtitle("Log in using your biometric credential");
promptBuilder.setNegativeButtonText("Use account password");

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){ //  안면인식 ap사용 android 11부터 지원
    promptBuilder.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG | BiometricManager.Authenticators.DEVICE_CREDENTIAL);
}

promptInfo =  promptBuilder.build();

 

생체 인증 실행

biometricLoginButton.setOnClickListener(view -> {
    biometricPrompt.authenticate(promptInfo);
});

 

생체 인식 Callback

	biometricPrompt = new BiometricPrompt(MainActivity.this,
                executor, new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode,
                                              CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(getApplicationContext(),

                        "ErrorCode: "+errorCode+
                                "\nAuthentication error: " + errString, Toast.LENGTH_SHORT)
                        .show();
            }

            @Override
            public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                Toast.makeText(getApplicationContext(), "result : Authentication succeeded", Toast.LENGTH_SHORT).show();
            }
            
            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
                Toast.makeText(getApplicationContext(), "Authentication failed",
                        Toast.LENGTH_SHORT)
                        .show();
            }
        });

 

추후 생체 인증 방법에 대해서는 조금 더 공부를 해보아야 겠다.

+ Recent posts