首页 > 解决方案 > 钥匙斗篷+弹簧。无论授权标头如何,都忽略某些 API

问题描述

我想api/v1/public/**为所有经过身份验证的用户和匿名用户允许某些模式 API。

我的项目中有 Keycloak 适配器设置,我的 WebConfiguration 如下所示:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true)
public class KeycloakWebConfig extends KeycloakWebSecurityConfigurerAdapter {

// ........ other methods .........//

  @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors().and().csrf().disable()
                .authorizeRequests()
                .anyRequest().permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/v1/public/**");
    }
}

使用此设置,当我调用/api/v1/public/ping没有任何标头的 API 时,它可以正常工作。我得到了预期的回应。

但是,如果我在向同一个 api 发出请求时添加一个随机或空的 Authorization 标头,它根本不会响应。

如果我在没有任何标题字段的情况下再次拨打相同的电话(使用邮递员或 curl),它会再次正常工作。知道是什么原因造成的吗?

标签: spring-bootkeycloak

解决方案


推荐阅读