Spring Security-多個(gè)OAuth2身份提供程序(github和google)只能使用一個(gè)Bean和@Prirmary?

我有一個(gè)Spring啟動(dòng)應(yīng)用程序版本2.7.10,我希望能夠通過OAuth2向兩個(gè)身份提供程序(githubgoogle)進(jìn)行身份驗(yàn)證。

基本上,我的web應(yīng)用程序應(yīng)該允許用戶選擇githubgoogle進(jìn)行身份驗(yàn)證,但我無法使它同時(shí)適用于兩個(gè)身份驗(yàn)證提供程序,只有在OAuth2ClientServiceConfig.java中有bean注釋的@Primary

有了這一點(diǎn),我需要兩個(gè)提供程序的配置,如下所示:

  • Oauth2ClientConfiguration.java


@Configuration
public class Oauth2ClientConfiguration {

    @Bean(name = "github")
    public ClientRegistrationRepository githubClientRegistrationRepository() {

        ClientRegistration clientRegistration = ClientRegistration.withRegistrationId("github")
                .clientId("GITHUB_CLIENT_ID")
                .clientSecret("GITHUB_CLIENT_SECRET")
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUriTemplate("{baseUrl}/login/oauth2/code/{registrationId}")
                .scope("user")
                .authorizationUri("https://github.com/login/oauth/authorize")
                .tokenUri("https://github.com/login/oauth/access_token")
                .userInfoUri("https://api.github.com/user")
                .userNameAttributeName("id")
                .clientName("gitHub")
                .build();

        return new InMemoryClientRegistrationRepository(clientRegistration);

    }
    
    
    @Bean(name = "google")
    public ClientRegistration clientRegistration = ClientRegistration.withRegistrationId("google")
                .clientId("GOOGLE_CLIENT_ID")
                .clientSecret("GOOGLE_CLIENT_SECRET")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
                .scope("openid", "profile", "email", "address", "phone")
                .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
                .tokenUri("https://www.googleapis.com/oauth2/v4/token")
                .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
                .userNameAttributeName(IdTokenClaimNames.SUB)
                .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
                .clientName("Google")
                .build();

        return new InMemoryClientRegistrationRepository(clientRegistration);

    }
    
}

  • OAuth2ClientServiceConfig.java
@Configuration
public class OAuth2ClientServiceConfig {

    @Bean(name = "githubClientService")
    public OAuth2AuthorizedClientService githubAuthorizedClientService(
            @Qualifier("github") ClientRegistrationRepository githubClientRegistrationRepository) {
        return new InMemoryOAuth2AuthorizedClientService(githubClientRegistrationRepository);
    }

    @Bean(name = "googleClientService")
    @Primary
    public OAuth2AuthorizedClientService googleAuthorizedClientService(
            @Qualifier("google")  ClientRegistrationRepository googleClientRegistrationRepository) {
        return new InMemoryOAuth2AuthorizedClientService(googleClientRegistrationRepository);
    }


}
  • WebSecurityConfig.java(注意類底部的note注釋)

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    @Qualifier("githubClientService")
    private OAuth2AuthorizedClientService githubOAuth2AuthorizedClientService;

    @Autowired
    @Qualifier("googleClientService")
    private OAuth2AuthorizedClientService googleOAuth2AuthorizedClientService;

    @Autowired
    @Qualifier("github")
    private ClientRegistrationRepository githubClientRegistrationRepository;

    @Autowired
    @Qualifier("google")
    private ClientRegistrationRepository googleClientRegistrationRepository;
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
            http
                .addFilterBefore(new CustomSecurityExceptionHandlingFilter(), UsernamePasswordAuthenticationFilter.class)
                .oauth2Login()
                .authorizationEndpoint()
                .authorizationRequestResolver(
                        new CustomAuthorizationRequestResolver(
                                googleClientRegistrationRepository)
                )
                .and()
                .loginPage("/login") // this one is important to not use Spring security formlogin
                .clientRegistrationRepository(googleClientRegistrationRepository)
                .successHandler(this::handleOAuth2Login)
                
    }
    
    private void handleOAuth2Login(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException {
        String clientRegistrationId = "google";

        OAuth2User oauth2User = (OAuth2User) authentication.getPrincipal();


        // NOTE:
        // - authorizedClient here is NOT null if @Primary is SET to @Bean(name = "googleClientService") in OAuth2ClientServiceConfig.java
        // - authorizedClient here is null if @Primary is  SET to @Bean(name = "githubClientService") in OAuth2ClientServiceConfig.java

        OAuth2AuthorizedClient authorizedClient = googleOAuth2AuthorizedClientService.loadAuthorizedClient(
                clientRegistrationId, oauth2User.getName())
                
         String accessToken = authorizedClient.getAccessToken().getTokenValue();        
                

                
                
    }


}


