4.3. Spring Boot
除了标准的 JAAS 身份验证外,Spring Boot 上的 HawtIO 也可以通过 Spring Security 或 Keycloak 进行保护。如果要为 Spring Boot 禁用 HawtIO 身份验证,请将以下配置添加到 application.properties
中:
hawtio.authenticationEnabled = false
hawtio.authenticationEnabled = false
4.3.1. Spring Security
将 Spring Security 与 HawtIO 搭配使用:
将
org.springframework.boot:spring-boot-starter-security
添加到pom.xml
中的依赖项中:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
Copy to Clipboard Copied! src/main/resources/application.properties
中的 Spring Security 配置应该类似如下:spring.security.user.name = hawtio spring.security.user.password = s3cr3t! spring.security.user.roles = admin,viewer
spring.security.user.name = hawtio spring.security.user.password = s3cr3t! spring.security.user.roles = admin,viewer
Copy to Clipboard Copied! 必须定义安全配置类来设置如何使用 Spring Security 保护应用程序:
@EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(authorize -> authorize .anyRequest().authenticated() ) .formLogin(withDefaults()) .httpBasic(withDefaults()) .csrf(csrf -> csrf .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .csrfTokenRequestHandler(new SpaCsrfTokenRequestHandler()) ) .addFilterAfter(new CsrfCookieFilter(), BasicAuthenticationFilter.class); return http.build(); } }
@EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(authorize -> authorize .anyRequest().authenticated() ) .formLogin(withDefaults()) .httpBasic(withDefaults()) .csrf(csrf -> csrf .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .csrfTokenRequestHandler(new SpaCsrfTokenRequestHandler()) ) .addFilterAfter(new CsrfCookieFilter(), BasicAuthenticationFilter.class); return http.build(); } }
Copy to Clipboard Copied! 注意在身份验证成功后刷新令牌,并且需要注销成功,因为
CsrfAuthenticationStrategy
和CsrfLogoutHandler
将清除前面的令牌。客户端应用程序将无法在不获取新令牌的情况下执行不安全 HTTP 请求,如 POST。
Example:
有关工作 示例,请参阅 springboot-security 示例。
4.3.1.1. 使用 Spring Security 连接到远程应用程序
如果您试图连接到启用了 Spring Security 的远程 Spring Boot 应用程序,请确保 Spring Security 配置允许从 HawtIO 控制台访问。最有可能,默认的 CSRF 保护阻止远程访问 Jolokia 端点,因此会导致 HawtIO 控制台的身份验证失败。
请注意,它将使您的应用程序暴露于 CSRF 攻击的风险。
最简单的解决方案是在远程应用程序中禁用 Jolokia 端点的 CSRF 保护,如下所示:
import org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpoint; import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { ... // Disable CSRF protection for the Jolokia endpoint http.csrf().ignoringRequestMatchers(EndpointRequest.to(JolokiaEndpoint.class)); return http.build(); } }
import org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpoint; import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { ... // Disable CSRF protection for the Jolokia endpoint http.csrf().ignoringRequestMatchers(EndpointRequest.to(JolokiaEndpoint.class)); return http.build(); } }
Copy to Clipboard Copied! 要保护 Jolokia 端点,即使没有 Spring Security 的 CSRF 保护,您需要在
src/main/resources/
下提供一个jolokia-access.xml
文件,如下所示(snippet),以便只有可信节点可以访问它:<restrict> ... <cors> <allow-origin>http*://localhost:*</allow-origin> <allow-origin>http*://127.0.0.1:*</allow-origin> <allow-origin>http*://*.example.com</allow-origin> <allow-origin>http*://*.example.com:*</allow-origin> <strict-checking /> </cors> </restrict>
<restrict> ... <cors> <allow-origin>http*://localhost:*</allow-origin> <allow-origin>http*://127.0.0.1:*</allow-origin> <allow-origin>http*://*.example.com</allow-origin> <allow-origin>http*://*.example.com:*</allow-origin> <strict-checking /> </cors> </restrict>
Copy to Clipboard Copied!