无法在 spring 启动应用程序中配置 Keycloak 17 - 元素 [keycloak.keycloak.bearer-only] 未绑定

Can not configure Keycloak 17 in spring boot app - elements [keycloak.keycloak.bearer-only] were left unbound

我需要在 Spring 启动应用程序中禁用登录页面 Keycloak 重定向,这是我的配置:

application.yml

server:
  port: 8091
  connection-timeout: 6000
keycloak:
  enabled: true
  bearer-only: true
  auth-server-url: http://localhost:8484/auth
  realm: test-realm
  resource: my-test-client
  keycloak.bearer-only: true
  use-resource-role-mappings: false

安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);

        http.cors().and().csrf().disable().sessionManagement().

                sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
                .antMatchers("/users/unprotected-data").permitAll()
                .antMatchers("/users/create").permitAll()
                .antMatchers("/users/signin").permitAll()
                .anyRequest().authenticated();

    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }

    @Bean
    public KeycloakConfigResolver keycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new NullAuthenticatedSessionStrategy();
    }

    @Autowired
    public KeycloakClientRequestFactory keycloakClientRequestFactory;

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public KeycloakRestTemplate keycloakRestTemplate(){
        return new KeycloakRestTemplate(keycloakClientRequestFactory);
    }
}

但我有一条错误配置消息,KeycloakSpringBootProperties cannot bi binded to ConfigurationProperties:

Description:

Binding to target [Bindable@7bb35cc6 type = org.keycloak.adapters.springboot.KeycloakSpringBootProperties, value = 'provided', annotations = array<Annotation>[@org.springframework.boot.context.properties.ConfigurationProperties(ignoreInvalidFields=false, ignoreUnknownFields=false, prefix=keycloak, value=keycloak)]] failed:

    Property: keycloak.keycloak.bearer-only
    Value: true
    Origin: class path resource [application.yml] - 10:25
    Reason: The elements [keycloak.keycloak.bearer-only] were left unbound.

Action:

Update your application's configuration

更新配置的正确方法是什么?

据我所知,

它是 keycloak.bearer-only: true

keycloak:
bearer-only: true

两者相等。

但是你试图做的是 keycloak.keycloak.bearer-only: true 这是不正确的。

这是一个answer可能有用的

keycloak v17 中的 auth 服务器 url 它是 http://localhost:${port number} 没有 /auth ,你可以在这里查看 spring 引导适配器配置 https://www.keycloak.org/docs/latest/securing_apps/#_spring_boot_adapter 它也是 keycloak.bearer-only=true 而不是 keycloak.keycloak.bearer-only=true