如何根据配置文件或特定属性禁用 Spring (Boot) Security OAuth?

问题描述 投票:0回答:1

我正在将 OAuth2(Google 作为 OIDC 提供商)集成到当前使用简单的

DaoAuthenticationProvider
的现有 Spring Boot 应用程序中。我已经进行了更改以使 OAuth 集成正常工作,但现在我需要提供一个配置开关以在某些环境中将其关闭(回退到使用旧的基于数据库的身份验证)。

使用 OAuth2 的配置如下所示:

spring:
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: 1234567890-1a2b3c4d5e6f7g8h9i0j.apps.googleusercontent.com
            client-secret: ABCDEF-1234-OU812_XYZZY

我的

@Configuration
身份验证类如下所示:

@Configuration
@EnableWebSecurity
public class GoogleOIDCSecurityConfiguration {

    @Bean
    OidcUserService oidcUserService() {
        Set<String> scopes = Set.of(
                "https://www.googleapis.com/auth/userinfo.email",
                "https://www.googleapis.com/auth/userinfo.profile"
                );
        OidcUserService service = new OidcUserService();
        service.setAccessibleScopes(scopes);
        return service;
    }

    @Bean
    SecurityFilterChain oidcFilterChain(HttpSecurity http, OidcUserService userService) throws Exception {
        http
            .csrf(csrf -> csrf.disable())
            .authorizeHttpRequests(this::configureAuthorization)
            .oauth2Login(c -> configureOAuth2(c, userService))
            .logout(this::configureLogout)
            ;

        return http.build();
    }

    protected void configureAuthorization(AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry authorize) {
        authorize
            .dispatcherTypeMatchers(DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ERROR).permitAll()

            .requestMatchers("/*/*.js", "/*/*/*.js", "/css/**", "/lib/**", "/assets/**", "favicon.ico").permitAll()
            .requestMatchers("/", LOGIN_PAGE, INVALID_SESSION_PAGE, LOGOUT_PAGE).permitAll()
            .requestMatchers("/foo/**").hasAnyRole(Roles.FOO)
            .requestMatchers("/admin/**").hasRole(Roles.ADMIN)
            .anyRequest().authenticated();
    }

    private void configureOAuth2(OAuth2LoginConfigurer<HttpSecurity> oauthLogin, OidcUserService userService) {
        oauthLogin
            .userInfoEndpoint(endpoint -> endpoint.oidcUserService(userService));
    }
}

我第一次有条件地禁用它的尝试是在某些配置文件中将

client-id
设置为空,并使用
@ConditionalOnProperty
引用该键。失败并出现验证异常,提示
client-id
不能为空。似乎在
spring.security.oauth2.client.registration.*
下有任何内容都会触发 OAuth 的自动配置。

我需要做的是找到一种方法来完全禁用任何 OAuth 客户端的自动配置,但仅限于某些配置文件(或使用特定的配置属性)。

spring-boot spring-security spring-oauth2
1个回答
0
投票

为什么不将 beans 中的所有配置属性放入该配置文件中?

# properties for all profiles here

---
spring:
  config:
    activate:
      on-profile:
      - oauth2
  security:
    oauth2:
      client:
        registration:
          google:
            client-id: 1234567890-1a2b3c4d5e6f7g8h9i0j.apps.googleusercontent.com
            client-secret: ABCDEF-1234-OU812_XYZZY
@Profile("oauth2")
@Configuration
@EnableWebSecurity
public class GoogleOIDCSecurityConfiguration {
    ...
}

您可能还想定义一个 Maven 配置文件来限制对

spring-boot-starter-oauth2-client
的依赖(您甚至可以配置此 Maven 配置文件来设置您的 Spring 配置文件)。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.