4
4
<a href =" http://projects.spring.io/spring-boot/ " ><img src =" https://img.shields.io/badge/SpringBoot-1.5.14-990066.svg " ></a >
5
5
<img src =" https://img.shields.io/badge/Database-MySQL%7CPostgreSQL-brightgreen.svg " >
6
6
<img src =" https://img.shields.io/badge/License-Apache%202.0-blue.svg " >
7
- <img src =" https://img.shields.io/badge/release-1.1.1 -brightgreen.svg " >
7
+ <img src =" https://img.shields.io/badge/release-1.2.0 -brightgreen.svg " >
8
8
9
9
</p >
10
10
11
11
# oauthserver
12
12
## 简介
13
13
oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server微服务。仅仅需要创建相关数据表,修改数据库的连接信息,你就可以得到一个Oauth2 Server微服务。
14
14
15
+ Oauth2 Client通常是要被保护的资源,例如app接口。配套的Oauth2 Client示例可以在这里找到[ https://github.com/jeesun/qbankserver ] ( https://github.com/jeesun/qbankserver ) 。
16
+
15
17
支持的关系型数据库:
16
18
- PostgreSQL
17
19
- MySQL
@@ -20,12 +22,20 @@ oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server
20
22
1 . 集成Spring Boot Oauth2,实现Oauth服务;
21
23
2 . token保存到关系型数据库;
22
24
3 . 获取token时,username允许传用户名、手机号或者邮箱;
23
- 4 . 日志记录保存到文件,并按日归档;
24
- 5 . 数据库连接信息加密;
25
- 6 . 集成Druid数据库连接池;
26
- 7 . 自定义Oauth2Exception异常返回的json信息。
25
+ 4 . token获取支持密码模式和授权码模式;
26
+ 5 . 日志记录保存到文件,并按日归档;
27
+ 6 . 数据库连接信息加密;
28
+ 7 . 集成Druid数据库连接池;
29
+ 8 . 自定义Oauth2Exception异常返回的json信息。
27
30
28
31
## 更新日志
32
+ ### v1.2.0(2018-07-25)
33
+ - 集成Mybatis,以及Mybatis三大插件:通用Mapper,Mybatis-Plus,PageHelper;
34
+ - 集成swagger2,并配置非全局、无需重复输入的header参数(token);
35
+ - authorities表使用user_id代替username字段;
36
+ - 集成Redis缓存;
37
+ - 更棒的接口示例HelloWorldController,强烈建议您阅读该Controller代码。
38
+
29
39
### v1.1.1(2018-07-07)
30
40
- 升级Spring Boot版本从1.5.13.RELEASE到1.5.14.RELEASE;
31
41
- 修复检查的access_token无法识别时,返回中文message。
@@ -45,7 +55,7 @@ oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server
45
55
- MySQL
46
56
请执行` src/main/resources/schema-mysql.sql ` ,完成数据表的创建和测试数据的导入。
47
57
### 2. 修改数据库连接信息
48
- 在application.yml中,配置着数据库的连接信息。其中,配置项username和password是要经过jasypt加密的,不能直接填明文。加密密钥由` jasypt.encryptor.password ` 配置。你需要使用test目录下的UtilTests工具得到加密字符串。
58
+ 在application.yml中,配置着数据库的连接信息。其中,配置项username和password是要经过jasypt加密的,~~ 不能直接填明文~~ ,也可直接填明文 。加密密钥由` jasypt.encryptor.password ` 配置。你需要使用test目录下的UtilTests工具得到加密字符串。
49
59
- PostgreSQL
50
60
```
51
61
# PostgreSQL连接信息
@@ -59,9 +69,9 @@ oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server
59
69
```
60
70
# MySQL连接信息
61
71
driver-class-name: com.mysql.jdbc.Driver
62
- url: jdbc:mysql://127.0.0.1:3306/test ?useUnicode=true&characterEncoding=utf-8&useSSL=false
72
+ url: jdbc:mysql://127.0.0.1:3306/thymelte ?useUnicode=true&characterEncoding=utf-8&useSSL=false
63
73
username: ENC(YiYjVwTulDGN//YaB3KbuA==) #明文root
64
- password: ENC(9oaIJkFgGSDFaHH3OXY63RHWQ+amDmiJ ) #明文19941017
74
+ password: ENC(BZUIhx1tIC75T4JTYROwbsPIRuhrPQMx ) #明文19941017
65
75
```
66
76
### 3. 运行
67
77
现在,一切已准备就绪。运行项目,当程序成功启动时,即表明你已配置成功。
@@ -176,6 +186,29 @@ status=401,返回的json数据:
176
186
"data" : null
177
187
}
178
188
```
189
+
190
+ > 4、根据授权码获取token
191
+ >> POST [ http://localhost:8182/oauth/authorize?response_type=code&client_id=clientIdPassword&scope=read&redirect_uri=http://www.baidu.com ] ( http://localhost:8182/oauth/authorize?response_type=code&client_id=clientIdPassword&scope=read&redirect_uri=http://www.baidu.com )
192
+ ![ 登录] ( screenshots/login.png )
193
+ ![ 授权] ( screenshots/approve.png )
194
+ 同意授权,跳转到` https://www.baidu.com/?code=jgA1h3 ` ,` jgA1h3 ` 就是授权码。
195
+ 使用授权码获取token:
196
+ >> POST [ http://localhost:8182/oauth/token?grant_type=authorization_code&code=jgA1h3&redirect_uri=http://www.baidu.com ] ( http://localhost:8182/oauth/token?grant_type=authorization_code&code=jgA1h3&redirect_uri=http://www.baidu.com )
197
+ <br />
198
+ ** 成功示例**
199
+ status=200,返回的json数据:
200
+ ``` json
201
+ {
202
+ "access_token" : " ca582cd1-be6c-4a5a-82ec-10af7a8e06eb" ,
203
+ "token_type" : " bearer" ,
204
+ "refresh_token" : " c24a6143-97c8-4642-88b9-d5c5b902b487" ,
205
+ "expires_in" : 3824 ,
206
+ "scope" : " read write trust"
207
+ }
208
+ ```
209
+ ### 5. 关于国际化
210
+ 接口参数添加locale,值为语言区域,例如zh_CN,zh_CH,en_US。
211
+
179
212
## app实践指南
180
213
app获取到token信息后,需要保存token信息和请求时间。在传access_token之前,需要检查access_token是否过期。为了减少后台压力,检查access_token是否过期应该是在app本地完成。通过token的key` expires_in ` (剩余有效期)的值,以及本地记录的请求时间,和当前时间做对比,可以很方便地判断出access_token是否过期。如果过期了,需要通过refresh_token获取新的access_token。因为access_token的有效期只有2个小时,这个验证是必须的。
181
214
refresh_token同理。
0 commit comments