【发布时间】:2018-03-13 11:28:00
【问题描述】:
对通过用户角色保护端点有疑问。我有一个端点“/message”,它以如下所示的两种方式之一受到保护
1) 在Controller中如下
@PreAuthorize("hasAuthority('USER')")
@RequestMapping(value = "/message", method = RequestMethod.GET)
public String message() {
return "Hello World!!!"
}
2)在配置(WebSecurityConfigurereAdapter)文件中如下
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().anyRequest().authenticated()
.antMatchers(HttpMethod.GET, "/message").access("hasAuthority('USER')");
}
可以看出角色 USER 已经被两种方式硬编码,如何动态实现,一种方式是我们可以从配置文件中的数据库中读取并构建 HttpSecurity,但这发生在应用程序启动期间,在运行时创建的新角色如何保护端点?
【问题讨论】:
-
你必须实现 PermissionEvaluator 和 example:
标签: spring-boot spring-security