Spring security 配置执行器端点安全性
默认情况下,Spring引导执行器端点受基本http安全性保护 可以将其更改为使用Spring Security吗? 我已经成功地设置了Spring安全性,并使用它来保护我的其他页面 我尝试了Spring security 配置执行器端点安全性,spring-security,spring-boot,Spring Security,Spring Boot,默认情况下,Spring引导执行器端点受基本http安全性保护 可以将其更改为使用Spring Security吗? 我已经成功地设置了Spring安全性,并使用它来保护我的其他页面 我尝试了security.basic.enabled:false并在我的授权请求中添加.antMatchers(“/manage/**”).hasRole(“ADMIN”)(注意,我正在使用不同的url作为端点的根目录),但这没有帮助。 我一直得到一个基本的http身份验证日志,它不支持用户在Authenticat
security.basic.enabled:false
并在我的授权请求中添加.antMatchers(“/manage/**”).hasRole(“ADMIN”)
(注意,我正在使用不同的url作为端点的根目录),但这没有帮助。
我一直得到一个基本的http身份验证日志,它不支持用户在AuthenticationManager中配置
有什么想法吗
编辑-提供更多详细信息-
My 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
}
}
}
在我的应用程序.yml中,我还有:
management:
context-path: /management
请注意,is设置与您提到的指南相同
现在,我所期望的(或想要配置的)是/manage端点(如运行状况、映射等)将受到来自自定义AuthenticationManager的用户的保护
我还尝试添加management.security.enabled=false
,这确实关闭了例如/manage/mappings的身份验证。
但问题是,我明确告诉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.lower\u priority-8)
,但我不知道为什么使用-8
为了更深入地研究,我还亲自运行了sample(),我在样例应用程序中看到了相同的行为。
管理安全性似乎完全独立于在示例的内存验证中配置的用户
和管理员
用户
可以将其更改为使用Spring Security吗
它是Spring Security(你认为我们还会用什么?)。如果您只想保留默认的安全规则并自定义AuthenticationManager
,那么按照Spring安全团队的建议使用AuthenticationManagerBuilder
就可以了。具有您正在查找的行为,因此您可以从中复制配置模式。如果要替换引导默认身份验证策略,关键是在globalaauthenticationconfigureradapter
中配置AuthenticationManager
您可以使用management.security.enabled=false关闭管理安全性(假设Spring安全性位于类路径上)。在中提到了这一点,但请随意提出澄清。我想说,如果您有非常具体的情况,有时排除Spring Boot组件的自动配置并从头开始进行配置会更容易。在这种情况下,您可以使用:
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class})
如果您想保留引导安全配置的其余部分,也可以使用ManagementWebSecurityConfiguration.java
。然后你可以用这样的东西:
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration {
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
private final SecurityProperties securityProperties;
@Autowired
AuthenticationSecurity(SecurityProperties securityProperties) {
this.securityProperties = securityProperties;
}
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
// configuration
}
}
@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
private SecurityProperties security;
@Autowired
protected ApplicationSecurity(SecurityProperties security) {
this.security = security;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// you configuration
}
}
}
如您所见,我在本例中重用了SecurityProperty,以避免创建自己的SecurityProperty。我已用更多细节更新了问题。顺便说一句,我在您提到的示例中看到了相同的行为(我的代码基于此)。谢谢。我现在就不回答了,因为这是预期的行为,但看起来情况并非如此。我向示例中添加了一些测试用例,看看它们是否可以帮助我们解释()。我用指向示例中新的正确代码的指针更新了答案。很抱歉,再次响应需要一些时间。使用GlobalAuthenticationConfigurerAdapter更新的安全示例确实有效。然而,有一件奇怪的事情正在发生:当我通过management.security.enabled=false禁用管理安全性,而是设置一个定制的spring安全antMatcher[e.g.antMatchers(“/management/**”).hasRole(“ADMIN”)]时,像/management/mappings这样的管理URL是不安全的。似乎management.security.enabled=false完全推翻了其他spring安全设置。是的,当我使用@Order(Ordered.lower\u priority-11)
而不是-8
时,它就工作了。当使用-10
甚至停止启动时,选择一个值感觉有点神奇,因为@Order不是unqiue