最后我设法解决我的问题我只在执行器中启用了/ info和/ health端点,并且允许只有ADMIN角色的用户才能访问/ info端点我需要混合执行器管理安全和弹簧安全配置
所以我的application.yml看起来像这样:
endpoints.enabled: false
endpoints:
info.enabled: true
health.enabled: true
management.security.role: ADMIN
和春天这样的安全配置(在这里我需要改变ManagementSecurityConfig的才能有更高的优先级):
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {
@Configuration
protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
@Autowired
private AuthenticationProvider authenticationProvider;
public AuthenticationSecurity() {
super();
}
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("secret").roles("ADMIN");
}
}
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE + 2)
public static class ManagementSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.requestMatchers()
.antMatchers("/info/**")
.and()
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
}
@Configuration
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
// API security configuration
}
}
}
保护端点可能是你唯一的选择。由于您关闭了使用执行器的能力,因此禁用生产似乎是一个奇怪的选择。 – code
我能够通过附加的网络安全配置(除了执行机构的默认安全配置外)来保护/信息端点。我不喜欢的是除了_/info_之外的所有执行器端点都可以通过执行器配置来保护,即'management.security.enabled:true'。但为了保护_/info_端点,我需要为此端点创建单独的Web安全配置。好像我在代码中做了一些破解。 – Sasa