Spring Boot升级后Actuator与REST接口权限配置异常问题
解决Spring Boot 2.0.1 Actuator认证与REST API公开访问的问题
Hey,我刚好踩过Spring Boot 1.x升级到2.x时Security配置的坑,给你一步步搞定这个权限异常的问题!
首先得明白:Spring Boot 2.x的Spring Security自动配置逻辑和1.x完全不一样了,原来的security.user.*配置升级成spring.security.user.*后,默认会保护所有路径,包括你的REST API,这就是你遇到权限异常的原因。我们需要自定义Security规则,明确区分Actuator和REST API的权限。
步骤1:确保依赖正确
先检查你的pom.xml(或build.gradle)里已经引入了这两个starter:
<!-- Spring Security --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <!-- Actuator --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
步骤2:自定义Security配置类
创建一个SecurityConfig类,继承WebSecurityConfigurerAdapter(这是Spring Boot 2.0.x版本的标准方式),明确配置哪些路径公开,哪些需要认证:
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // 生产环境一定要用加密的密码编码器,这里用BCrypt @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http) throws Exception { http // 配置权限规则 .authorizeRequests() // 替换成你的实际REST API路径,比如/api/**,允许所有访问 .antMatchers("/your-rest-api/**").permitAll() // 其他所有路径(包括Actuator的/actuator/**)需要认证 .anyRequest().authenticated() .and() // 启用HTTP Basic认证,这是Actuator默认的认证方式 .httpBasic(); // 如果不需要CSRF保护(比如纯后端API),可以关闭 // http.csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // 配置内存用户,对应你原来的foo/bar/ADMIN auth.inMemoryAuthentication() .withUser("foo") // 用编码器加密密码,生产环境不要明文存储 .password(passwordEncoder().encode("bar")) .roles("ADMIN"); } }
关键细节说明:
- 密码编码器:Spring Security 5+(Spring Boot 2.x内置)强制要求密码必须有编码器,
{noop}是明文密码的标记(只适合测试),生产环境一定要用BCryptPasswordEncoder这类安全的编码器。 - 路径匹配:
antMatchers("/your-rest-api/**")要替换成你实际的REST API前缀,比如你的API都在/api下,就写/api/**。 - Actuator路径:Spring Boot 2.x默认Actuator的基础路径是
/actuator,所以anyRequest().authenticated()会自动覆盖这个路径,确保只有认证用户能访问。
步骤3:配置Actuator端点暴露
Spring Boot 2.x默认只暴露health和info两个Actuator端点,如果你需要访问其他端点(比如metrics、beans),要在application.properties(或application.yml)里配置:
# 暴露所有Actuator端点(也可以指定具体端点,比如actuator,metrics) management.endpoints.web.exposure.include=* # 可选:显示健康端点的详细信息 management.endpoint.health.show-details=always
可选:用配置属性替代内存用户
如果你不想在代码里写用户信息,也可以用application.properties配置,但要配合密码编码器前缀:
spring.security.user.name=foo # 注意:如果用BCrypt加密,这里要写加密后的密码;如果用明文测试,加{noop}前缀 spring.security.user.password={noop}bar spring.security.user.roles=ADMIN
这种情况下,你可以去掉configure(AuthenticationManagerBuilder auth)的代码,Spring会自动加载这些配置。
测试验证
- 访问你的REST API,比如
http://localhost:8080/your-rest-api/test,应该不需要认证就能正常返回。 - 访问Actuator端点,比如
http://localhost:8080/actuator/metrics,浏览器会弹出登录框,输入foo/bar后才能访问。
内容的提问来源于stack exchange,提问作者Marged
相关产品推荐
相关产品推荐

