Mar*_*ijk 12 spring-security spring-boot
Spring Boot Actuator端点默认受基本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
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的application.yml我也有:
management:
context-path: /management
Run Code Online (Sandbox Code Playgroud)
请注意,设置与您提到的指南相同.
现在我期望 - 或者喜欢配置 - 是健康,映射等/ manage端点将受到来自定制AuthenticationManager的用户的保护.
我也尝试添加management.security.enabled=false,这确实关闭了例如/ manage/mappings的身份验证.然而,有问题的是我明确告诉Spring Security保护这些网址:
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers("/app/login").permitAll()
.antMatchers("/app/**").hasRole("USER")
.antMatchers("/manage/**").hasRole("ADMIN")
Run Code Online (Sandbox Code Playgroud)
但这不起作用.注意其他授权匹配器确实有效.我想知道在时间/顺序中是否有事可做.我@Order(Ordered.LOWEST_PRECEDENCE - 8)从样本中复制了但我不知道为什么 - 使用了8.
为了深入研究,我还运行了这个示例(https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-web-method-安全)我自己和我在示例应用程序中看到相同的行为.管理安全性似乎完全独立于内存身份验证中样本中配置的用户user和admin用户.
可以更改为使用 Spring Security 吗?
它是Spring Security(您认为我们还会使用什么?)。如果您只想保留默认的安全规则并自定义它,那么如果您使用Spring Security 团队推荐的AuthenticationManager规则,它应该可以工作。AuthenticationManagerBuilder安全方法示例具有您正在寻找的行为,因此您可以从那里复制配置模式。如果您想替换 Boot 默认身份验证策略,关键是要在示例中AuthenticationManager进行配置。GlobalAuthenticationConfigurerAdapter
您可以关闭管理安全性management.security.enabled=false(假设 Spring Security 位于类路径上)。用户指南中提到了这一点,但请随时提出澄清。
| 归档时间: |
|
| 查看次数: |
27632 次 |
| 最近记录: |