账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Spirngboot Oauth2中的@EnableOAuth2Sso @EnableOAuth2Client有啥用?
    55
    0

    我已经用了@EnableResourceServer EnableAuthorizationServer
    问下@EnableOAuth2Sso @EnableOAuth2Client有啥用?

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 在Spring Boot Oauth2中,@EnableOAuth2Sso 是一个注解,用于启用 OAuth2 SSO(Single Sign-On)功能。OAuth2 SSO 是一种身份验证方式,允许用户使用他们的现有 OAuth2 登录凭证(如 Google 或 Facebook)登录 Spring Boot 应用程序,而无需重新创建一个新的 OAuth2 令牌。

      @EnableOAuth2Sso 的用法如下:

      ```java @EnableOAuth2Sso public class AppConfig {

      // 在 Spring Boot 应用程序的其他配置类中添加@EnableOAuth2Sso 注解
      @EnableOAuth2Sso
      public class OAuth2Config extends OAuth2ConfigImpl {
      
          // 在 OAuth2 SSO 服务中注册一个 OAuth2 SSO 注解
          @Autowired
          public void registerOAuth2Sso() {
              OAuth2SsoService ssoService = new OAuth2SsoService();
              ssoService.setConsumerKey("your_consumer_key");
              ssoService.setConsumerSecret("your_consumer_secret");
              ssoService.setAuthorizationUrl("your_authorization_url");
              ssoService.setAccessTokenUrl("your_access_token_url");
              ssoService.setAccessTokenSecret("your_access_token_secret");
              ssoService.setRememberMeUrl("your_remember_me_url");
              ssoService.setClientId("your_client_id");
              ssoService.setClientSecret("your_client_secret");
              ssoService.setScopes("scope1,scope2");
              ssoService.setSingleLogoutUrl("your_single_logout_url");
              ssoService.setSingleLogoutSessionTimeout("your_single_logout_session_timeout");
              ssoService.setLogoutUrl("your_logout_url");
      
              OAuth2AuthenticationProvider authenticationProvider = new OAuth2AuthenticationProvider(ssoService);
              authenticationProvider.setTokenEndpointUrl("your_token_endpoint_url");
              authenticationProvider.setResourceOwnerPasswordCredentialsParameterName("client_id");
              authenticationProvider.setResourceOwnerPasswordParameterType("client_secret");
              authenticationProvider.setRememberMeUrlParameterName("remember_me");
              authenticationProvider.setRememberMeUrlParameterType("token_type");
              authenticationProvider.setGrantType("password");
              authenticationProvider.setAudience("your_audience");
              authenticationProvider.setCallbackUrl("your_callback_url");
      
              authenticationProvider.setDefaultGrantType("password");
              authenticationProvider.setDefaultAudience("your_audience");
      
              configurers.put("oauth2Authentication", authenticationProvider);
          }
      }
      
      // 在 OAuth2 SSO 服务中注册 OAuth2 SSO 注解的配置项
      @Bean
      public OAuth2Configurer oauth2Config() {
          OAuth2ClientRepository OAuth2ClientRepository = new OAuth2ClientRepository();
          OAuth2ClientRepository.addClientRepository("oauth2-client-repository", OAuth2Client.class);
          OAuth2ClientRepository.addClientRepository("oauth2-client-repository", OAuth2Token.class);
          OAuth2ClientRepository.addClientRepository("oauth2-client-repository", OAuth2Redirect.class);
      
          OAuth2ClientRepository.addOAuth2SsoClientRepository("oauth2-client-repository", OAuth2SsoClient.class);
          OAuth2ClientRepository.addOAuth2SsoClientRepository("oauth2-client-repository", OAuth2Token.class);
          OAuth2ClientRepository.addOAuth2SsoClientRepository("oauth2-client-repository", OAuth2Redirect.class);
      
          OAuth2ClientRepository.setCacheRefreshPeriod(10, TimeUnit.SECONDS);
          OAuth2ClientRepository.setCacheInvalidationPeriod(10, TimeUnit.SECONDS);
      
          OAuth2AuthenticationProviderConfigurer oauth2AuthenticationProviderConfigurer = new OAuth2AuthenticationProviderConfigurer();
          oauth2AuthenticationProviderConfigurer.setOAuth2ClientRepository(OAuth2ClientRepository);
          oauth2AuthenticationProviderConfigurer.setClientDetailsService(
                  new DefaultOAuth2ClientDetailsService("clientDetailsService.xml", "client-secret.xml", "client-redirect.xml"));
          oauth2AuthenticationProviderConfigurer.setBearerFlow(true);
          oauth2AuthenticationProviderConfigurer.setRememberMeStrategy(new SingleLogoutRememberMeStrategy());
          oauth2AuthenticationProviderConfigurer.setServerTrustManager(new KeyStoreTrustManagerFactory());
          oauth2AuthenticationProviderConfigurer.setUserDetailsService(
                  new DefaultOAuth2UserDetailsService("userDetailsService.xml", "client-secret.xml", "client-redirect.xml"));
      
          configurers.put("oauth2AuthenticationProvider", oauth2AuthenticationProviderConfigurer);
          return new OAuth2AuthenticationConfigurer();
      }
      

      } ```

      在上述代码中,@EnableOAuth2Sso 注解定义了以下配置:

      1. 注解需要被Spring Boot应用的其他配置类注册到配置中心,例如 OAuth2Config 类。在 OAuth2ConfigImpl 类中,OAuth2SsoService 是一个接口,它包含了 OAuth2 SSO 的服务接口,如 getConsumerKey(), getConsumerSecret(), getAuthorizationUrl(), getAccessTokenUrl(), getAccessTokenSecret(), getRememberMeUrl(), getClientId(), getClientSecret(), getScopes(), getSingleLogoutUrl(), getSingleLogoutSessionTimeout(), getLogoutUrl() 等方法。

      2. OAuth2Config 中,注册了 OAuth2 SSO 的认证器、授权器、资源Owner和资源Owner Password Credentials、RememberMe策略以及一些其他配置项。这些配置项可以用来设置 OAuth2 SSO 的注册URL、授权URL、令牌URL、访问令牌URL、记住我URL、客户端ID、客户端密钥、认证器策略、授权器策略、资源Owner和资源Owner Password Credentials的配置,以及访问令牌和令牌密钥的配置。

      3. 注解的 OAuth2AuthenticationProvider 接口定义了一个 OAuth2 身份验证器,它接受 OAuth2ClientRepositoryOAuth2TokenOAuth2Redirect 类型的 OAuth2ClientOAuth2TokenOAuth2Redirect 对象作为参数,并提供以下方法:

      4. getConsumerKey(): 获取 OAuth2 主密钥。

      5. getConsumerSecret(): 获取 OAuth2 主密钥。
      6. getAuthorizationUrl(): 获取 OAuth2 验证器的授权 URL。
      7. getAccessTokenUrl(): 获取 OAuth2 验证器的令牌 URL。
      8. getAccessTokenSecret(): 获取 OAuth2 验证器的令牌密钥。
      9. getRememberMeUrl(): 获取 OAuth2 记住我的 URL。
      10. getRememberMeUrlParameterName(): 获取 OAuth2 记住我的 URL 参数名称。
      11. getRememberMeUrlParameterType(): 获取 OAuth2 记住我的 URL 参数类型。
      12. getGrantType(): 获取 OAuth2 认证器的默认授权类型。
      13. getAudience(): 获取 OAuth2 认证器的授权范围。
      14. getCallbackUrl(): 获取 OAuth2 认证器的回调 URL。

      15. OAuth2AuthenticationProviderConfigurer 接口定义了 OAuth2 身份验证器的配置项,包括 OAuth2ClientRepositoryOAuth2TokenOAuth2Redirect 类型的 OAuth2ClientOAuth2TokenOAuth2Redirect 对象作为参数,并提供以下方法:

      16. setOAuth2ClientRepository(OAuth2ClientRepository):注册 OAuth2 SSO 的认证器 OAuth2ClientRepository

      17. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2Client
      18. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Token):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2TokenOAuth2Redirect 对象。
      19. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Redirect):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2TokenOAuth2Redirect 对象。
      20. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2SsoClient):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2TokenOAuth2RedirectOAuth2SsoClient
      21. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Token, OAuth2Redirect):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2TokenOAuth2RedirectOAuth2SsoClient
      22. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Token, OAuth2Redirect, OAuth2Redirect):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2TokenOAuth2RedirectOAuth2SsoClient
      23. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client, OAuth2Token, OAuth2Redirect):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2TokenOAuth2RedirectOAuth2SsoClient
      24. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client, OAuth2Token, OAuth2Redirect, OAuth2Redirect):注册 OAuth2 SSO 的认证器 OAuth2ClientRepositoryOAuth2TokenOAuth2RedirectOAuth2SsoClient
      25. setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client, OAuth2Client, OAuth2Token, OAuth2Redirect):注册 OAuth2 SSO 的认证器 `OAuth2Client
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部