質問編集履歴
1
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
java tls1.3のRES
|
1
|
+
java tls1.3のRESTクライアントの実装
|
test
CHANGED
@@ -8,4 +8,198 @@
|
|
8
8
|
|
9
9
|
あるサーバでTLS1.3のREST APIを作成されています。
|
10
10
|
|
11
|
-
そのAPI
|
11
|
+
そのAPIをリクエストするクライアントをどのように作成したらよいでしょうか?
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
#ソース
|
16
|
+
|
17
|
+
String response = null;
|
18
|
+
|
19
|
+
InputStream inputStream = null;
|
20
|
+
|
21
|
+
OutputStream outputStream = null;
|
22
|
+
|
23
|
+
HttpURLConnection conn = null;
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
try {
|
28
|
+
|
29
|
+
URL url = new URL(urlStr);
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
// iaik.protocol.https.HttpsURLConnectionの場合は、ポート番号を指定する必要がある
|
34
|
+
|
35
|
+
if (url.getPort() == -1) {
|
36
|
+
|
37
|
+
String newUrlStr = StringUtils.replace(urlStr, url.getHost(), url.getHost() + ":19443");
|
38
|
+
|
39
|
+
url = new URL(newUrlStr);
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
conn = new HttpsURLConnection(url);
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
// SSLClientContextの設定
|
48
|
+
|
49
|
+
SSLClientContext sslContext = (SSLClientContext) ((HttpsURLConnection) conn).getSSLContext();
|
50
|
+
|
51
|
+
sslContext.setChainVerifier(new NonCheckChainVerifier()); // OpenTrustはチェック不要
|
52
|
+
|
53
|
+
sslContext.setSessionManager(new NonCacheSessionManager());
|
54
|
+
|
55
|
+
sslContext.setAllowLegacyRenegotiation(false);
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
※↓SSLClientContext.VERSION_TLS13の定数がない
|
60
|
+
|
61
|
+
sslContext.setAllowedProtocolVersions(SSLClientContext.VERSION_TLS10, SSLClientContext.VERSION_TLS12);
|
62
|
+
|
63
|
+
※↑SSLClientContext.VERSION_TLS13の定数がない
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
sslContext.setEnabledCipherSuiteList(SSLClientContextUtils.getDefaultCipherSuiteList());
|
68
|
+
|
69
|
+
// SNI設定
|
70
|
+
|
71
|
+
ServerNameList serverNameList = new ServerNameList(new ServerName[] {
|
72
|
+
|
73
|
+
new ServerName(APIサーバ)
|
74
|
+
|
75
|
+
});
|
76
|
+
|
77
|
+
serverNameList.setCritical(false);
|
78
|
+
|
79
|
+
ExtensionList extensions = new ExtensionList();
|
80
|
+
|
81
|
+
extensions.addExtension(serverNameList);
|
82
|
+
|
83
|
+
extensions.addExtension(new SupportedEllipticCurves());
|
84
|
+
|
85
|
+
extensions.addExtension(new SupportedPointFormats());
|
86
|
+
|
87
|
+
sslContext.setExtensions(extensions);
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// タイムアウト設定:iSaSiLkはシステムプロパティ経由でしか設定できない
|
92
|
+
|
93
|
+
//String connTimeoutStr = String.valueOf(AppConfig.getConfig().getInt(CONNECT_TIMEOUT_KEY));
|
94
|
+
|
95
|
+
//String timeoutStr = String.valueOf(AppConfig.getConfig().getInt(READ_TIMEOUT_KEY));
|
96
|
+
|
97
|
+
//System.setProperty(HttpManager.CONN_TIMEOUT_P, connTimeoutStr);
|
98
|
+
|
99
|
+
//System.setProperty(HttpManager.TIMEOUT_P, timeoutStr);
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// タイムアウト設定
|
104
|
+
|
105
|
+
conn.setConnectTimeout(AppConfig.getConfig().getInt(CONNECT_TIMEOUT_KEY));
|
106
|
+
|
107
|
+
conn.setReadTimeout(AppConfig.getConfig().getInt(READ_TIMEOUT_KEY));
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// クライアント証明書の設定
|
112
|
+
|
113
|
+
CertificateFactory fac = CertificateFactory.getInstance("X509");
|
114
|
+
|
115
|
+
FileInputStream is = new FileInputStream(cerファイルのパス);
|
116
|
+
|
117
|
+
X509Certificate cert[] = new X509Certificate[1];
|
118
|
+
|
119
|
+
cert[0] = ((X509Certificate) fac.generateCertificate(is));
|
120
|
+
|
121
|
+
is.close();
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
InputStream keyInput = FileUtils.openInputStream(new File(p12ファイルのパス));
|
126
|
+
|
127
|
+
PKCS12 p12 = new PKCS12(keyInput);
|
128
|
+
|
129
|
+
p12.decrypt(パスワード);
|
130
|
+
|
131
|
+
KeyBag[] keybags = p12.getKeyBags();
|
132
|
+
|
133
|
+
PrivateKey key = (PrivateKey) keybags[0].getPrivateKey();
|
134
|
+
|
135
|
+
keyInput.close();
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
sslContext.clearPSKCredentials();
|
140
|
+
|
141
|
+
sslContext.clearClientCredentials();
|
142
|
+
|
143
|
+
sslContext.addClientCredentials(new KeyAndCert(cert, key));
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
// ヘッダ指定
|
148
|
+
|
149
|
+
conn.setRequestMethod("POST");
|
150
|
+
|
151
|
+
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
|
152
|
+
|
153
|
+
conn.setRequestProperty("Content-Length", "98");
|
154
|
+
|
155
|
+
conn.setDoOutput(true);
|
156
|
+
|
157
|
+
conn.setDoInput(true);
|
158
|
+
|
159
|
+
outputStream = conn.getOutputStream();
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
// リクエストボディー書き込み
|
164
|
+
|
165
|
+
IOUtils.write(input, outputStream, AppConfig.getAppEncoding());
|
166
|
+
|
167
|
+
outputStream.flush();
|
168
|
+
|
169
|
+
IOUtils.closeQuietly(outputStream);
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
// レスポンスを文字列に変換する
|
174
|
+
|
175
|
+
inputStream = conn.getInputStream();
|
176
|
+
|
177
|
+
response = IOUtils.toString(inputStream, AppConfig.getAppEncoding());
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
catch (Throwable e) {
|
182
|
+
|
183
|
+
// 失敗したのでnullを返す
|
184
|
+
|
185
|
+
return null;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
finally {
|
190
|
+
|
191
|
+
IOUtils.closeQuietly(outputStream);
|
192
|
+
|
193
|
+
IOUtils.closeQuietly(inputStream);
|
194
|
+
|
195
|
+
if (conn != null) {
|
196
|
+
|
197
|
+
conn.disconnect();
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
return response;
|