Spring引导-在启动时覆盖OAuth客户端配置

Spring引导-在启动时覆盖OAuth客户端配置,spring,spring-boot,oauth,openid,Spring,Spring Boot,Oauth,Openid,我有一个具有以下配置的Spring引导应用程序: @SpringBootApplication @EnableOAuth2Sso @Configuration public class DemoApplication extends WebSecurityConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http

我有一个具有以下配置的Spring引导应用程序:

@SpringBootApplication
@EnableOAuth2Sso
@Configuration
public class DemoApplication extends WebSecurityConfigurerAdapter {

 @Override
    public void configure(HttpSecurity http) throws Exception {

        http
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/","/appConfig", "/login/**", "/webjars/**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .exceptionHandling()
            .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
            .and().logout()
            .logoutSuccessUrl("/").permitAll()
            .and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());

    } 
...
}
在参考资料中,我有一个application.yml,其中包含客户端配置:

security:
      oauth2:
        client:
          clientId: @keycloak.client.id@
          clientSecret: @keycloak.client.secret@
          accessTokenUri: https://@keycloak.host@:@keycloak.port@/auth/realms/@keycloak.realm@/protocol/openid-connect/token
          userAuthorizationUri: https://@keycloak.host@:@keycloak.port@/auth/realms/@keycloak.realm@/protocol/openid-connect/auth
          authenticationScheme: header
          clientAuthenticationScheme: header
        resource:
          userInfoUri: https://@keycloak.host@:@keycloak.port@/auth/realms/@keycloak.realm@/protocol/openid-connect/userinfo
到目前为止,它还可以正常工作,但我必须在应用程序启动时以编程方式设置clientSecret(可能还有其他属性),因为客户端也在启动时向OpenId服务器注册,所以只有在注册完成后才能知道该密则

我使用AuthorizationServerConfigurerAdapter进行了一些实验,以创建inMemory客户端,但如果它也添加到筛选器链中,则应用程序根本不会启动:

- Bean method 'userInfoRestTemplateFactory' not loaded because @ConditionalOnMissingBean (types: org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration; SearchStrategy: all) found bean 'org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerEndpointsConfiguration'

如何在启动时通过代码设置clientSecret,而不是在应用程序中对其进行硬编码。yml?

这些属性转到
ResourceServerProperties
对象。如果您自己创建它会怎么样:

@Bean
public ResourceServerProperties ouathResource() {
   // read from somewhere
   return new ResourceServerProperties( id, secret)
}

此bean的存在可能会抑制其他bean的自动创建,在这种情况下,它们也必须手动创建。

谢谢!做了,并用@Primary标记了它,这样spring就知道使用这两个bean中的哪一个了。但是这样就根本不设置clientId和secret了。如果您用设置
id
secret
的代码替换
//从某处读取
?是的,当然-我将id和secret硬编码给构造函数只是为了确定,如果它像这样工作,我会在以后进行动态读取。我在问自己,如果在过滤器链中创建这个bean还不算太晚。。。