搜索

4.3. Spring Boot

download PDF

除了标准的 JAAS 身份验证外,Spring Boot 上的 HawtIO 还可以通过 Spring SecurityKeycloak 进行保护。如果要为 Spring Boot 禁用 HawtIO 身份验证,请在 application.properties 中添加以下配置:

hawtio.authenticationEnabled = false

4.3.1. Spring Security

将 Spring Security 与 HawtIO 搭配使用:

  1. org.springframework.boot:spring-boot-starter-security 添加到 pom.xml 中的依赖项:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  2. src/main/resources/application.properties 中的 Spring Security 配置应该类似如下:

    spring.security.user.name = hawtio
    spring.security.user.password = s3cr3t!
    spring.security.user.roles = admin,viewer
  3. 必须定义安全配置类来设置如何使用 Spring Security 保护应用程序:

    @EnableWebSecurity
    public class SecurityConfig
    {
        @Bean
        public SecurityFilterChain filterChain(HttpSecurity http) throws Exception
        {
            http.authorizeRequests().anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .httpBasic()
                .and()
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
            return http.build();
        }
    }

Example:

有关工作 示例,请参阅 springboot-security 示例。

4.3.1.1. 使用 Spring Security 连接到远程应用程序

如果您试图连接到启用了 Spring Security 的远程 Spring Boot 应用程序,请确保 Spring Security 配置允许从 HawtIO 控制台访问。最有可能,默认的 CSRF 保护会禁止远程访问 Jolokia 端点,因此会在 HawtIO 控制台中导致身份验证失败。

警告

请注意,它会将您的应用程序暴露于 CSRF 攻击的风险。

  1. 最简单的解决方案是在远程应用中禁用 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();
        }
    
    }
  2. 要保护 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>

4.3.2. 带有 Keycloak 的 Spring Boot

请参阅 Keycloak 集成 - Spring Boot

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.