如何正确使用SpringSecurity中的SecurityConfig

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

我目前正在为我的 Web 客户端使用 oauth2 客户端,如下所示:

@Bean("OAuth2Webclient")
WebClient webClient() {

    OAuth2AuthorizedClientRepository oAuth2AuthorizedClientRepository = new HttpSessionOAuth2AuthorizedClientRepository();
    var oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(getClientRegistrationRepository(), oAuth2AuthorizedClientRepository);
    oauth.setDefaultClientRegistrationId("myclientID");
    oauth.setClientCredentialsTokenResponseClient(accessTokenResponseClient2());

    return WebClient.builder()
        .apply(oauth.oauth2Configuration()) // <- I would like to create the oauth object in the config file and inject it here as ooposed to creating it above
        .build();
}

授权服务器以非标准字段名称响应,而不是预期的“access_token”属性,我得到“accessToken”,对于“refresh_token”我得到“refreshToken”等;所以我需要创建一个自定义令牌响应转换器,将其提供给我的对象:

oauth.setClientCredentialsTokenResponseClient(accessTokenResponseClient2());

有一些额外的代码可以获取 clientRegistrationRepository 以及自定义 accessTokenResponseClient ,但我没有包含这些代码。

这效果很好,但我确信有一种更优雅的方法可以使用 SecurityConfig 类来做到这一点。我玩过它,但我不清楚如何使用它。我尝试了几个选项,它确实在启动时创建了一个 bean,并且我注意到它在服务启动时确实调用了 ServletOAuth2AuthorizedClientExchangeFilterFunction 构造函数,但是我如何访问这个 bean,以便将其添加到我的 web 客户端?

基本上,我想将上述内容包含在配置类中,如下所示:

@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain springSecurityFilterChain(HttpSecurity http) throws Exception {
    http
        ..{what to do here to replace my code above?}
    return http.build();
    }
}

感谢您的帮助。

spring-security oauth-2.0 spring-oauth2
1个回答
0
投票

要在Spring Security中正确使用

SecurityConfig
并实现更优雅的解决方案,可以按照以下步骤操作:

  1. 创建一个扩展的配置类
    WebSecurityConfigurerAdapter
    :
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // Configure your security rules here
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .oauth2Login()
                .defaultSuccessUrl("/home")
                .and()
            .oauth2Client();
    }

    // Add any additional configuration or beans here
}
  1. 从当前配置中删除
    webClient()
    方法并创建一个单独的
    @Bean
    方法来提供
    WebClient
    实例:
@Bean("OAuth2Webclient")
public WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
    return WebClient.builder()
            .apply(oauth2Login())
            .apply(oauth2Client(authorizedClientManager))
            .build();
}

@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
        ClientRegistrationRepository clientRegistrationRepository,
        OAuth2AuthorizedClientRepository authorizedClientRepository) {

    OAuth2AuthorizedClientProvider authorizedClientProvider =
            OAuth2AuthorizedClientProviderBuilder.builder()
                    .clientCredentials()
                    .build();

    DefaultOAuth2AuthorizedClientManager authorizedClientManager =
            new DefaultOAuth2AuthorizedClientManager(
                    clientRegistrationRepository, authorizedClientRepository);
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

    return authorizedClientManager;
}
  1. webClient()
    方法中,注入
    OAuth2AuthorizedClientManager
    bean,该 bean 将由 Spring Security 自动创建。

  2. 更新

    configure(HttpSecurity http)
    类中的
    SecurityConfiguration
    方法以包含必要的 OAuth 2.0 配置。这将为您处理身份验证和授权过程。

  3. 根据您的应用程序的要求自定义

    configure(HttpSecurity http)
    方法中的安全规则。

通过执行这些步骤,您可以利用 Spring Security 配置的强大功能,让它为您处理 OAuth 2.0 身份验证和授权过程。

OAuth2AuthorizedClientManager
bean 将自动创建并注入到
webClient()
方法中,允许您使用必要的 OAuth 2.0 配置构建
WebClient
实例。

~希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.