在我的应用程序中,我需要从批处理应用程序 (jar) 进行 https 调用。
为了成功调用服务,我肯定需要安装与指向的端点相关的证书。
我们如何安装 SSL 证书并使用相同的证书以便从独立的 jar 应用程序(不涉及服务器)安全地调用 HTTPS 服务?
最佳答案
一种导入方法是在您的 spring 配置文件中为下面的类创建 bean
<bean id="certLoad" class="com.kp.LoadHttpsCertificate">
<property key="password" value="password"/>
<property key="keyfile" value="absolute file path"/>
</bean>
加载HttpsCertificate.java
public class LoadHttpsCertificate {
private String password;
private String keyfile;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getKeyfile() {
return keyfile;
}
public void setKeyfile(String keyfile) {
this.keyfile = keyfile;
}
@PostConstruct
public void loadFile(){
//System.out.println("setting property javax.net.ssl.trustStore = " + keyfile);
System.setProperty("javax.net.ssl.keyStore", keyfile);
System.setProperty("javax.net.ssl.keyStorePassword",password);
//System.setProperty("javax.net.ssl.keyStoreType", "jks");
//System.setProperty("org.jboss.security.ignoreHttpsHost", "true");
Properties pi = System.getProperties();
pi.put("file.encoding", "UTF-8"); // To add a new one
System.setProperties(pi);
}
}
虽然我更愿意通过导入到 java 的安全目录来加载证书(请参阅 here 了解更多详细信息)
关于java - 如何处理 SSL 证书以在 Jar (Spring Batch) 应用程序中调用 https 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28819313/