【发布时间】:2015-12-03 05:31:29
【问题描述】:
我正在尝试在方法级别定义访问规则,但它并没有像以前那样工作。
安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().
withUser("user").password("user").roles("USER").and().
withUser("admin").password("admin").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/v2/**").authenticated()
.and()
.httpBasic()
.realmName("Secure api")
.and()
.csrf()
.disable();
}
}
示例控制器
@EnableAutoConfiguration
@RestController
@RequestMapping({"/v2/"})
public class ExampleController {
@PreAuthorize("hasAuthority('ROLE_ADMIN')")
@RequestMapping(value = "/home", method = RequestMethod.GET)
String home() {
return "Hello World";
}
}
每当我尝试使用 user:user 访问 /v2/home 时,它执行得很好,它不应该因为“用户”没有 ROLE_ADMIN 而给我一个访问被拒绝错误吗?
我实际上正在考虑在方法级别放弃访问规则并坚持 http() ant 规则,但我必须知道为什么它不适合我。
【问题讨论】:
标签: spring spring-security spring-boot spring-java-config