【发布时间】:2014-01-14 20:43:36
【问题描述】:
使用 Spring Boot 和 Spring Security,我配置了一个超级简单的应用程序并基于 URL 添加了安全性,但没有得到尊重。例如,如果我以“侦察员”身份登录并点击像 /api/leaders 这样的管理员 URL,我仍然可以看到它。
我已经尝试了许多 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上:https://github.com/ssoward/scouts
【问题讨论】:
-
您的
/apiURL 上是否映射了 spring securityDelegatingFilterProxy? -
不,我没有那个过滤器。我以为 Spring Boot Actuator 会自动配置它。
标签: java spring spring-mvc spring-security