【发布时间】:2018-04-26 19:29:07
【问题描述】:
我正在尝试将 Keycloak 与我的 Spring Boot 应用程序一起使用。
我想根据 REST 方法和用户角色限制对特定 URL 的访问。
在下面的示例中,具有view-all 或calendar 任何角色的用户都可以执行GET,而具有manage-all 或calendar_manage 的用户可以执行POST、PUT 或DELETE。
不幸的是,此配置允许任何经过身份验证的用户访问 /api/calendar URL。我做错了什么?
@Configuration
@EnableWebSecurity
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
...
@Override
protected void configure(HttpSecurity http) throws Exception
{
super.configure(http);
http
.csrf().disable()
.antMatcher("/api/**")
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyRole("view-all", "calendar")
.antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
.antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage")
.antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyRole("manage-all", "calendar_manage");
}
}
【问题讨论】:
标签: spring spring-security keycloak