项目作者: justauth

项目描述 :
Spring Boot 集成 JustAuth 的最佳实践~
高级语言: Java
项目地址: git://github.com/justauth/justauth-spring-boot-starter.git
创建时间: 2019-07-22T06:54:45Z
项目社区:https://github.com/justauth/justauth-spring-boot-starter

开源协议:GNU Lesser General Public License v3.0

下载


justauth-spring-boot-starter

Spring Boot 集成 JustAuth 的最佳实践~

JustAuth 脚手架

Maven CentralTravis (.com)GitHub

1. Demo

懒得看文档的,可以直接看demo

https://github.com/xkcoding/justauth-spring-boot-starter-demo

完整版 demo:https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social

2. 更新日志

CHANGELOG

3. 快速开始

3.1. 基础配置

  • 引用依赖
  1. <dependency>
  2. <groupId>com.xkcoding.justauth</groupId>
  3. <artifactId>justauth-spring-boot-starter</artifactId>
  4. <version>1.4.0</version>
  5. </dependency>
  • 添加配置,在 application.yml 中添加配置配置信息

注意:

  • justauth.type节点的配置,请根据项目实际情况选择,多余的可以删除
  • 如果使用 QQ 登录,并且需要获取unionId,则必须传union-id配置,并置为true
  • 如果使用支付宝登录,必传alipay-public-key
  • 如果使用 Stack Overflow 登录,必传stack-overflow-key
  • 如果使用企业微信登录,必传agent-id
  • 如果使用 CODING 登录,必传coding-group-name
  1. justauth:
  2. enabled: true
  3. type:
  4. QQ:
  5. client-id: 10**********6
  6. client-secret: 1f7d08**********5b7**********29e
  7. redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
  8. union-id: false
  9. WEIBO:
  10. client-id: 10**********6
  11. client-secret: 1f7d08**********5b7**********29e
  12. redirect-uri: http://oauth.xkcoding.com/demo/oauth/weibo/callback
  13. GITEE:
  14. client-id: 10**********6
  15. client-secret: 1f7d08**********5b7**********29e
  16. redirect-uri: http://oauth.xkcoding.com/demo/oauth/gitee/callback
  17. DINGTALK:
  18. client-id: 10**********6
  19. client-secret: 1f7d08**********5b7**********29e
  20. redirect-uri: http://oauth.xkcoding.com/demo/oauth/dingtalk/callback
  21. BAIDU:
  22. client-id: 10**********6
  23. client-secret: 1f7d08**********5b7**********29e
  24. redirect-uri: http://oauth.xkcoding.com/demo/oauth/baidu/callback
  25. CSDN:
  26. client-id: 10**********6
  27. client-secret: 1f7d08**********5b7**********29e
  28. redirect-uri: http://oauth.xkcoding.com/demo/oauth/csdn/callback
  29. CODING:
  30. client-id: 10**********6
  31. client-secret: 1f7d08**********5b7**********29e
  32. redirect-uri: http://oauth.xkcoding.com/demo/oauth/coding/callback
  33. coding-group-name: xx
  34. OSCHINA:
  35. client-id: 10**********6
  36. client-secret: 1f7d08**********5b7**********29e
  37. redirect-uri: http://oauth.xkcoding.com/demo/oauth/oschina/callback
  38. ALIPAY:
  39. client-id: 10**********6
  40. client-secret: 1f7d08**********5b7**********29e
  41. redirect-uri: http://oauth.xkcoding.com/demo/oauth/alipay/callback
  42. alipay-public-key: MIIB**************DAQAB
  43. WECHAT_OPEN:
  44. client-id: 10**********6
  45. client-secret: 1f7d08**********5b7**********29e
  46. redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_open/callback
  47. WECHAT_MP:
  48. client-id: 10**********6
  49. client-secret: 1f7d08**********5b7**********29e
  50. redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_mp/callback
  51. WECHAT_ENTERPRISE:
  52. client-id: 10**********6
  53. client-secret: 1f7d08**********5b7**********29e
  54. redirect-uri: http://oauth.xkcoding.com/demo/oauth/wechat_enterprise/callback
  55. agent-id: 1000002
  56. TAOBAO:
  57. client-id: 10**********6
  58. client-secret: 1f7d08**********5b7**********29e
  59. redirect-uri: http://oauth.xkcoding.com/demo/oauth/taobao/callback
  60. GOOGLE:
  61. client-id: 10**********6
  62. client-secret: 1f7d08**********5b7**********29e
  63. redirect-uri: http://oauth.xkcoding.com/demo/oauth/google/callback
  64. FACEBOOK:
  65. client-id: 10**********6
  66. client-secret: 1f7d08**********5b7**********29e
  67. redirect-uri: http://oauth.xkcoding.com/demo/oauth/facebook/callback
  68. DOUYIN:
  69. client-id: 10**********6
  70. client-secret: 1f7d08**********5b7**********29e
  71. redirect-uri: http://oauth.xkcoding.com/demo/oauth/douyin/callback
  72. LINKEDIN:
  73. client-id: 10**********6
  74. client-secret: 1f7d08**********5b7**********29e
  75. redirect-uri: http://oauth.xkcoding.com/demo/oauth/linkedin/callback
  76. MICROSOFT:
  77. client-id: 10**********6
  78. client-secret: 1f7d08**********5b7**********29e
  79. redirect-uri: http://oauth.xkcoding.com/demo/oauth/microsoft/callback
  80. MI:
  81. client-id: 10**********6
  82. client-secret: 1f7d08**********5b7**********29e
  83. redirect-uri: http://oauth.xkcoding.com/demo/oauth/mi/callback
  84. TOUTIAO:
  85. client-id: 10**********6
  86. client-secret: 1f7d08**********5b7**********29e
  87. redirect-uri: http://oauth.xkcoding.com/demo/oauth/toutiao/callback
  88. TEAMBITION:
  89. client-id: 10**********6
  90. client-secret: 1f7d08**********5b7**********29e
  91. redirect-uri: http://oauth.xkcoding.com/demo/oauth/teambition/callback
  92. RENREN:
  93. client-id: 10**********6
  94. client-secret: 1f7d08**********5b7**********29e
  95. redirect-uri: http://oauth.xkcoding.com/demo/oauth/renren/callback
  96. PINTEREST:
  97. client-id: 10**********6
  98. client-secret: 1f7d08**********5b7**********29e
  99. redirect-uri: http://oauth.xkcoding.com/demo/oauth/pinterest/callback
  100. STACK_OVERFLOW:
  101. client-id: 10**********6
  102. client-secret: 1f7d08**********5b7**********29e
  103. redirect-uri: http://oauth.xkcoding.com/demo/oauth/stack_overflow/callback
  104. stack-overflow-key: asd*********asd
  105. HUAWEI:
  106. client-id: 10**********6
  107. client-secret: 1f7d08**********5b7**********29e
  108. redirect-uri: http://oauth.xkcoding.com/demo/oauth/huawei/callback
  109. KUJIALE:
  110. client-id: 10**********6
  111. client-secret: 1f7d08**********5b7**********29e
  112. redirect-uri: http://oauth.xkcoding.com/demo/oauth/kujiale/callback
  113. GITLAB:
  114. client-id: 10**********6
  115. client-secret: 1f7d08**********5b7**********29e
  116. redirect-uri: http://oauth.xkcoding.com/demo/oauth/gitlab/callback
  117. MEITUAN:
  118. client-id: 10**********6
  119. client-secret: 1f7d08**********5b7**********29e
  120. redirect-uri: http://oauth.xkcoding.com/demo/oauth/meituan/callback
  121. ELEME:
  122. client-id: 10**********6
  123. client-secret: 1f7d08**********5b7**********29e
  124. redirect-uri: http://oauth.xkcoding.com/demo/oauth/eleme/callback
  125. TWITTER:
  126. client-id: 10**********6
  127. client-secret: 1f7d08**********5b7**********29e
  128. redirect-uri: http://oauth.xkcoding.com/demo/oauth/twitter/callback
  129. XMLY:
  130. client-id: 10**********6
  131. client-secret: 1f7d08**********5b7**********29e
  132. redirect-uri: http://oauth.xkcoding.com/demo/oauth/xmly/callback
  133. # 设备唯一标识ID
  134. device-id: xxxxxxxxxxxxxx
  135. # 客户端操作系统类型,1-iOS系统,2-Android系统,3-Web
  136. client-os-type: 3
  137. # 客户端包名,如果 clientOsType 为1或2时必填。对Android客户端是包名,对IOS客户端是Bundle ID
  138. #pack-id: xxxx
  139. FEISHU:
  140. client-id: 10**********6
  141. client-secret: 1f7d08**********5b7**********29e
  142. redirect-uri: http://oauth.xkcoding.com/demo/oauth/feishu/callback
  143. JD:
  144. client-id: 10**********6
  145. client-secret: 1f7d08**********5b7**********29e
  146. redirect-uri: http://oauth.xkcoding.com/demo/oauth/jd/callback
  147. cache:
  148. type: default
  • 然后就开始玩耍吧~
  1. @Slf4j
  2. @RestController
  3. @RequestMapping("/oauth")
  4. @RequiredArgsConstructor(onConstructor_ = @Autowired)
  5. public class TestController {
  6. private final AuthRequestFactory factory;
  7. @GetMapping
  8. public List<String> list() {
  9. return factory.oauthList();
  10. }
  11. @GetMapping("/login/{type}")
  12. public void login(@PathVariable String type, HttpServletResponse response) throws IOException {
  13. AuthRequest authRequest = factory.get(type);
  14. response.sendRedirect(authRequest.authorize(AuthStateUtils.createState()));
  15. }
  16. @RequestMapping("/{type}/callback")
  17. public AuthResponse login(@PathVariable String type, AuthCallback callback) {
  18. AuthRequest authRequest = factory.get(type);
  19. AuthResponse response = authRequest.login(callback);
  20. log.info("【response】= {}", JSONUtil.toJsonStr(response));
  21. return response;
  22. }
  23. }

