[英]Trying to set up proper HTTPS with Spring Boot 4
我有一個Spring Boot應用程序,在沒有HTTPS的情況下運行得很好。 現在,我獲得了我的SSL證書以便在prod環境中使用,我現在想要默認使所有端點都是HTTPS。
我一直在使用Spring Security配置我的頁面訪問權限,這就是我所擁有的:
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/images/**",
"/css/**",
"/js/**",
....bunch of endpoints....
"/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.failureUrl("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.clearAuthentication(true)
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID", "remember-me")
.logoutSuccessUrl("/")
.permitAll()
.and()
.exceptionHandling()
.accessDeniedPage("/error");
我讀到你可以將它添加到我上面的配置中以強制所有HTTPS請求,但我想確定它應該去哪里,所以我不打破生產:
.requiresChannel().anyRequest().requiresSecure();
我正在通過AWS Elastic Beanstalk運行我的應用程序,並且已成功在AWS上正確安裝了SSL證書(准備就緒)。 只是為了澄清一下,SSL / HTTPS在Load Balance處終止,而不是在EC2實例處終止,所以這可能會改變Spring Boot中的配置,我猜?
此外,如果我可以在我的機器上本地測試使用Spring的https,那將是很棒的,但我不知道如何繼續。 很多在線示例看起來都很復雜。
每個人對此有何建議? 謝謝
是的,為了強制執行HTTPS,您需要添加以下代碼行
http.requiresChannel().anyRequest().requiresSecure();
要在本地進行測試,您需要在本地和所需的以下配置中安裝cert
在Spring Boot中啟用HTTPS:
在您的application.properties中定義以下屬性:
# Define a custom port instead of the default 8080
server.port = 8089
# Tell Spring Security to require requests over HTTPS
security.require-ssl=true
# The keystore containing the certificate keys
server.ssl.key-store=keystore.jks
# The password used to generate the keys
server.ssl.key-store-password=password
# The alias mapped to the certificate
server.ssl.keyAlias=tomcat
啟用HSTS
HTTP嚴格傳輸安全(HSTS)是一種Web安全策略機制,可幫助保護網站免受協議降級攻擊和cookie劫持。 它允許Web服務器聲明Web瀏覽器(或其他符合要求的用戶代理)應該只使用安全的HTTPS連接[1]與之交互,而不是通過不安全的HTTP協議。
Strict-Transport-Security: max-age=31536000; includeSubDomains
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.