I m试图利用服务器的证书文档建立链接。 我能够用浏览器手工获取证书档案,并用钥匙工具将其输入钥匙仓库。 然后,我可以使用java代码进入钥匙仓库,获得在钥匙仓库中添加的证书,并与服务器连接。
然而,现在我甚至想执行获得证书的程序,并用java法将其添加到我的关键仓库,而不使用钥匙托ol或浏览器取得证书。
请允许我告诉我,如何处理这一问题,我需要做些什么?
I m试图利用服务器的证书文档建立链接。 我能够用浏览器手工获取证书档案,并用钥匙工具将其输入钥匙仓库。 然后,我可以使用java代码进入钥匙仓库,获得在钥匙仓库中添加的证书,并与服务器连接。
然而,现在我甚至想执行获得证书的程序,并用java法将其添加到我的关键仓库,而不使用钥匙托ol或浏览器取得证书。
请允许我告诉我,如何处理这一问题,我需要做些什么?
Edit: 。
Using the following code it is possible to add a trust store during runtime.
import java.io.InputStream;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
public class SSLClasspathTrustStoreLoader {
public static void setTrustStore(String trustStore, String password) throws Exception {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream keystoreStream = SSLClasspathTrustStoreLoader.class.getResourceAsStream(trustStore);
keystore.load(keystoreStream, password.toCharArray());
trustManagerFactory.init(keystore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustManagers, null);
SSLContext.setDefault(sc);
}
}
我利用该守则建立了与积极名录服务器的安全LDAP联系。
我写了小图书馆ssl-utils-android 。
仅凭资产目录提供档案名称即可装载任何证书。
使用:
OkHttpClient client = new OkHttpClient();
SSLContext sslContext = SslUtils.getSslContextForCertificateFile(context, "BPClass2RootCA-sha2.cer");
client.setSslSocketFactory(sslContext.getSocketFactory());
正义
https://docs.oracle.com/cd/E19509-01/820-3503/ggfgo/index.html https://www.sslshopper.com/article-most-common-java-keytool-keystore-commands.html
javac -cp .:/home/ec2-user/velu/*: QuickStart.java
java -cp .:/home/ec2-user/velu/*: QuickStart
[ec2-user@ip-10-30-0-66 velu]$ ls
QuickStart.class commons-codec-1.2.jar input-payload.txt logback-core-1.1.3.jar
QuickStart.java commons-httpclient-3.1.jar httpclient-4.5.jar jdk-8u101-linux-x64.rpm slf4j-api-1.7.12.jar
certificates commons-logging-1.2.jar httpcore-4.4.1.jar logback-classic-1.1.3.jar
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class QuickStart {
public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.keyStore", "/home/user/velu/certificates/myownOut.pkcs12");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
System.setProperty("javax.net.ssl.trustStore", "/home/user/velu/certificates/myTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientParams params = new HttpClientParams();
params.setConnectionManagerClass(MultiThreadedHttpConnectionManager.class);
HttpClient client = new HttpClient(params);
HttpMethod m = new PostMethod("https://velu.org:443/Services/com/Echo");
m.setRequestHeader("content-type", "application/xml");
//m.setRequestHeader("Accept", "application/xml");
// m.setRequestHeader("SOAPAction", "Echo");
try {
((PostMethod) m).setRequestEntity(new StringRequestEntity(getFileContent(), "application/xml", "UTF-8"));
System.out.println("VELU EXCUTING");
client.executeMethod(m);
if (m.getStatusCode() == 200) {
System.out.println("VELU RECEIVED:" + m.getResponseBodyAsString());
}
} catch (IOException e) {
System.out.println(e.toString());
} finally {
m.releaseConnection();
}
}
public static String getFileContent() {
BufferedReader br = null;
String fileContent = "";
try {
br = new BufferedReader(new FileReader(
"/home/user/velu/input-payload.txt")); // Note that this file format should be proper.
String sCurrentLine = "";
while ((sCurrentLine = br.readLine()) != null) {
fileContent += sCurrentLine;
}
System.out.println(fileContent);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return fileContent;
}
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...