3.2. 缓存配置

starter 内置了2种缓存实现,一种是上面的默认实现,另一种是基于 Redis 的缓存实现。

当然了,你也可以自定义实现你自己的缓存。

3.2.1. 默认缓存实现

在配置文件配置如下内容即可

  1. justauth:
  2. cache:
  3. type: default

3.2.2. Redis 缓存实现

1.添加 Redis 相关依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <!-- 对象池,使用redis时必须引入 -->
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-pool2</artifactId>
  9. </dependency>

2.配置文件配置如下内容即可

  1. justauth:
  2. cache:
  3. type: redis
  4. # 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE::
  5. prefix: ''
  6. # 超时时长,目前只对redis缓存生效,默认3分钟
  7. timeout: 1h
  8. spring:
  9. redis:
  10. host: localhost
  11. # 连接超时时间(记得添加单位,Duration)
  12. timeout: 10000ms
  13. # Redis默认情况下有16个分片,这里配置具体使用的分片
  14. # database: 0
  15. lettuce:
  16. pool:
  17. # 连接池最大连接数(使用负值表示没有限制) 默认 8
  18. max-active: 8
  19. # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
  20. max-wait: -1ms
  21. # 连接池中的最大空闲连接 默认 8
  22. max-idle: 8
  23. # 连接池中的最小空闲连接 默认 0
  24. min-idle: 0

