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 两个端点。通过设置 * ,可以开放所有端点。
相关推荐
减瓦1 小时前
Jackson 使用指南
java·spring boot·json
Devin~Y1 小时前
互联网大厂 Java 面试实录:Spring Boot、MyBatis、Redis、Kafka、Spring Security、RAG 与 MCP 全链路问答
java·redis·kafka·mybatis·spring security·spring mvc·sprint boot
weixin_437662981 小时前
redisson
java
码农进化录1 小时前
Java 程序员的 AI 进化论 | AI 加 Postman 跑接口测试,省了三天活
java·后端·openai
前鼻音太阳熊1 小时前
【MES系统】- 工业HMI状态机可视化实践:从手写SVG到Cytoscape.js的技术选型与踩坑
开发语言·javascript·ecmascript
阿米亚波2 小时前
【C++ STL】std::array
java·开发语言·c++
奶糖 肥晨2 小时前
mac系统中Java项目环境配置与问题解决全记录| 项目构建篇:Maven编译与打包
java·macos·maven
wuyk5552 小时前
66.嵌入式C语言进阶:共用体(Union)实战指南——单片机内存省一半的神级技巧
c语言·开发语言·stm32·单片机·嵌入式硬件
Ivanqhz2 小时前
Rust #[derive(Serialize)]浅析
开发语言·后端·rust
怪奇云呼军2 小时前
闪电智能 Voice Agent 怎样根据语速、停顿和追问方式切换话术?策略引擎拆解
开发语言·人工智能·网络协议·算法·语音识别·web app