- 55
- 0
我已经用了@EnableResourceServer EnableAuthorizationServer
问下@EnableOAuth2Sso @EnableOAuth2Client有啥用?
- 共 0 条
- 全部回答
-
罙噯伱の癡情患者° 普通会员 1楼
在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 注解定义了以下配置:
-
注解需要被Spring Boot应用的其他配置类注册到配置中心,例如
OAuth2Config类。在OAuth2ConfigImpl类中,OAuth2SsoService是一个接口,它包含了 OAuth2 SSO 的服务接口,如getConsumerKey(),getConsumerSecret(),getAuthorizationUrl(),getAccessTokenUrl(),getAccessTokenSecret(),getRememberMeUrl(),getClientId(),getClientSecret(),getScopes(),getSingleLogoutUrl(),getSingleLogoutSessionTimeout(),getLogoutUrl()等方法。 -
在
OAuth2Config中,注册了 OAuth2 SSO 的认证器、授权器、资源Owner和资源Owner Password Credentials、RememberMe策略以及一些其他配置项。这些配置项可以用来设置 OAuth2 SSO 的注册URL、授权URL、令牌URL、访问令牌URL、记住我URL、客户端ID、客户端密钥、认证器策略、授权器策略、资源Owner和资源Owner Password Credentials的配置,以及访问令牌和令牌密钥的配置。 -
注解的
OAuth2AuthenticationProvider接口定义了一个 OAuth2 身份验证器,它接受OAuth2ClientRepository、OAuth2Token和OAuth2Redirect类型的OAuth2Client、OAuth2Token和OAuth2Redirect对象作为参数,并提供以下方法: -
getConsumerKey(): 获取 OAuth2 主密钥。 getConsumerSecret(): 获取 OAuth2 主密钥。getAuthorizationUrl(): 获取 OAuth2 验证器的授权 URL。getAccessTokenUrl(): 获取 OAuth2 验证器的令牌 URL。getAccessTokenSecret(): 获取 OAuth2 验证器的令牌密钥。getRememberMeUrl(): 获取 OAuth2 记住我的 URL。getRememberMeUrlParameterName(): 获取 OAuth2 记住我的 URL 参数名称。getRememberMeUrlParameterType(): 获取 OAuth2 记住我的 URL 参数类型。getGrantType(): 获取 OAuth2 认证器的默认授权类型。getAudience(): 获取 OAuth2 认证器的授权范围。-
getCallbackUrl(): 获取 OAuth2 认证器的回调 URL。 -
OAuth2AuthenticationProviderConfigurer接口定义了 OAuth2 身份验证器的配置项,包括OAuth2ClientRepository、OAuth2Token和OAuth2Redirect类型的OAuth2Client、OAuth2Token和OAuth2Redirect对象作为参数,并提供以下方法: -
setOAuth2ClientRepository(OAuth2ClientRepository):注册 OAuth2 SSO 的认证器OAuth2ClientRepository。 setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client):注册 OAuth2 SSO 的认证器OAuth2ClientRepository和OAuth2Client。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Token):注册 OAuth2 SSO 的认证器OAuth2ClientRepository、OAuth2Token和OAuth2Redirect对象。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Redirect):注册 OAuth2 SSO 的认证器OAuth2ClientRepository、OAuth2Token和OAuth2Redirect对象。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2SsoClient):注册 OAuth2 SSO 的认证器OAuth2ClientRepository、OAuth2Token、OAuth2Redirect和OAuth2SsoClient。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Token, OAuth2Redirect):注册 OAuth2 SSO 的认证器OAuth2ClientRepository、OAuth2Token、OAuth2Redirect和OAuth2SsoClient。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Token, OAuth2Redirect, OAuth2Redirect):注册 OAuth2 SSO 的认证器OAuth2ClientRepository、OAuth2Token、OAuth2Redirect和OAuth2SsoClient。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client, OAuth2Token, OAuth2Redirect):注册 OAuth2 SSO 的认证器OAuth2ClientRepository、OAuth2Token、OAuth2Redirect和OAuth2SsoClient。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client, OAuth2Token, OAuth2Redirect, OAuth2Redirect):注册 OAuth2 SSO 的认证器OAuth2ClientRepository、OAuth2Token、OAuth2Redirect和OAuth2SsoClient。setOAuth2ClientRepository(OAuth2ClientRepository, OAuth2Client, OAuth2Client, OAuth2Token, OAuth2Redirect):注册 OAuth2 SSO 的认证器 `OAuth2Client
-
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
