【发布时间】:2019-09-30 17:25:26
【问题描述】:
您能帮助保护 Spring Boot 2 中的执行器端点吗?我检查了迁移指南,但它对我没有帮助。
这是我的安全配置:
@Configuration
@EnableWebSecurity
public class SecConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ADMIN")
.anyRequest().authenticated();
}
}
但是当我转到http://localhost:8080/actuator/health 时,它无需登录即可加载。其他前缀为 /actuator 的端点也不需要登录。我做错了什么?
我还使用此配置添加了 OAuth:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("client-id")
.scopes("read", "write")
.authorizedGrantTypes("password")
.secret("xxxxxx")
.accessTokenValiditySeconds(6000);
}
}
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/ajax/**").authenticated()
.and()
.csrf()
.disable();
}
}
【问题讨论】:
-
@DennisGlot 我开始了新项目,这是第一个配置:/ 我只是在 configureGlobal(AuthenticationManagerBuilder auth) 中添加了 userService 和密码编码器
-
@DenisStephanov 您是否在一个应用程序中拥有所有配置? AFAIR 那么你的配置顺序是
AuthorizationServerConfig(仅OAuth2端点)、ResourceServerConfig(所有端点)和SecConfig(从未使用过)。 -
@DenisStephanov 你必须限制你的资源配置。例如,您可以将
.antMatcher("/ajax/**")添加到您的资源服务器配置中,以仅应用/ajax/**的配置而不应用/actuator的配置。
标签: java spring spring-boot spring-security spring-boot-actuator