我不確定我遺漏了什么,或者Spring安全性有一些限制,無法通過OAuth2支持通過多個(gè)身份提供程序進(jìn)行身份驗(yàn)證。

? 最佳回答:

ClientRegistrationRepository與任何存儲庫一樣,旨在管理多個(gè)注冊。

@Bean
ClientRegistrationRepository clientRegistrationRepository() {
  final var github = ClientRegistration.withRegistrationId("github")
      .clientId("GITHUB_CLIENT_ID")
      .clientSecret("GITHUB_CLIENT_SECRET")
      .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
      .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
      .scope("user")
      .authorizationUri("https://github.com/login/oauth/authorize")
      .tokenUri("https://github.com/login/oauth/access_token")
      .userInfoUri("https://api.github.com/user")
      .userNameAttributeName("id")
      .clientName("gitHub")
      .build();

  final var google = ClientRegistration.withRegistrationId("google")
      .clientId("GOOGLE_CLIENT_ID")
      .clientSecret("GOOGLE_CLIENT_SECRET")
      .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
      .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
      .redirectUri("{baseUrl}/login/oauth2/code/{registrationId}")
      .scope("openid", "profile", "email", "address", "phone")
      .issuerUri("https://accounts.google.com")
      .userNameAttributeName(IdTokenClaimNames.SUB)
      .clientName("Google")
      .build();

  return new InMemoryClientRegistrationRepository(github, google);
}
主站蜘蛛池模板: 国模私拍一区二区三区| 国产精品亚洲一区二区三区久久| 日韩毛片一区视频免费| 人妻av无码一区二区三区| 日本一区二区三区中文字幕| 久久99精品一区二区三区| 久久精品国产免费一区| 国产精品无码一区二区在线观一| 一区二区三区在线免费| 在线视频一区二区三区四区| 在线成人一区二区| 无码人妻一区二区三区免费| 无码丰满熟妇一区二区| 亚洲一区二区三区亚瑟 | 国产一区二区三区不卡观| 国产萌白酱在线一区二区| 亚洲AV噜噜一区二区三区| 亚洲午夜一区二区三区| 在线免费观看一区二区三区| 亚洲日韩一区精品射精| 亚洲一区二区三区高清在线观看 | 国产一区二区精品| 亚洲啪啪综合AV一区| 亚洲福利一区二区| 国产一区二区免费在线| 成人无码一区二区三区| 亚洲一区二区三区无码中文字幕| 国产成人精品无人区一区| 水蜜桃av无码一区二区| 春暖花开亚洲性无区一区二区| 国产伦精品一区二区三区不卡| 成人无码一区二区三区| 91精品一区国产高清在线| 国产亚洲综合一区柠檬导航 | 成人一区专区在线观看| 国产一区二区三区亚洲综合| 一区二区三区在线看| 国产AV一区二区三区传媒| 精品人妻码一区二区三区| 国产精品无码一区二区在线观 | 国产免费无码一区二区 |