为什么不是';Spring安全Java配置AntMatcher是否阻止此url?
使用SpringBoot和SpringSecurity,我配置了一个超级简单的应用程序,并基于URL添加了安全性,但它不受尊重。例如,如果我以“scout”身份登录并点击管理员URL,如为什么不是';Spring安全Java配置AntMatcher是否阻止此url?,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,使用SpringBoot和SpringSecurity,我配置了一个超级简单的应用程序,并基于URL添加了安全性,但它不受尊重。例如,如果我以“scout”身份登录并点击管理员URL,如/api/leaders,我仍然可以看到它 我尝试了许多URL的排列,甚至尝试了anyRequest().denyAll(),而scout用户仍然可以访问它 @Configuration @EnableAutoConfiguration @ComponentScan(basePackages = {"com.ss
/api/leaders
,我仍然可以看到它
我尝试了许多URL的排列,甚至尝试了anyRequest().denyAll()
,而scout
用户仍然可以访问它
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.ssoward.scouts"})
public class MainConfiguration {
public static void main(String[] args) throws Exception {
SpringApplication.run(MainConfiguration.class, args);
}
//security
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
static class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return authenticationManager();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/leaders*").hasRole("ADMIN") //url level security
.antMatchers("/api/scouts*").hasRole("USER"); //url level security
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("leader").password("password").roles("USER", "ADMIN").and()
.withUser("scout").password("password").roles("USER");
}
}
}
整个项目都在Github上:Spring Actuator自动配置Spring Security,但是当我添加上面提到的配置时,它没有删除Spring Actuator添加的配置
解决方案:通过在应用程序属性文件中添加以下内容来禁用Spring执行器添加的安全性:
security.basic.enabled=false
您是否在/api
URL上映射了Spring securityDelegatingFilterProxy
?否,我没有该筛选器。我以为Spring Boot执行器会自动配置它。在Spring Boot里程碑7之后,不再需要此解决方案