gpt4 book ai didi

Javafx SSL 客户端配置以使用我的 Spring boot ssl 安全 Rest Api

转载 作者:搜寻专家 更新时间:2023-11-01 02:57:37 24 4
gpt4 key购买 nike

我使用基本的 auth header 开发了一个安全的 Spring boot Rest Api,现在我正尝试添加 SSL 以防止中间人攻击,正如 spring 教程所建议的那样 I activated the SSL security on my spring by generating a keystore.JKS that I Added to the ressources. server works fine .

现在我有一个 JavaFX 客户端,我在其中使用 Unirest Api 来执行我的请求,但我不知道如何更新我的客户端以成功地向我的服务器执行获取/发布请求。

我尝试了很多教程,但没有一个起作用我不想禁用 ssl

这是我的 spring boot 配置:

 # The format used for the keystore 
server.ssl.key-store-type=JKS
# The path to the keystore containing the certificate
server.ssl.key-store=classpath:keystore.jks
# The password used to generate the certificate
server.ssl.key-store-password=mypass
# The alias mapped to the certificate
server.ssl.key-alias=tomcat

当我在 chrome 中接受证书时,我的服务器启动良好,使用 postman 很好。

对于我的 java 客户端,我正在使用这个片段:

public class MainApp extends Application {

private static Stage parent;
private static Parent root;

static
{
System.setProperty("javax.net.ssl.trustStore","c:/ssl/keystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "mypass");
System.setProperty("javax.net.ssl.keyStore", "c:/ssl/keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "mypass");
}

@Override
public void start(Stage stage) throws Exception {


Unirest.setHttpClient(HttpClients.createSystem());

root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
parent = stage;
Scene scene = new Scene(root);
scene.getStylesheets().add("/styles/Styles.css");
stage.initStyle(StageStyle.TRANSPARENT);
scene.setFill(Color.TRANSPARENT);

stage.setTitle("JavaFX and Maven");
// stage.setOpacity(0.2);
stage.setScene(scene);
stage.show();

}

如您所见,我正在尝试在应用程序启动时配置我的 Unirest httpClient

  HttpResponse<String> asJson = Unirest.get("http://localhost:8181/logins/login/?us_username=" + username.getText() + "&us_pwdusr=" +  Crypto.getSha(password.getText()))
.header("authorization", "Basic " + value)
.header("cache-control", "no-cache").asString();

但是当我在我的服务器上执行 get 方法时(我在服务器中激活了从 8181 和 8080 到 8443 的重定向)我收到以下错误

Caused by: com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLPeerUnverifiedException: Certificate for doesn't match any of the subject alternative names: [] at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143) at com.mashape.unirest.request.BaseRequest.asString(BaseRequest.java:56) at easypos.easyposclientfx.FXMLController.login(FXMLController.java:130) ... 59 more

Caused by: javax.net.ssl.SSLPeerUnverifiedException: Certificate for doesn't match any of the subject alternative names: [] at org.apache.http.conn.ssl.SSLConnectionSocketFactory.verifyHostname(SSLConnectionSocketFactory.java:467) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:397) at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138) ... 61 more

尝试阅读这篇文章我明白我的证书别名或名称可能是我所有的证书都没有在系统中注册

为了响应这个想法,我确实在java jre环境中将证书添加到cacerts中,但仍然出现相同的错误

我应该怎么做才能解决这个问题?

这是建议的证书

my certaficate that I downloaded using chrome

最佳答案

您遇到的异常建议如下:

您的客户端系统的 java keystore (客户端应用程序正在使用的 JRE)正在向服务提供商请求证书,当它收到证书时,无法将服务器的主题备用名称与其匹配。

示例:您正在调用您的网络服务 http://localhost:8181/logins/login

但是安装在您的 REST 生产者服务上的证书有一个主题名称,如“abc.com”,这会在本地主机和 abc.com 比较之间造成不匹配,因此会引发错误。

为了在本地测试服务和客户端交互,您可以使用以下任何技术:

  • 创建一个主题名为 localhost 的新证书并使用它客户端 keystore 和其余生产者中的证书
  • 在本地安装 Apache HTTP 服务器,启用 mod_ssl 模块以启用https。完成后通过在主机文件中创建主机条目来欺骗您的 DNS您的主机名和 IP 地址为 127.0.0.1

能否分享下图中自签名证书的截图:

Stack Overflow Certificate

它将帮助社区探索更多选项来更好地帮助您。

关于Javafx SSL 客户端配置以使用我的 Spring boot ssl 安全 Rest Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50025655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com