我的 Spring Boot 應用程式包含一個登錄驗證系統,該系統強制用戶注冊其憑據并通過在其帳戶中發送帶有“立即激活”按鈕的確認表單來驗證其電子郵件。驗證完成后,用戶可以在我的應用程式中輸入他們經過驗證的憑據。
后者是在 https:localhost:8443 上的 HTTPS 上實作的,同時啟用了 SSL 和 Spring Security。
我的問題如下:
我想公開執行器指標,但由于我啟用了 HTTPS,所以匿名 URL 將是 https:localhost:8443/actuator。我想從 http:localhost:8080/actuator 訪問該 URL,因此我創建了第二個 Tomcat 連接器,它以某種方式解決了我的問題,因為我收到了 POSTMAN 的回復。
但是...一旦我創建了第二個 Tomcat 連接器,我就必須從頭開始重新進行驗證程序,這意味著第二個 Tomcat 連接器創建了我的應用程式的第二個實體,但在不同的埠上運行. 我提供一個圖表只是為了幫助您解決我的問題。
我想訪問 http:localhost:8080/actuator,同時維護從我的應用程式開始就已經在 https:localhost:8443 上進行的驗證程序。那可能嗎???

安全配置類
package com.andrekreou.iot.authentication.security;
import com.andrekreou.iot.authentication.user.ApplicationUserService;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class ApplicationSecurityConfig {
private final ApplicationUserService applicationUserService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf().disable()
.requiresChannel()
.antMatchers("/greeting","/actuator/prometheus")
.requiresInsecure()
.and()
.authorizeRequests()
.antMatchers("/api/v*/registration/**","/register*","/login","/registration","/registration-complete","/greeting").permitAll()
//.antMatchers("/show-news-contents").hasRole(ADMIN.name())
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("email")
.permitAll()
.defaultSuccessUrl("/",true)
.failureUrl("/login-error")
.and()
.logout()
.logoutUrl("/logout")
.clearAuthentication(true)
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID","Idea-2e8e7cee")
.logoutSuccessUrl("/login");
return http.build();
}
@Bean
public AuthenticationManager authenticationManager(
AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider =
new DaoAuthenticationProvider();
provider.setPasswordEncoder(bCryptPasswordEncoder);
provider.setUserDetailsService(applicationUserService);
return provider;
}
}
主班
package com.andrekreou.iot;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EnableJpaRepositories
public class IotApplication {
public static void main(String[] args) {
SpringApplication.run(IotApplication.class, args);
}
@Bean
public ServletWebServerFactory servletContainer(@Value("${server.http.port}") int httpPort) {
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setPort(httpPort);
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
}
應用程式屬性
#Server properties for HTTPS configuration
server.ssl.enabled=true
server.ssl.key-store-type=PKCS12
server.ssl.key-store=classpath:local-ssl.p12
server.ssl.key-store-password=<password>
server.ssl.key-password=<password>
server.servlet.context-path=/
server.ssl.key-alias=local_ssl
server.port=8443
server.http.port=8080
uj5u.com熱心網友回復:
正如 M. Deinum 所建議的,最好避免這樣的實作。您必須通過添加這兩行來配置您的 application.properties 檔案。
management.server.ssl.enabled=false
management.server.port=8080
然后洗掉它server.http.port=8080以及ServletWebServerFactory在 Main 類中宣告的 bean。最后,在 Security Config 類的 antMatchers 行中添加以下路徑,就在authorizeRequests().
這樣,您將同時擁有/actuator/prometheus8443 和 8080 埠。
希望這可以幫助!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513237.html
標籤:爪哇春天弹簧靴ssl雄猫
上一篇:條紋-雷達規則
