SpringBootloggers未授权访问漏洞处理

在编写系统的时候SpringBootloggers在未授权情况下可以访问是一个系统漏洞,我们可以通过加白名单或者直接关闭功能的方式处理。

1.添加白名单

添加SpringSecurity方法拦截,过滤用户拦截请求AuthorizeRequestsCustomizer

java 复制代码
package com.todod.basemanage.module.base.framework.security.config;

import com.todod.basemanage.framework.security.config.AuthorizeRequestsCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer;

/**
 * base 模块的 Security 配置
 */
@Configuration(proxyBeanMethods = false, value = "baseSecurityConfiguration")
public class SecurityConfiguration {

    @Value("${spring.boot.admin.context-path:''}")
    private String adminSeverContextPath;

    @Bean("baseAuthorizeRequestsCustomizer")
    public AuthorizeRequestsCustomizer authorizeRequestsCustomizer() {
        return new AuthorizeRequestsCustomizer() {

            @Override
            public void customize(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry registry) {
                // Swagger 接口文档
                registry.requestMatchers("/v3/api-docs/**").hasRole("ADMIN")
                        .requestMatchers("/webjars/**").hasRole("ADMIN")
                        .requestMatchers("/swagger-ui.html").hasRole("ADMIN")
                        .requestMatchers("/swagger-ui/**").hasRole("ADMIN");
                // Spring Boot Actuator 的安全配置
                registry.requestMatchers("/actuator").hasRole("ADMIN")
                        .requestMatchers("/actuator/**").hasRole("ADMIN");
                // Druid 监控
                registry.requestMatchers("/druid/**").hasRole("ADMIN");
                // Spring Boot Admin Server 的安全配置
                registry.requestMatchers(adminSeverContextPath).hasRole("ADMIN")
                        .requestMatchers(adminSeverContextPath + "/**").hasRole("ADMIN");
                // 文件读取
                registry.requestMatchers(buildAdminApi("/base/file/*/get/**")).hasRole("ADMIN");
            }

        };
    }

}

在yaml文件里配置用户角色权限:

XML 复制代码
spring:
  security:
    user:
      name: admin
      password: admin
      roles: ADMIN

测试一下就看看是否还可以访问,正常情况下是无法访问了

2.直接关闭端口(暴力解法)

在yaml文件中修改以下配置

XML 复制代码
# Actuator 监控端点的配置项
management:
  endpoints:
    web:
      base-path: /actuator # Actuator 提供的 API 接口的根目录。默认为 /actuator
      exposure:
        include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。

星号:'*'代表开放所有端点,我们只需要把*改成[]即可,代表空即无法访问,代码如下:

复制代码
# Actuator 监控端点的配置项
management:
  endpoints:
    web:
      base-path: /actuator # Actuator 提供的 API 接口的根目录。默认为 /actuator
      exposure:
        include: [] # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
相关推荐
寻星探路2 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly2024064 小时前
Bootstrap 警告框
开发语言
2601_949146535 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧5 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX5 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb01035 小时前
C++课后习题训练记录Day98
开发语言·c++
爬山算法6 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7256 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎6 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven