java 启动 jvm 加载_在运行jvm之后,在运行时加载Java信任库吗?

在我的Java应用程序中,我需要将POST请求发送到位于https后面的服务器.在运行Java应用程序的计算机上,在/usr/local/comp.jks中有一个Java信任存储,其中包含我需要与之交互的服务器(已经导入)的证书.

问题是我无法控制将如何运行将运行Java应用程序的JVM-例如通过增加:

-Djavax.net.ssl.trustStore = /usr/local/comp.jks到VM参数.

JVM启动后,是否可以在运行时从我的应用程序在上述路径中将信任库加载到上述路径中,以便我可以对https站点进行身份验证?

我只找到了有关如何在运行时导入证书的指南,但是我无法使用-也是因为我没有/usr/local/comp.jks的密码

下面是我目前的实现(常规):

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

import java.security.KeyManagementException;

import java.security.KeyStore

import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException

import java.security.cert.X509Certificate

import java.util.Base64;

import javax.net.ssl.HostnameVerifier;

import javax.net.ssl.HttpsURLConnection;

import javax.net.ssl.SSLContext;

import javax.net.ssl.SSLSession;

import javax.net.ssl.TrustManager;

import javax.net.ssl.X509TrustManager;

public class HttpsClientImpl extends AbstractHttpClient {

private String username = null;

private String password = null;

public HttpsClientImpl (String username, String password) {

this.username=username;

this.password=password;

}

@Override

public String sendRequest(String request, String method) {

System.setProperty( "javax.net.ssl.trustStore", "/usr/local/comp.jks" );

URL url = new URL(request);

HttpsURLConnection con = (HttpsURLConnection) url.openConnection()

// Set auth

byte[] name = (username + ":" + password).getBytes();

String authStr = Base64.getEncoder().encodeToString(name);

con.setRequestProperty("Authorization", "Basic " + authStr)

con.setRequestMethod(method);

writeResult(con);

return con.getResponseCode();

}

private void writeResult(HttpsURLConnection con) throws IOException {

if(con!=null){

BufferedReader br = null;

if (200 <= con.getResponseCode() && con.getResponseCode() <= 299) {

br = new BufferedReader(new InputStreamReader(con.getInputStream()));

} else {

br = new BufferedReader(new InputStreamReader(con.getErrorStream()));

}

try {

String input;

while ((input = br.readLine()) != null){

System.out.println(input);

}

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

当我运行时,我得到:

sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)

at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)

at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)

at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)

Caused: sun.security.validator.ValidatorException: PKIX path building failed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值