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
相关推荐
程序员天天困几秒前
优雅记录操作日志:从注解到 SpEL 的全链路实践与开源方案对比
java·后端
战血石LoveYY18 分钟前
Integer类型超限,导致数据异常
java·算法
qetfw21 分钟前
CentOS 7 配置 iptables 防火墙
安全
磐链科技33 分钟前
区块链链游安全警示:常见漏洞与攻击手段
安全·区块链
Fuzio1 小时前
用 Spring Boot + Vue + Fuzio 构建现代 Java 桌面应用
vue.js·spring boot
饼干哥哥1 小时前
Vibe Coding 出海首月收割 100+用户爆赚美金,怎么做?
后端·架构·代码规范
大康2 小时前
macOS Homebrew 重装与国内镜像配置手册
后端
行者全栈架构师2 小时前
大模型批量生成代码的质量衰减:为什么 AI 越写越敷衍?根因分析与系统性解决方案
后端·架构·代码规范
乐观的Terry2 小时前
1、为什么要自己造发布系统
java·spring boot·后端·spring cloud·架构
用户EasyAdminBlazor2 小时前
EasyAdminBlazor 第十篇:数据导入导出——批量处理不再麻烦
前端·后端