UnknownHostException + SSLHandshakeException 에러로 고생하는 분이 있다면 조금이나마 도움 되시길
이전 글에서 떴던
UnknownHostException 에러의 해결법 :
도메인을 IP로 바꾸지 못 하는 문제 때문에 서버의 /etc/hosts 파일에 아래와 같이 추가해줌
# host파일은 이게 주석
123.12.123.123 api.test.co.kr
(운영 서버 같은 경우 super 권한으로 변경해야 하기 때문에 PuTTy에서 따로 설정해준다.)
아싸뵷~!!! 다시 실행 해보니
SSLHandshakeException 에러 무한반복 : 씨앙~!!!!!!!!!!!!!!!!!!!!!
(javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake)
개발 서버에서도 만 하루 지나면 자꾸 뜨고 있어서 해결 방도가 필요했다.
서버 재부팅 하면 해결되긴 했지만 매 번 그럴수는 없는 노릇이니..
선배님에게 여쭤보니 다음 방법을 알려주심
1. OpenSSL로 서버 인증서를 만듦 (인터넷에서 다운)
2. keytool로 만든 인증서를 신뢰하는 인증서로 등록 - JAVA 설치시 설치 됨
3. 아래의 명령어를 shell에 입력 후 .jks 파일 만듦 ( ssl을 이용해서 Keystore만 변환해준거고, 실제 사용하는 건 jks임 )
# openSSL을 활용한 인증서 다운로드
openssl s_client -connect v4.cartory.net:443 < NUL | openssl x509 -out {설치파일 경로}
# keytool을 활용한 keystore 변환
keytool -importcert -trustcacerts -file d:\ssl\server.crt -alias youralias -keystore d:\ssl\carzenkeystore.jks
1.OpenSSL
: 공개적으로 사용 가능한 툴 SSL/TLS (서버)인증서 생성 및 관리하고, 다양한 환경에서 동작
.crt 확장자 파일 만듦
2.Keytool
: 자바에서 제공되는 툴 / Keystore 파일 관리
키스토어는 개인키, 인증서, 공개키 같은 보안 정보를 저장하는 파일
.jks 확장자 파일 만듦
* 아래는 참고
어려워 보이지만 실질적으로 내가 입력한 것은
1. ssl 명령어 한 줄
2. keytool 명령어 한 줄 + 비번 + 신뢰 합니까? Y
이것 밖에 없음
Win64 OpenSSL Command Prompt
OpenSSL 3.4.0 22 Oct 2024 (Library: OpenSSL 3.4.0 22 Oct 2024)
built on: Tue Oct 22 23:27:41 2024 UTC
platform: VC-WIN64A
options: bn(64,64)
compiler: cl /Z7 /Fdossl_static.pdb /Gs0 /GF /Gy /MD /W3 /wd4090 /nologo /O2 -DL_ENDIAN -DOPENSSL_PIC -D"OPENSSL_BUILDING_OPENSSL" -D"OPENSSL_SYS_WIN32" -D"WIN32_LEAN_AND_MEAN" -D"UNICODE" -D"_UNICODE" -D"_CRT_SECURE_NO_DEPRECATE" -D"_WINSOCK_DEPRECATED_NO_WARNINGS" -D"NDEBUG" -D_WINSOCK_DEPRECATED_NO_WARNINGS -D_WIN32_WINNT=0x0502
OPENSSLDIR: "C:\Program Files\Common Files\SSL"
ENGINESDIR: "C:\Program Files\OpenSSL\lib\engines-3"
MODULESDIR: "C:\Program Files\OpenSSL\lib\ossl-modules"
Seeding source: os-specific
CPUINFO: OPENSSL_ia32cap=0xfedaf387ffebffff:0x9c67ab
C:\Users\테스트>openssl s_client -connect api.test.co.kr:443 < NUL | openssl x509 -out d:\ssl\server.crt
Connecting to 175.45.207.159
depth=3 C=GB, ST=Greater Manchester, L=Salford, O=Comodo CA Limited, CN=AAA Certificate Services
verify error:num=19:self-signed certificate in certificate chain
verify return:1
depth=3 C=GB, ST=Greater Manchester, L=Salford, O=Comodo CA Limited, CN=AAA Certificate Services
verify return:1
depth=2 C=US, ST=New Jersey, L=Jersey City, O=The USERTRUST Network, CN=USERTrust RSA Certification Authority
verify return:1
depth=1 C=GB, ST=Greater Manchester, L=Salford, O=Sectigo Limited, CN=Sectigo RSA Domain Validation Secure Server CA
verify return:1
depth=0 CN=api.withauth.co.kr
verify return:1
DONE
C:\Users\테스트>openssl s_client -connect t-test.co.kr:443 < NUL | openssl x509 -out d:\ssl\t-server.crt
Connecting to 49.50.175.115
depth=2 C=US, ST=New Jersey, L=Jersey City, O=The USERTRUST Network, CN=USERTrust RSA Certification Authority
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=1 C=GB, ST=Greater Manchester, L=Salford, O=Sectigo Limited, CN=Sectigo RSA Domain Validation Secure Server CA
verify return:1
depth=0 CN=t-api.withauth.co.kr
verify return:1
DONE
C:\Users\테스트>keytool -importcert -trustcacerts -file d:\ssl\t_server.crt -alias youralias -keystore d:\ssl\t-withAuthkeystore.jks
키 저장소 비밀번호 입력:
새 비밀번호 다시 입력:
소유자: CN=t-api.withauth.co.kr
발행자: CN=Sectigo RSA Domain Validation Secure Server CA, O=Sectigo Limited, L=Salford, ST=Greater Manchester, C=GB
일련 번호: d1a54272a403420819c9ab79ac910744
적합한 시작 날짜: Thu Sep 19 09:00:00 KST 2024 종료 날짜: Tue Oct 21 08:59:59 KST 2025
인증서 지문:
MD5: 5E:D4:6C:E3:8D:3B:FE:C7:FA:75:E0:56:8D:10:45:FF
SHA1: 51:FB:58:09:5E:D5:25:0E:16:81:39:29:F5:8C:7F:F4:46:B3:8A:EC
SHA256: 9F:79:07:63:3A:E0:37:23:C6:30:22:D9:71:C9:FC:E2:D4:87:9D:D2:70:62:16:D4:5D:96:1E:B1:7F:3C:4F:33
서명 알고리즘 이름: SHA256withRSA
주체 공용 키 알고리즘: 2048비트 RSA 키
버전: 3
확장:
#1: ObjectId: 1.3.6.1.4.1.11129.2.4.2 Criticality=false
0000: 04 82 01 6A 01 68 00 77 00 DD DC CA 34 95 D7 E1 ...j.h.w....4...
0010: 16 05 E7 95 32 FA C7 9F F8 3D 1C 50 DF DB 00 3A ....2....=.P...:
0020: 14 12 76 0A 2C AC BB C8 2A 00 00 01 92 08 FC 77 ..v.,...*......w
0030: 7B 00 00 04 03 00 48 30 46 02 21 00 D3 71 8D B0 ......H0F.!..q..
0040: 61 37 71 2B 41 3A 7D A4 D5 8C DE 13 B6 38 61 82 a7q+A:.......8a.
0050: 16 35 68 E3 B9 20 4B FE 5A C7 C4 B6 02 21 00 B5 .5h.. K.Z....!..
0060: 0B DA E4 D5 53 D4 56 5F EF F2 A5 0F D1 5C FA B7 ....S.V_.....\..
0070: 38 6E 0B 0A 39 3A 8C B2 9A FF AD 3C 68 C7 B9 00 8n..9:.....<h...
0080: 75 00 CC FB 0F 6A 85 71 09 65 FE 95 9B 53 CE E9 u....j.q.e...S..
0090: B2 7C 22 E9 85 5C 0D 97 8D B6 A9 7E 54 C0 FE 4C .."..\......T..L
00A0: 0D B0 00 00 01 92 08 FC 77 39 00 00 04 03 00 46 ........w9.....F
00B0: 30 44 02 20 50 A9 02 00 38 51 28 A9 E0 45 80 12 0D. P...8Q(..E..
00C0: A5 5F 6D 88 B4 95 F7 F3 1C 34 5C ED 8A E4 D9 62 ._m......4\....b
00D0: 29 8D 3C 2A 02 20 5A A5 E6 87 7A 7E E6 35 FB F4 ).<*. Z...z..5..
00E0: 78 2A 54 46 C8 AE 27 35 17 84 54 D5 0A 0B 8B 99 x*TF..'5..T.....
00F0: FA 30 52 BD FA F7 00 76 00 12 F1 4E 34 BD 53 72 .0R....v...N4.Sr
0100: 4C 84 06 19 C3 8F 3F 7A 13 F8 E7 B5 62 87 88 9C L.....?z....b...
0110: 6D 30 05 84 EB E5 86 26 3A 00 00 01 92 08 FC 77 m0.....&:......w
0120: 07 00 00 04 03 00 47 30 45 02 21 00 90 A3 0D A3 ......G0E.!.....
0130: C9 FD C4 62 9E A4 6B AE A2 F1 53 9F E7 72 5D 06 ...b..k...S..r].
0140: 03 47 08 CD 17 04 8B 9B 2C 44 ED CB 02 20 48 36 .G......,D... H6
0150: 1D 8E E4 81 DD BD FE 8E FF 83 38 26 10 81 2C 5F ..........8&..,_
0160: FB DF 82 37 FD 28 6C 22 4B 37 3B F0 0C DD ...7.(l"K7;...
#2: ObjectId: 1.3.6.1.5.5.7.1.1 Criticality=false
AuthorityInfoAccess [
[
accessMethod: caIssuers
accessLocation: URIName: http://crt.sectigo.com/SectigoRSADomainValidationSecureServerCA.crt
,
accessMethod: ocsp
accessLocation: URIName: http://ocsp.sectigo.com
]
]
#3: ObjectId: 2.5.29.35 Criticality=false
AuthorityKeyIdentifier [
KeyIdentifier [
0000: 8D 8C 5E C4 54 AD 8A E1 77 E9 9B F9 9B 05 E1 B8 ..^.T...w.......
0010: 01 8D 61 E1 ..a.
]
]
#4: ObjectId: 2.5.29.19 Criticality=true
BasicConstraints:[
CA:false
PathLen: undefined
]
#5: ObjectId: 2.5.29.32 Criticality=false
CertificatePolicies [
[CertificatePolicyId: [1.3.6.1.4.1.6449.1.2.2.7]
[PolicyQualifierInfo: [
qualifierID: 1.3.6.1.5.5.7.2.1
qualifier: 0000: 16 17 68 74 74 70 73 3A 2F 2F 73 65 63 74 69 67 ..https://sectig
0010: 6F 2E 63 6F 6D 2F 43 50 53 o.com/CPS
]] ]
[CertificatePolicyId: [2.23.140.1.2.1]
[] ]
]
#6: ObjectId: 2.5.29.37 Criticality=false
ExtendedKeyUsages [
serverAuth
clientAuth
]
#7: ObjectId: 2.5.29.15 Criticality=true
KeyUsage [
DigitalSignature
Key_Encipherment
]
#8: ObjectId: 2.5.29.17 Criticality=false
SubjectAlternativeName [
DNSName: t-api.withauth.co.kr
DNSName: www.t-api.withauth.co.kr
]
#9: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 70 18 29 65 3D E8 15 EA 4D E6 3C E2 1D 9E EB 09 p.)e=...M.<.....
0010: F3 9A D1 FC ....
]
]
이 인증서를 신뢰합니까? [아니오]: Y
인증서가 키 저장소에 추가되었습니다.
그리고 자바 파일에 아래 설정 추가 후 운영서버에 jks를 넣어 줌~~
// TLS 버전 설정
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
System.setProperty("javax.net.ssl.trustStore", "/경로/파일명.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "password___area");
// connection 하기 전에 버전 설정을 해줘야 한다.
HttpsURLConnection connection = (HttpsURLConnection) withauth_url.openConnection();
그리고 다시 반영 후 확인 했을 때 토큰 값도 정상적으로 들어 오는 거 확인했고, 간편인증도 정상적으로 열림 그리고 오늘부로 드디어 해방이댜~~!!!!!!!!!!!!!!!!!!!!!!!!!~!!!! 야호오~~~~
'개발일지 > 네트워크' 카테고리의 다른 글
네트워크/서버 + UnknownHostException 에러 처리 등등..(1/2) (0) | 2024.11.21 |
---|---|
curl 통신라이브러리 (0) | 2024.10.29 |