我有调用第三方安全端点的代码。这段代码在带有java主类的linux环境中运行良好。请查找下面的代码,了解我如何将证书设置到java代码中。
import java.io.IOException;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
public class HttpPostRequest {
public static void demo() throws Exception {
String consumer_key = "a";
String consumer_secret = "b";
String access_token = "c";
String access_secret= "d";
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumer_key, consumer_secret);
consumer.setTokenWithSecret(access_token, access_secret);
String uri = "url";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri);
System.out.println(httpPost.getURI().getHost());
String json = "{ \"start\": { \"from\": 1615245718000, \"to\": 1620516118000 }}";
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
System.out.println(httpPost.getHeaders("Authorization"));
System.out.println(httpPost.getHeaders("Accept"));
try {
consumer.sign(httpPost);
}catch (Exception e) {
e.printStackTrace();
}
System.out.println(httpPost.getHeaders("Authorization"));
System.out.println(httpPost.getHeaders("Accept"));
HttpResponse response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine().toString());
HttpEntity entity2 = response.getEntity();
System.out.println();
System.out.println(EntityUtils.toString(entity2));
}
public static void main(String[] args) throws Exception {
try {
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.keyStore", "C:\\project\\myystore.jks");
System.setProperty("javax.net.debug", "ssl # very verbose debug");
System.setProperty("javax.net.ssl.keyStorePassword", "password1");
System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\Java\\jdk1.8.0\\jre\\lib\\security\\cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "password2");
demo();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
我在windows和linux环境中尝试了spring boot应用程序,但得到了相同的错误。
我刚刚从linux服务器复制了.jks文件,并给出了windows环境中的路径。
我这里有两个问题。
为什么相同的代码在windows环境中不起作用?
如何将其转换为spring启动应用程序?
错误:
javax.net.ssl.sslpeerunverifiedexception:未在org.apache.http.conn.ssl.abstractverifier.verify(abstractverifier.java:128)org.apache.http.conn.ssl.sslsocketfactory.connectsocket(sslsocketfactory.java:339)上的sun.security.ssl.ssl.sslsessionimpl.getpeercertificates(ssl.java:450)上验证对等方
暂无答案!
目前还没有任何答案,快来回答吧!