首页 > 解决方案 > Java/Spring Boot:如何合并 HTTPS?

问题描述

我正在使用 Spring Boot 开发一系列服务来提供应用程序框架和 REST 功能。我已经成功设置了一组简单的服务(仅使用 HTTP)并且它们运行良好。

现在,我被要求让他们使用 HTTPS。我对“软件安全方法”知之甚少,对 HTTPS 或其操作或使用所涉及的内容一无所知。到目前为止,我的研究表明需要定义某种安全证书,并且它们在 HTTP 通信的“安全”中发挥作用。

我看过很多例子和教程,这些在一定程度上有助于理解拼图的一些部分,但整个画面仍然非常模糊,很多部分都丢失了。在大多数情况下,这是一个令人困惑的类和 API 调用的海洋,几乎没有解释正在做什么的“为什么”。

我正在寻找有关设置使用 HTTPS 的“正确”方法的入门建议或详细教程。如果它解释了机器的各个“部件”,它们扮演什么角色以及何时需要使用这些部件(以及为什么),将会非常有帮助。

更新

我会尝试更详细一点。

根据我发现讨论在 Spring Boot 应用程序中“启用”HTTPS 以及所涉及的一些配置的教程,我整理了以下内容,试图为我的应用程序设置和配置 HTTPS。

应用程序.yml:

...
server:
  port: 8443
  ssl:
    key-store-type: PKCS12
    key-store: classpath:keystore/keystore.p12
    key-store-password: <password>
    key-alias: tomcat

顺便说一句:明文密码不会被删除。我们如何解决这个问题?

安全配置.java:

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(AuthenticationManagerBuilder auth) throws Exception {
    // I have no idea what I should be specifying here, nor what any of the available methods
    // actually DO.
    auth.inMemoryAuthentication();
  }

  @Override
  public void configure(HttpSecurity http) throws Exception {
    // I have no idea what I should be specifying here, nor what any of the available methods
    // actually DO.
    http.httpBasic().and().authorizeRequests().antMatchers("/rst").permitAll().anyRequest().authenticated();
  }

}

当我尝试运行应用程序时,很明显它正在尝试使用安全机制,但由于我没有正确完成某些事情而失败。

这是我从一个尝试向另一个服务发起 POST 调用的服务中得到的异常:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://localhost:8090/rst/missionPlanning/generateRoute": java.security.cert.CertificateException: No name matching localhost found; nested exception is javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found

好的,找到了解决“本地主机”问题的方法:

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
  @Override
  public boolean verify(String hostname, SSLSession sslSession) {
    if (hostname.equals("localhost")) {
      return true;
    }

    return false;
  }
});

现在我得到一个不同的例外:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://localhost:8090/rst/missionPlanning/generateRoute": sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

在所有这些之前,我确实设置了一个密钥keytool,并将其放在src/main/resources/keystore.

另一件值得一提的是,发出 POST 调用的服务正在使用不同的端口:

https://localhost:8090/rst/missionPlanning/generateRoute

被调用的服务配置为使用端口 8090。这与使用端口 8443 的 HTTPS 有什么关系?

标签: javaspring-boothttps

解决方案


嗨,你能看看这个解决方案,它可以帮助你使用 Spring Boot https://www.tutorialspoint.com/spring_boot/spring_boot_enabling_https


推荐阅读