Spring Boot 集成 JustAuth 的最佳实践~
Spring Boot 集成 JustAuth 的最佳实践~
JustAuth 脚手架
懒得看文档的,可以直接看demo
https://github.com/xkcoding/justauth-spring-boot-starter-demo
完整版 demo:https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social
com.xkcoding.justauth
justauth-spring-boot-starter
1.4.0
application.yml 中添加配置配置信息注意:
justauth.type节点的配置,请根据项目实际情况选择,多余的可以删除unionId,则必须传union-id配置,并置为truealipay-public-keystack-overflow-keyagent-idcoding-group-name……starter 内置了2种缓存实现,一种是上面的默认实现,另一种是基于 Redis 的缓存实现。
当然了,你也可以自定义实现你自己的缓存。
在配置文件配置如下内容即可
justauth:
cache:
type: default1.添加 Redis 相关依赖
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
2.配置文件配置如下内容即可
justauth:
cache:
type: redis
# 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE::
prefix: ''
# 超时时长,目前只对redis缓存生效,默认3分钟
timeout: 1h
spring:
redis:
host: localhost
# 连接超时时间(记得添加单位,Duration)
timeout: 10000ms
# Redis默认情况下有16个分片,这里配置具体使用的分片
# database: 0
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制) 默认 8
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-wait: -1ms
# 连接池中的最大空闲连接 默认 8
max-idle: 8
# 连接池中的最小空闲连接 默认 0
min-idle: 01.配置文件配置如下内容
justauth:
cache:
type: custom2.自定义缓存实现 AuthStateCache 接口
…3.自动装配 JustAuthConfig
/**
*
* 自定义缓存装配
*
*
* @author yangkai.shen
* @date Created in 2019/8/31 12:29
*/
@Configuration
public class AuthStateConfiguration {
@Bean
public AuthStateCache authStateCache() {
return new MyAuthStateCache();
}
}1.创建自定义的平台枚举类
…2.创建自定义的请求处理
…3.在配置文件配置相关信息
justauth:
enabled: true
extend:
enum-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendSource
config:
TEST:
request-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendTestRequest
client-id: xxxxxx
client-secret: xxxxxxxx
redirect-uri: http://oauth.xkcoding.com/demo/oauth/test/callback修改配置文件,增加如下配置:
justauth:
http-config:
timeout: 30000
proxy:
GOOGLE:
type: HTTP
hostname: 127.0.0.1
port: 10080注:当项目中使用了自定义的第三方登录,并且需要使用代理时,也要在 http-config 节点下添加相关配置,格式参考上面示例
修改配置文件,增加如下配置:
justauth:
enabled: true
type:
QQ:
client-id: 10**********6
client-secret: 1f7d08**********5b7**********29e
redirect-uri: http://oauth.xkcoding.com/demo/oauth/qq/callback
union-id: false
scopes:
- get_user_info
- xxxx注:你可以前往 me.zhyd.oauth.enums.scope 包下查看各个渠道所支持的 scopes,当然你可以不配置该项,JustAuth 会默认添加上一些基础 scope
justauth 配置列表| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
|---|---|---|---|---|
justauth.enabled |
boolean |
true | true/false | 是否启用 JustAuth |
justauth.type |
java.util.Map |
无 | JustAuth 配置 | |
justauth.httpConfig |
java.util.Map |
无 | http 相关配置 | |
justauth.cache |
com.xkcoding.justauth.autoconfigure.CacheProperties |
JustAuth缓存配置 | ||
justauth.extend |
com.xkcoding.justauth.autoconfigure.ExtendProperties |
无 | JustAuth第三方平台配置 |
justauth.type 配置列表| 属性名 | 描述 |
|---|---|
justauth.type.keys |
justauth.type 是 Map 格式的,key 的取值请参考 AuthDefaultSource |
justauth.type.keys.values |
justauth.type 是 Map 格式的,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.type 是 Map 格式的,key 的取值请参考 AuthDefaultSource |
justauth.httpConfig.keys.values |
justauth.type 是 Map 格式的,value 的取值请参考 JustAuthProperties.JustAuthHttpConfig |
justauth.httpConfig.keys.values 所有可选配置如下:| 属性名 | 描述 | 备注 |
|---|---|---|
timeout |
请求超时时间 | |
proxy |
代理的相关配置,针对国外平台,需要配置代理 | 必填 |
justauth.httpConfig.proxy 所有可选配置如下:| 属性名 | 描述 | 备注 |
|---|---|---|
type |
代理类型,可选值:HTTP、DIRECT、SOCKS,默认为 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 |
无 | 枚举类全路径 | |
justauth.extend.config |
java.util.Map |
无 | 对应配置信息 |
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 |
如果需要体验快照版本,可以在你的 pom.xml进行如下配置:
aliyun
aliyun
http://maven.aliyun.com/nexus/content/groups/public
oss
oss
http://oss.sonatype.org/content/repositories/snapshots
true
true
暂无开放 Issues,或尚未同步最近议题。