spring boot actuator 安全配置 springboot的安全性

关于springboot Actuator框架的安全配置方案:

加入security安全验证框架

方案一:

配置信息:

复制代码
spring:
    security:
        user:
          password: admin
          name: admin

management:
  endpoints:
    web:
      base-path: /monitor
      exposure:
        include: "*"
        # 排除端点
        exclude: shutdown
  server:
    port: 9595
  endpoint:
    health:
      show-details: always
    shutdown:
      enabled: true

引入依赖信息

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

需要上下午url对进行处理;

处理方法一:只针对端点请求进行权限校验

复制代码
@Configuration
@EnableWebSecurity
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  Environment env;

  @Override
  protected void configure(HttpSecurity security) throws Exception {
        String contextPath = env.getProperty("management.endpoints.web.base-path");
        if(StringUtils.isEmpty(contextPath)) {
            contextPath = "";
        }
        security.csrf().disable().headers().frameOptions().disable();
        security.cors().and().antMatcher("/**"+contextPath+"/**")
				.authorizeRequests()
				.anyRequest()
                .authenticated().and().httpBasic();
     }
}

以下处理跨域请求

复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {

	/**
	 * 允许跨域请求
	 *
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
			.allowedOrigins("*")
			.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
			.allowCredentials(true)
			.maxAge(3600)
			.allowedHeaders("*");
	}

	@Bean
	CorsConfigurationSource corsConfigurationSource() {
		CorsConfiguration configuration = new CorsConfiguration();
		configuration.setAllowedOrigins(Arrays.asList("*"));
		configuration.setAllowedMethods(Arrays.asList("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"));
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", configuration);
		return source;
	}
}

方案二:定制端点信息

**启用端点:**默认情况下,启用除shutdown 之外的所有端点。要配置端点的启用,请使用其management.endpoint...enabled 属性。以下示例启用shutdown 端点:

properties 复制代码
management.endpoint.shutdown.enabled=true
management.endpoint.env.enabled=false

如果您希望端点启用是选择加入而不是选择退出,请将management.endpoints.enabled-by-default 属性设置为false 并使用单个端点enabled 属性重新加入。以下示例启用info endpoint并禁用所有其他端点:

properties 复制代码
management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true
相关推荐
xiaolyuh123几秒前
Spring MVC Bean 参数校验 @Validated
java·spring·mvc
独自破碎E几秒前
RabbitMQ中的Prefetch参数
分布式·rabbitmq
蕨蕨学AI7 分钟前
【Wolfram语言】45.2 真实数据集
java·数据库
Mr -老鬼18 分钟前
Rust适合干什么?为什么需要Rust?
开发语言·后端·rust
予枫的编程笔记22 分钟前
【Java集合】深入浅出 Java HashMap:从链表到红黑树的“进化”之路
java·开发语言·数据结构·人工智能·链表·哈希算法
ohoy27 分钟前
RedisTemplate 使用之Set
java·开发语言·redis
mjhcsp28 分钟前
C++ 后缀数组(SA):原理、实现与应用全解析
java·开发语言·c++·后缀数组sa
123445235 分钟前
Agent入门实战-一个题目生成Agent
人工智能·后端
IT_陈寒38 分钟前
Java性能调优实战:5个被低估却提升30%效率的JVM参数
前端·人工智能·后端
快手技术39 分钟前
AAAI 2026|全面发力!快手斩获 3 篇 Oral,12 篇论文入选!
前端·后端·算法