Keycloak和Spring Boot - 如何应用资源作用域

5

我正在使用Keycloak 7.0.1和Spring Boot 1.5.16.RELEASE,通过指定资源、基于角色的策略和权限来保护端点,并且这正如预期的那样工作。

麻烦的是只保护POST请求,并允许GET请求到特定的URI。在application.yml中我做了什么:

policy-enforcer-config:
  enforcement-mode: ENFORCING
  paths[0]:
    name: all
    path: /*
  paths[1]:
    name: test post
  path: /my/url
    methods[0]:
      method: GET
      scopes[0]: view
    methods[1]:
      method: POST
      scopes[0]: edit

在keycloak中,我创建了编辑查看范围,/my/url资源,策略包含角色和负面决策(如果用户具有该角色-拒绝访问),权限包含资源,范围和策略。评估按预期运行,但我的Spring应用程序始终收到403错误。
请提供一个资源范围使用示例或建议还需要完成哪些内容才能使其正常工作?

你是否在使用Spring Security时使用了@EnableGlobalMethodSecurity(prePostEnabled = true)这个注解? - UsamaAmjad
不,只有Ant匹配器。 - Javasick
1个回答

3

可能有多个问题,但首先,请检查您的SecurityConfig。

这是我们现在采用的:

@Configuration
@EnableWebSecurity
@KeycloakConfiguration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableConfigurationProperties(KeycloakSpringBootProperties.class)
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    bla-bla..

@Override
public void configure(WebSecurity web) throws Exception {
    web
            .ignoring()
            .antMatchers(HttpMethod.POST, "/auth/**")
            .antMatchers(HttpMethod.OPTIONS,"/**")
            // allow anonymous resource requests
            .and()
            .ignoring()
            .antMatchers(
                    HttpMethod.GET,
                    "/",
                    "/*.html",
                    "/favicon.ico",
                    "/**/*.html",
                    "/**/*.css",
                    "/**/*.js",
                    "/actuator/**"

            )
    ;
}


    @Override
protected void configure(HttpSecurity http) throws Exception {
    http
            // we don't need CSRF because our token is invulnerable
            .csrf().disable()

            .exceptionHandling()
            .defaultAuthenticationEntryPointFor(
                    getRestAuthenticationEntryPoint(),
                    new AntPathRequestMatcher("/**")
            )
            .authenticationEntryPoint(unauthorizedHandler).and()

            // don't create session
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()

            .authorizeRequests()

            // system state endpoint
            .antMatchers("/ping").permitAll()

            .antMatchers(HttpMethod.GET, "/whatever/you/need/to/open/to/public/one", "/whatever/you/need/to/open/to/public/two").permitAll()


            // User authentication actions
            .antMatchers("/auth/**").permitAll()
            .antMatchers("/**/*.css").permitAll()

            .anyRequest().authenticated()
    ;

    http
            .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
    ;

    // disable page caching
    http
            .headers()
            .frameOptions().sameOrigin()
            .cacheControl();

}

如果您想通过角色限制REST API端点,请在您的控制器方法中添加@PreAuthorize("hasRole('keycloak中的您的角色')")


谢谢,已经实现并且运行正常。无法仅利用POST和GET分离。 - Javasick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接