【问题标题】:spring security keycloak adapter弹簧安全钥匙披风适配器
【发布时间】:2018-04-26 19:29:07
【问题描述】:

我正在尝试将 Keycloak 与我的 Spring Boot 应用程序一起使用。

我想根据 REST 方法和用户角色限制对特定 URL 的访问。

在下面的示例中,具有view-allcalendar 任何角色的用户都可以执行GET,而具有manage-allcalendar_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


    【解决方案1】:

    尝试使用 isAuthenticatedhasAnyAuthority 以及 ("/api/**") 的权限

           @Configuration
            @EnableWebSecurity
            class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
            ...
                @Override
                protected void configure(HttpSecurity http) throws Exception
                {
                    super.configure(http);
                    http    .authorizeRequests()
                            .csrf().disable()
                            .antMatcher("/api/**").permitAll()
                            .isAuthenticated()
                            .antMatchers(HttpMethod.GET, "/api/calendar/*").hasAnyAuthority("view-all", "calendar")
                            .antMatchers(HttpMethod.POST, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                            .antMatchers(HttpMethod.PUT, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage")
                            .antMatchers(HttpMethod.DELETE, "/api/calendar/*").hasAnyAuthority("manage-all", "calendar_manage");
                }
            }
    

    【讨论】:

    • 无法编译 :-(
    【解决方案2】:

    诀窍是 - 您需要在蚂蚁匹配器的路径末尾添加一个 /**

    (我不得不通过 Spring 代码来解决这个问题 - 请参阅 Spring Core 的 AntPathMatcher 类中的方法 doMatch()

    .antMatchers(HttpMethod.GET, "/calendar/**")
        .hasAnyRole("view-all", "calendar", "calendar_manage")
    .antMatchers(HttpMethod.POST, "/calendar/**")
        .hasAnyRole("manage-all", "calendar_manage")
    .antMatchers(HttpMethod.PUT, "/calendar/**")
         .hasAnyRole("manage-all", "calendar_manage")
    .antMatchers(HttpMethod.DELETE, "/calendar/**")
        .hasAnyRole("manage-all", "calendar_manage")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2023-01-03
      • 2018-09-08
      • 1970-01-01
      • 2021-11-29
      • 2020-05-19
      相关资源
      最近更新 更多