3.2.3. 自定义缓存实现

1.配置文件配置如下内容

  1. justauth:
  2. cache:
  3. type: custom

2.自定义缓存实现 AuthStateCache 接口

  1. /**
  2. * <p>
  3. * 自定义缓存实现
  4. * </p>
  5. *
  6. * @author yangkai.shen
  7. * @date Created in 2019/8/31 12:53
  8. */
  9. public class MyAuthStateCache implements AuthStateCache {
  10. /**
  11. * 存入缓存
  12. *
  13. * @param key 缓存key
  14. * @param value 缓存内容
  15. */
  16. @Override
  17. public void cache(String key, String value) {
  18. // TODO: 自定义存入缓存
  19. }
  20. /**
  21. * 存入缓存
  22. *
  23. * @param key 缓存key
  24. * @param value 缓存内容
  25. * @param timeout 指定缓存过期时间(毫秒)
  26. */
  27. @Override
  28. public void cache(String key, String value, long timeout) {
  29. // TODO: 自定义存入缓存
  30. }
  31. /**
  32. * 获取缓存内容
  33. *
  34. * @param key 缓存key
  35. * @return 缓存内容
  36. */
  37. @Override
  38. public String get(String key) {
  39. // TODO: 自定义获取缓存内容
  40. return null;
  41. }
  42. /**
  43. * 是否存在key,如果对应key的value值已过期,也返回false
  44. *
  45. * @param key 缓存key
  46. * @return true:存在key,并且value没过期;false:key不存在或者已过期
  47. */
  48. @Override
  49. public boolean containsKey(String key) {
  50. // TODO: 自定义判断key是否存在
  51. return false;
  52. }
  53. }

3.自动装配 JustAuthConfig

  1. /**
  2. * <p>
  3. * 自定义缓存装配
  4. * </p>
  5. *
  6. * @author yangkai.shen
  7. * @date Created in 2019/8/31 12:29
  8. */
  9. @Configuration
  10. public class AuthStateConfiguration {
  11. @Bean
  12. public AuthStateCache authStateCache() {
  13. return new MyAuthStateCache();
  14. }
  15. }

3.3. 自定义第三方平台配置

