我正在使用 Spring Boot 开发一组服务来提供应用程序框架和 REST 功能。我已经成功设置了一组简单的服务(仅使用 HTTP)并且它们运行良好。
现在,我被要求让它们使用 HTTPS。我对“软件安全方法”知之甚少,对 HTTPS 或其操作或使用涉及的内容一无所知。到目前为止,我的研究表明需要定义某种安全证书,并且它们在 HTTP 通信的“安全”中发挥作用。
我看过许多示例和教程,这些在一定程度上有助于理解拼图的某些部分,但整个图片仍然非常模糊,并且缺少许多部分。在大多数情况下,这是一个令人困惑的类和 API 调用的海洋,几乎没有解释为什么要做这些事情。
我正在寻找有关设置使用 HTTPS 的“正确”方法的入门建议或详细教程。如果它能解释机器的各个“部件”、它们扮演什么角色以及何时需要使用这些部件(以及为什么),那将会非常有帮助。
更新
我会尝试更详细地介绍一下。
根据我发现讨论在 Spring Boot 应用程序中“启用”HTTPS 以及涉及的一些配置的教程,我将以下内容放在一起,尝试为我的应用程序设置和配置 HTTPS。
application.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
好的,找到了解决“localhost”问题的方法:
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
设置了一个 key ,并将其放置在src/main/resources/keystore
中。
另一件事值得一提的是,进行 POST 调用的服务正在使用不同的端口:
https://localhost:8090/rst/missionPlanning/generateRoute
被调用的服务被配置为使用端口 8090。这与使用端口 8443 的 HTTPS 有何关系?
最佳答案
您好,请查看这个解决方案,它可以帮助您使用 Spring Boot 实现 HTTPS https://www.tutorialspoint.com/spring_boot/spring_boot_enabling_https
关于Java/Spring启动: How do I incorporate HTTPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57381791/