我使用以下代码发送OTP
,但我收到错误:
PKIX路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException:无法 在responseMessage =找到所请求目标的有效证书路径 connection.getResponseMessage();
我找到了几个解决方案但不确定答案的实施情况。
public Boolean sendSmsOTP(String MobileNumber, String OTPKey) {
logDebugInfo("In sendSmsOTP ", LOG_TYPE_INFORMATIVE);
HttpURLConnection connection;
int responseCode;
String requestData, responseMessage;
URL smsUrl;
URI uri;
String mobileNumber = MobileNumber.replace("+91", "");
try {
logDebugInfo("Mobile Number : " + mobileNumber + " OTP : " + OTPKey, LOG_TYPE_INFORMATIVE);
requestData = gResourceBundle.getString("RequestSMSData");
requestData = requestData.replace("[MobileNumber]", mobileNumber.trim());
requestData = requestData.replace("[OTPKEY]", OTPKey);
requestData = requestData.replace("[ ]", "%20");
requestData = requestData.replace(" ", "%20");
System.out.println(requestData.toString());
uri = new URI(requestData);
smsUrl = uri.toURL();
logDebugInfo("URL : " + smsUrl.toString(), LOG_TYPE_INFORMATIVE);
connection = (HttpURLConnection) smsUrl.openConnection();
connection.setDoOutput(false);
connection.setDoInput(true);
System.out.println("Manish negi -> "+connection.toString());
responseMessage = connection.getResponseMessage();
logDebugInfo("Response Message from SMS server " + responseMessage, LOG_TYPE_INFORMATIVE);
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
connection.disconnect();
System.out.println("OTP GENERATED");
return true;
} else {
connection.disconnect();
return false;
}
} catch (Exception e) {
logErrorInfo("Exception in sendSmsOTP function()..." + e.getMessage());
logDebugInfo("Exception in sendSmsOTP function()..." + e.getMessage(), LOG_TYPE_CRITICAL);
gResultMessage = gResultMessage.replace("Error Code", "CA01");
gReturnResponse = getJSONString(gErrorResponse, gResultMessage);
e.printStackTrace();
return false;
}
}
答案 0 :(得分:1)
根据我的经验,此错误消息通常意味着您尝试与具有自签名证书的服务器建立TLS连接。如果是这种情况,解决方案通常是将服务器的证书添加到运行客户端的JVM的证书存储中。
如果证书位于名为server.crt
的文件中,则可以使用JVM附带的keytool
添加证书,如下所示:
keytool -import -noprompt \
-storepass changeit \
-alias some_alias \
-keystore $JAVA_HOME/jre/lib/security/cacerts \
-file server.crt
您可以使用以下命令obtain the server's certificate using openssl
:
openssl s_client -showcerts -connect www.example.com:443 </dev/null
答案 1 :(得分:1)
首先,您需要将服务器的证书安装或添加到客户端JVM密钥库。您可以按照 anothernode 的说明通过keytool安装它,也可以使用基于GUI的工具keyStore Explorer。
在客户端商店获得服务器证书后,应该解决它。您是否可以使用keyStore Explorer工具检查证书是否已添加到客户端密钥库?