【发布时间】:2014-04-19 19:07:43
【问题描述】:
Spring Boot Actuator Endpoints 默认受基本 http 安全保护。
这可以更改为使用 Spring Security 吗? 我已成功设置 Spring Security 并使用它来保护我的其他页面。
我尝试了security.basic.enabled: false 并在我的授权请求中添加了.antMatchers("/manage/**").hasRole("ADMIN")(请注意,我使用不同的 url 作为端点的根)但这没有帮助。
我不断得到一个基本的 http auth 日志,它没有在 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
}
}
}
在我的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),我在示例应用程序中看到了相同的行为。
管理安全似乎完全独立于示例内存身份验证中配置的user 和admin 用户。
【问题讨论】:
标签: spring-security spring-boot