[issue] ERROR_CLEARTEXT_NOT_PERMITTED

지프치프·2022년 3월 11일
0

Issue(error)

목록 보기
10/20

개요

Rest 통신이나 WebView를 구현하다보면
간혹 위 에러를 만날 수 있는데
말그대로 cleartext를 허용하지 않아서 생기는 문제이다.
여기서 cleartext란 TLS 암호화가 되어있지 않은 일반 http를 일컫는다

원인

Android P(API level 28)부터 보안 정책 강화로 인해
https가 아닌 http로 request를 보낼 경우 빌드 중에 예외를 일으킨다.

물론 Android P 미만 (Android O 이하)에서는
http로 request를 보내도 정상적으로 작동한다.

해결

해결 방법에는 3가지가 있다.

URL을 HTTPS로 변경

가장 쉬우고 근본적인 방법이지만
HTTPS를 지원하지 않는 페이지도 있다.

그럴 경우엔 아래 2가지 방법을 권장한다.

모든 HTTP URL 허용

AndroidManifest에서 아래의 attribute 하나만 넣어주면 된다.

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
             ~~~
        android:usesCleartextTraffic="true">
  		<activity android:name=".activity.MainActivity">
          <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
  		</activity>
</application>

일부 HTTP URL 허용

모든 URL에 허용하기 보단
특정 몇 몇 URL에만 허용하고 싶을 때 사용할 방법도 있다.
먼저 res 디렉토리 하위에 xml 디렉토리를 만들어준다
그리고 나서 xml 파일을 하나 생성한다.
필자는 network_security_config.xml로 만들었다.

그리고 아래와 같이 작성한다.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted = "true">
        <domain includeSubdomains="true">www.naver.com</domain>
    </domain-config>
</network-security-config>

그리고나서 AndroidManifest로 이동하여
아래 attribute를 넣어주면 된다.
android:networkSecurityConfig="@xml/network_security_config"

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
             ~~~
        android:networkSecurityConfig="@xml/network_security_config">
  		<activity android:name=".activity.MainActivity">
          <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
  		</activity>
</application>

ERR_ACCESS_DENIED


Rest 통신 같은 경우는 위 방법을 적용시키면 바로 작동을 하지만
WebView의 경욱 간혹 위 에러코드와 함께 거부되는 현상이 있다.

너무 걱정하지말고 앱을 지웠다가 다시 깔면 정상적으로 작동한다.

개인적으로 공부했던 것을 바탕으로 작성하다보니
잘못된 정보가 있을수도 있습니다.
인지하게 되면 추후 수정하겠습니다.
피드백은 언제나 환영합니다.
읽어주셔서 감사합니다.

profile
지프처럼 거침없는 개발을 하고싶은 개발자

0개의 댓글