44>
55> JustAuth 脚手架
66
7- ![ Maven Central] ( https://img.shields.io/maven-central/v/com.xkcoding/justauth-spring-boot-starter.svg?color=brightgreen&label=Maven%20Central )
8- ![ Travis (.com)] ( https://img.shields.io/travis/com/xkcoding/justauth-spring-boot-starter.svg?label=Build%20Status )
9- ![ GitHub] ( https://img.shields.io/github/license/xkcoding/justauth-spring-boot-starter.svg )
7+ ![ Maven Central] ( https://img.shields.io/maven-central/v/com.xkcoding/justauth-spring-boot-starter.svg?color=brightgreen&label=Maven%20Central ) ![ Travis (.com)] ( https://img.shields.io/travis/com/xkcoding/justauth-spring-boot-starter.svg?label=Build%20Status ) ![ GitHub] ( https://img.shields.io/github/license/xkcoding/justauth-spring-boot-starter.svg )
108
11- ## Demo
9+ ## 1. Demo
1210
1311懒得看文档的,可以直接看demo
1412
1513https://github.com/xkcoding/justauth-spring-boot-starter-demo
1614
1715完整版 demo:https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-social
1816
19- ## 更新日志
17+ ## 2. 更新日志
2018
2119[ CHANGELOG] ( ./CHANGELOG.md )
2220
23- ## 快速开始
21+ ## 3. 快速开始
2422
25- ### 1. 基础配置
23+ ### 3. 1. 基础配置
2624
2725- 引用依赖
2826
@@ -80,13 +78,13 @@ public class TestController {
8078}
8179```
8280
83- ### 2. 缓存配置
81+ ### 3. 2. 缓存配置
8482
8583> starter 内置了2种缓存实现,一种是上面的默认实现,另一种是基于 Redis 的缓存实现。
8684>
8785> 当然了,你也可以自定义实现你自己的缓存。
8886
89- #### 2.1. 默认缓存实现
87+ #### 3. 2.1. 默认缓存实现
9088
9189在配置文件配置如下内容即可
9290
@@ -96,7 +94,7 @@ justauth:
9694 type : default
9795` ` `
9896
99- #### 2.2. Redis 缓存实现
97+ #### 3. 2.2. Redis 缓存实现
10098
101991.添加 Redis 相关依赖
102100
@@ -142,7 +140,7 @@ spring:
142140 min-idle : 0
143141` ` `
144142
145- #### 2.3. 自定义缓存实现
143+ #### 3. 2.3. 自定义缓存实现
146144
1471451.配置文件配置如下内容
148146
@@ -233,36 +231,208 @@ public class AuthStateConfiguration {
233231}
234232` ` `
235233
236- # # 附录
234+ # ## 3.3. 自定义第三方平台配置
237235
238- # ## 1. 基础配置
236+ 1.创建自定义的平台枚举类
239237
240- ` justauth` 配置列表
238+ ` ` ` java
239+ /**
240+ * <p>
241+ * 扩展的自定义 source
242+ * </p>
243+ *
244+ * @author yangkai.shen
245+ * @date Created in 2019/10/9 14:14
246+ */
247+ public enum ExtendSource implements AuthSource {
241248
242- | 属性名 | 类型 | 默认值 | 可选项 | 描述 |
243- | ------------------ | ------------------------------------------------------------ | ------ | ---------- | ----------------- |
244- | `justauth.enabled` | `boolean` | true | true/false | 是否启用 JustAuth |
245- | `justauth.type` | `java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig>` | 无 | | JustAuth 配置 |
246- | `justauth.cache` | `com.xkcoding.justauth.autoconfigure.CacheProperties` | | | JustAuth缓存配置 |
249+ /**
250+ * 测试
251+ */
252+ TEST {
253+ /**
254+ * 授权的api
255+ *
256+ * @return url
257+ */
258+ @Override
259+ public String authorize() {
260+ return "http://authorize";
261+ }
262+
263+ /**
264+ * 获取accessToken的api
265+ *
266+ * @return url
267+ */
268+ @Override
269+ public String accessToken() {
270+ return "http://accessToken";
271+ }
272+
273+ /**
274+ * 获取用户信息的api
275+ *
276+ * @return url
277+ */
278+ @Override
279+ public String userInfo() {
280+ return null;
281+ }
282+
283+ /**
284+ * 取消授权的api
285+ *
286+ * @return url
287+ */
288+ @Override
289+ public String revoke() {
290+ return null;
291+ }
292+
293+ /**
294+ * 刷新授权的api
295+ *
296+ * @return url
297+ */
298+ @Override
299+ public String refresh() {
300+ return null;
301+ }
302+ }
303+ }
304+ ` ` `
247305
248- ` justauth.type` 配置列表
306+ 2.创建自定义的请求处理
307+
308+ ` ` ` java
309+ /**
310+ * <p>
311+ * 测试用自定义扩展的第三方request
312+ * </p>
313+ *
314+ * @author yangkai.shen
315+ * @date Created in 2019/10/9 14:19
316+ */
317+ public class ExtendTestRequest extends AuthDefaultRequest {
318+
319+ public ExtendTestRequest(AuthConfig config) {
320+ super(config, ExtendSource.TEST);
321+ }
322+
323+ public ExtendTestRequest(AuthConfig config, AuthStateCache authStateCache) {
324+ super(config, ExtendSource.TEST, authStateCache);
325+ }
326+
327+ /**
328+ * 获取access token
329+ *
330+ * @param authCallback 授权成功后的回调参数
331+ * @return token
332+ * @see AuthDefaultRequest#authorize()
333+ * @see AuthDefaultRequest#authorize(String)
334+ */
335+ @Override
336+ protected AuthToken getAccessToken(AuthCallback authCallback) {
337+ return AuthToken.builder().openId("openId").expireIn(1000).idToken("idToken").scope("scope").refreshToken("refreshToken").accessToken("accessToken").code("code").build();
338+ }
339+
340+ /**
341+ * 使用token换取用户信息
342+ *
343+ * @param authToken token信息
344+ * @return 用户信息
345+ * @see AuthDefaultRequest#getAccessToken(AuthCallback)
346+ */
347+ @Override
348+ protected AuthUser getUserInfo(AuthToken authToken) {
349+ return AuthUser.builder().username("test").nickname("test").gender(AuthUserGender.MALE).token(authToken).source(this.source.toString()).build();
350+ }
351+
352+ /**
353+ * 撤销授权
354+ *
355+ * @param authToken 登录成功后返回的Token信息
356+ * @return AuthResponse
357+ */
358+ @Override
359+ public AuthResponse revoke(AuthToken authToken) {
360+ return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).msg(AuthResponseStatus.SUCCESS.getMsg()).build();
361+ }
362+
363+ /**
364+ * 刷新access token (续期)
365+ *
366+ * @param authToken 登录成功后返回的Token信息
367+ * @return AuthResponse
368+ */
369+ @Override
370+ public AuthResponse refresh(AuthToken authToken) {
371+ 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();
372+ }
373+ }
374+ ` ` `
375+
376+ 3.在配置文件配置相关信息
377+
378+ ` ` ` yaml
379+ justauth:
380+ enabled: true
381+ extend:
382+ enum-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendSource
383+ config:
384+ TEST:
385+ request-class: com.xkcoding.justauthspringbootstarterdemo.extend.ExtendTestRequest
386+ client-id: xxxxxx
387+ client-secret: xxxxxxxx
388+ redirect-uri: http://oauth.xkcoding.com/demo/oauth/test/callback
389+ ` ` `
390+
391+ # # 4. 附录
392+
393+ # ## 4.1. 基础配置
394+
395+ # ### 4.1.1. `justauth` 配置列表
396+
397+ | 属性名 | 类型 | 默认值 | 可选项 | 描述 |
398+ | ------------------ | ------------------------------------------------------------ | ------ | ---------- | ---------------------- |
399+ | `justauth.enabled` | `boolean` | true | true/false | 是否启用 JustAuth |
400+ | `justauth.type` | `java.util.Map<me.zhyd.oauth.config.AuthSource,me.zhyd.oauth.config.AuthConfig>` | 无 | | JustAuth 配置 |
401+ | `justauth.cache` | `com.xkcoding.justauth.autoconfigure.CacheProperties` | | | JustAuth缓存配置 |
402+ | `justauth.extend` | `com.xkcoding.justauth.autoconfigure.ExtendProperties` | 无 | | JustAuth第三方平台配置 |
403+
404+ # #### 4.1.1.1. `justauth.type` 配置列表
249405
250406| 属性名 | 描述 |
251407| --------------------------- | ------------------------------------------------------------ |
252408| `justauth.type.keys` | `justauth.type` 是 `Map` 格式的,key 的取值请参考 [`AuthSource`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthSource.java) |
253409| `justauth.type.keys.values` | `justauth.type` 是 `Map` 格式的,value 的取值请参考 [`AuthConfig`](https://github.com/zhangyd-c/JustAuth/blob/master/src/main/java/me/zhyd/oauth/config/AuthConfig.java) |
254410
255- ` justauth.cache` 配置列表
411+ # #### 4.1.1.2. `justauth.cache` 配置列表
256412
257413| 属性名 | 类型 | 默认值 | 可选项 | 描述 |
258414| ------------------------ | ------------------------------------------------------------ | ----------------- | -------------------- | ------------------------------------------------------------ |
259415| `justauth.cache.type` | `com.xkcoding.justauth.autoconfigure.CacheProperties.CacheType` | default | default/redis/custom | 缓存类型,default使用JustAuth默认的缓存实现,redis使用默认的redis缓存实现,custom用户自定义缓存实现 |
260- | `justauth.cache.prefix` | `string` | JUSTAUTH::STATE: : | | 缓存前缀,目前只对redis缓存生效,默认 JUSTAUTH::STATE:: |
261- | `justauth.cache.timeout` | `java.time.Duration` | 3分钟 | | 超时时长,目前只对redis缓存生效,默认3分钟 |
416+ | `justauth.cache.prefix` | `java.lang.String` | JUSTAUTH::STATE: : | | 缓存前缀,目前只对redis缓存生效,默认 `JUSTAUTH::STATE::` |
417+ | `justauth.cache.timeout` | `java.time.Duration` | 3分钟 | | 超时时长,目前只对redis缓存生效,默认`3分钟` |
418+
419+ # #### 4.1.1.3.`justauth.extend` 配置列表
420+
421+ | 属性名 | 类型 | 默认值 | 可选项 | 描述 |
422+ | ---------------------------- | -------------------------------------------- | ------ | ------ | ------------ |
423+ | `justauth.extend.enum-class` | `Class<? extends AuthSource>` | 无 | | 枚举类全路径 |
424+ | `justauth.extend.config` | `java.util.Map<String, ExtendRequestConfig>` | 无 | | 对应配置信息 |
425+
426+ # ##### 4.1.1.3.1.`justauth.extend.config` 配置列表
427+
428+ | 属性名 | 类型 | 默认值 | 可选项 | 描述 |
429+ | ------------------------------- | ------------------------------------------------------------ | ------ | ------ | ------------------------------------------------------------ |
430+ | `justauth.extend.config.keys` | `java.lang.String` | 无 | | key 必须在 `justauth.extend.enum-class` 配置的枚举类中声明 |
431+ | `justauth.extend.config.values` | `com.xkcoding.justauth.autoconfigure.ExtendProperties.ExtendRequestConfig` | 无 | | value 就是 `AuthConfig` 的子类,增加了一个 `request-class` 属性配置请求的全类名,具体参考类[`ExtendProperties.ExtendRequestConfig`](https://github.com/justauth/justauth-spring-boot-starter/blob/master/src/main/java/com/xkcoding/justauth/autoconfigure/ExtendProperties.java#L49-L54) |
262432
263- # ## 2. SNAPSHOT版本
433+ # ## 4. 2. SNAPSHOT版本
264434
265- 如果需要体验快照版本,可以在你的 `pom.xml`进行如下配置:
435+  如果需要体验快照版本,可以在你的 `pom.xml`进行如下配置:
266436
267437` ` ` xml
268438<repositories>
0 commit comments