0
我想通過https連接到我的後端。 我有2個certificats * .p12和* .cer。我用進口的命令(見代碼) 我進口與命令鍵:如何在grails中使用* .p12證書來建立https連接?
keytool -v -importkeystore -srckeystore bonus-testagent.p12 -srcstoretype PKCS12 -destkeystore truststore.jks -deststoretype JKS
我的代碼是(Grails的服務):
private SSLSocketFactory getSSLFactory() throws Exception {
KeyStore keyStore = KeyStore.getInstance("JKS");
String keystoreFile = 'D:\\grails_wide\\certificates\\truststore.jks'
File binaryFile = new File(keystoreFile);
InputStream is = binaryFile.newInputStream()
if (is == null) {
return null;
}
String password = "****";
keyStore.load(is, password.toCharArray());
is.close();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
return ctx.getSocketFactory();
}
def accountInfo(String cardNumber, Cookie[] cookies) {
String urlStr = "https://app-domain.com";
URL url = new URL(urlStr);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setSSLSocketFactory(getSSLFactory());
conn.setRequestMethod("GET");
conn.setRequestProperty("Cookie", WebUtils.buildCookiesHeader(cookies))
InputStream is = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer stringBuffer = new StringBuffer();
while ((line = rd.readLine()) != null) {
stringBuffer.append(line);
}
rd.close();
def response = stringBuffer.toString();
但它的主要錯誤:
ERROR errors.GrailsExceptionResolver - SunCertPathBuilderException occurred when processing request: [POST] /club/account
unable to find valid certification path to requested target. Stacktrace follows:
Message: unable to find valid certification path to requested target
在字符串:
InputStream is = conn.getInputStream();
在''Grails'中使用'is'作爲變量名可能不是一個好主意...... – Bruno