Spring Boot Actuator 端点默认受基本 http 安全保护。
可以更改为使用 Spring Security 吗?
我已成功设置 Spring Security 并使用它来保护我的其他页面。
I tried security.basic.enabled: false
并添加.antMatchers("/manage/**").hasRole("ADMIN")
在我的授权请求中(请注意,我使用不同的 url 作为端点的根),但这没有帮助。
我不断收到基本的 http 身份验证日志,该日志没有在 AuthenticationManager 中配置的用户。
任何想法?
编辑-提供更多详细信息-
我的 Application.java 看起来像:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/app").setViewName("app/index");
registry.addViewController("/app/login").setViewName("app/login");
}
@Bean
public ApplicationSecurity applicationSecurity() {
return new ApplicationSecurity();
}
@Order(Ordered.LOWEST_PRECEDENCE - 8)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth.inMemoryAuthentication()
.withUser("test1")
.password("test1pw")
.roles("USER", "ADMIN")
.and()
.withUser("test2")
.password("test2pw")
.roles("USER");
// @formatter:on
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/app/login").permitAll()
.antMatchers("/app/**").hasRole("USER")
.antMatchers("/manage/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/app/login")
.failureUrl("/app/login?error")
.defaultSuccessUrl("/app")
.permitAll()
.and()
.logout()
.logoutUrl("/app/logout")
.logoutSuccessUrl("/app/login?logout");
// @formatter:on
}
@Override
public void configure(WebSecurity web) throws Exception {
// @formatter:off
web
.ignoring()
.antMatchers("/assets/**");
// @formatter:on
}
}
}
In my application.yml
我也有:
management:
context-path: /management
请注意,设置与您提到的指南相同。
现在我期望 - 或者想要配置 - 是 /manage 端点(例如运行状况、映射等)将受到来自自定义 AuthenticationManager 的用户的保护。
我也尝试添加management.security.enabled=false
这确实关闭了身份验证,例如/管理/映射。
然而,有问题的是,我明确告诉 Spring Security 通过以下方式保护这些 url:
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers("/app/login").permitAll()
.antMatchers("/app/**").hasRole("USER")
.antMatchers("/manage/**").hasRole("ADMIN")
但这不起作用。请注意其他授权匹配器确实有效。
我想知道是否与时间/顺序有关。我复制了@Order(Ordered.LOWEST_PRECEDENCE - 8)
从示例中,但我不知道为什么使用 - 8。
为了更深入地研究,我还运行了示例(https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-web-method-security https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-web-method-security)我和我在示例应用程序中看到了相同的行为。
管理安全似乎完全独立于user
and admin
在示例的内存身份验证中配置的用户。