[英]Java/Spring Boot: How do I incorporate HTTPS?
我正在使用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
順便說一句 :使用明文密碼不會減少密碼。 我們如何才能做到這一點 ?
SecurityConfiguration.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如何配合?
您好,能否請您尋找此解決方案,它可以通過Spring Boot https://www.tutorialspoint.com/spring_boot/spring_boot_enabling_https幫助您使用HTTPS
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.