1.创建自定义的平台枚举类

  1. /**
  2. * <p>
  3. * 扩展的自定义 source
  4. * </p>
  5. *
  6. * @author yangkai.shen
  7. * @date Created in 2019/10/9 14:14
  8. */
  9. public enum ExtendSource implements AuthSource {
  10. /**
  11. * 测试
  12. */
  13. TEST {
  14. /**
  15. * 授权的api
  16. *
  17. * @return url
  18. */
  19. @Override
  20. public String authorize() {
  21. return "http://authorize";
  22. }
  23. /**
  24. * 获取accessToken的api
  25. *
  26. * @return url
  27. */
  28. @Override
  29. public String accessToken() {
  30. return "http://accessToken";
  31. }
  32. /**
  33. * 获取用户信息的api
  34. *
  35. * @return url
  36. */
  37. @Override
  38. public String userInfo() {
  39. return null;
  40. }
  41. /**
  42. * 取消授权的api
  43. *
  44. * @return url
  45. */
  46. @Override
  47. public String revoke() {
  48. return null;
  49. }
  50. /**
  51. * 刷新授权的api
  52. *
  53. * @return url
  54. */
  55. @Override
  56. public String refresh() {
  57. return null;
  58. }
  59. }
  60. }

2.创建自定义的请求处理

  1. /**
  2. * <p>
  3. * 测试用自定义扩展的第三方request
  4. * </p>
  5. *
  6. * @author yangkai.shen
  7. * @date Created in 2019/10/9 14:19
  8. */
  9. public class ExtendTestRequest extends AuthDefaultRequest {
  10. public ExtendTestRequest(AuthConfig config) {
  11. super(config, ExtendSource.TEST);
  12. }
  13. public ExtendTestRequest(AuthConfig config, AuthStateCache authStateCache) {
  14. super(config, ExtendSource.TEST, authStateCache);
  15. }
  16. /**
  17. * 获取access token
  18. *
  19. * @param authCallback 授权成功后的回调参数
  20. * @return token
  21. * @see AuthDefaultRequest#authorize()
  22. * @see AuthDefaultRequest#authorize(String)
  23. */
  24. @Override
  25. protected AuthToken getAccessToken(AuthCallback authCallback) {
  26. return AuthToken.builder().openId("openId").expireIn(1000).idToken("idToken").scope("scope").refreshToken("refreshToken").accessToken("accessToken").code("code").build();
  27. }
  28. /**
  29. * 使用token换取用户信息
  30. *
  31. * @param authToken token信息
  32. * @return 用户信息
  33. * @see AuthDefaultRequest#getAccessToken(AuthCallback)
  34. */
  35. @Override
  36. protected AuthUser getUserInfo(AuthToken authToken) {
  37. return AuthUser.builder().username("test").nickname("test").gender(AuthUserGender.MALE).token(authToken).source(this.source.toString()).build();
  38. }
  39. /**
  40. * 撤销授权
  41. *
  42. * @param authToken 登录成功后返回的Token信息
  43. * @return AuthResponse
  44. */
  45. @Override
  46. public AuthResponse revoke(AuthToken authToken) {
  47. return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).msg(AuthResponseStatus.SUCCESS.getMsg()).build();
  48. }
  49. /**
  50. * 刷新access token (续期)
  51. *
  52. * @param authToken 登录成功后返回的Token信息
  53. * @return AuthResponse
  54. */
  55. @Override
  56. public AuthResponse refresh(AuthToken authToken) {
  57. return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(AuthToken.builder().openId("openId").expireIn(1000).idToken("idToken").scope("scope").refreshToken("refreshToken").accessToken("accessToken").code("code").build()).build();
  58. }
  59. }

3.在配置文件配置相关信息

  1. justauth:
  2. enabled: true
  3. extend:
  4. enum-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendSource
  5. config:
  6. TEST:
  7. request-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendTestRequest
  8. client-id: xxxxxx
  9. client-secret: xxxxxxxx
  10. redirect-uri: http://oauth.xkcoding.com/demo/oauth/test/callback

4. http 代理配置

修改配置文件,增加如下配置:

  1. justauth:
  2. http-config:
  3. timeout: 30000
  4. proxy:
  5. GOOGLE:
  6. type: HTTP
  7. hostname: 127.0.0.1
  8. port: 10080

注:当项目中使用了自定义的第三方登录,并且需要使用代理时,也要在 http-config 节点下添加相关配置,格式参考上面示例

5. 自定义 Scopes

修改配置文件,增加如下配置:

  1. justauth:
  2. enabled: true
  3. type:
  4. QQ:
  5. client-id: 10**********6
  6. client-secret: 1f7d08**********5b7**********29e
  7. redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
  8. union-id: false
  9. scopes:
  10. - get_user_info
  11. - xxxx

