最後我設法解決我的問題我只在執行器中啓用了/ 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