注:你可以前往 me.zhyd.oauth.enums.scope 包下查看各个渠道所支持的 scopes,当然你可以不配置该项,JustAuth 会默认添加上一些基础 scope

6. 附录

6.1. justauth 配置列表

属性名 类型 默认值 可选项 描述
justauth.enabled boolean true true/false 是否启用 JustAuth
justauth.type java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig> JustAuth 配置
justauth.httpConfig java.util.Map<me.zhyd.oauth.config.AuthSource,com.xkcoding.justauth.autoconfigure.JustAuthProperties.JustAuthHttpConfig> http 相关配置
justauth.cache com.xkcoding.justauth.autoconfigure.CacheProperties JustAuth缓存配置
justauth.extend com.xkcoding.justauth.autoconfigure.ExtendProperties JustAuth第三方平台配置

justauth.type 配置列表

属性名 描述
justauth.type.keys justauth.typeMap 格式的,key 的取值请参考 AuthDefaultSource
justauth.type.keys.values justauth.typeMap 格式的,value 的取值请参考 AuthConfig
justauth.type.keys.values 所有可选配置如下:
属性名 描述 备注
client-id 客户端id,对应各平台的appKey 必填
client-secret 客户端Secret,对应各平台的appSecret 必填
redirect-uri 登录成功后的回调地址 必填
alipay-public-key 支付宝公钥 当使用支付宝登录时, 该值必填,对应“RSA2(SHA256)密钥”中的“支付宝公钥”
union-id 是否需要申请unionid 当使用QQ登录时,该值选填,如果置为true则qq开发者应用必须具备相应权限,参考链接:查看详情
stack-overflow-key Stack Overflow Key 当使用Stack Overflow登录时, 该值必填
agent-id 企业微信,授权方的网页应用ID 当使用企业微信登录时, 该值必填
coding-group-name 团队域名前缀 使用 Coding 登录时, 该值必填

justauth.httpConfig 配置列表

属性名 描述
justauth.httpConfig.keys justauth.typeMap 格式的,key 的取值请参考 AuthDefaultSource
justauth.httpConfig.keys.values justauth.typeMap 格式的,value 的取值请参考 JustAuthProperties.JustAuthHttpConfig
justauth.httpConfig.keys.values 所有可选配置如下:
属性名 描述 备注
timeout 请求超时时间
proxy 代理的相关配置,针对国外平台,需要配置代理 必填
justauth.httpConfig.proxy 所有可选配置如下:
属性名 描述 备注
type 代理类型,可选值:HTTPDIRECTSOCKS,默认为 HTTP
hostname 代理 IP 地址
port 代理端口

justauth.cache 配置列表

属性名 类型 默认值 可选项 描述
justauth.cache.type com.xkcoding.justauth.autoconfigure.CacheProperties.CacheType default default/redis/custom 缓存类型,default使用JustAuth默认的缓存实现,redis使用默认的redis缓存实现,custom用户自定义缓存实现
justauth.cache.prefix java.lang.String JUSTAUTH::STATE:: 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE::
justauth.cache.timeout java.time.Duration 3分钟 超时时长,目前只对redis缓存生效,默认3分钟

justauth.extend 配置列表

属性名 类型 默认值 可选项 描述
justauth.extend.enum-class Class<? extends AuthSource> 枚举类全路径
justauth.extend.config java.util.Map<String, ExtendRequestConfig> 对应配置信息
justauth.extend.config 配置列表
属性名 类型 默认值 可选项 描述
justauth.extend.config.keys java.lang.String key 必须在 justauth.extend.enum-class 配置的枚举类中声明
justauth.extend.config.values com.xkcoding.justauth.autoconfigure.ExtendProperties.ExtendRequestConfig value 就是 AuthConfig 的子类,增加了一个 request-class 属性配置请求的全类名,具体参考类ExtendProperties.ExtendRequestConfig

6.2. SNAPSHOT版本

https://img.shields.io/badge/snapshots-1.4.0--SNAPSHOT-green如果需要体验快照版本,可以在你的 pom.xml进行如下配置:

  1. <repositories>
  2. <!--阿里云私服-->
  3. <repository>
  4. <id>aliyun</id>
  5. <name>aliyun</name>
  6. <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  7. </repository>
  8. <!--中央仓库-->
  9. <repository>
  10. <id>oss</id>
  11. <name>oss</name>
  12. <url>http://oss.sonatype.org/content/repositories/snapshots</url>
  13. <releases>
  14. <enabled>true</enabled>
  15. </releases>
  16. <snapshots>
  17. <enabled>true</enabled>
  18. </snapshots>
  19. </repository>
  20. </repositories>