Skip to content

Commit 865cc5e

Browse files
committed
oauth_client_details表的autoapprove字段设为'false';更新README.md。
1 parent 2fd2da4 commit 865cc5e

File tree

6 files changed

+45
-12
lines changed

6 files changed

+45
-12
lines changed

README.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
<a href="http://projects.spring.io/spring-boot/"><img src="https://img.shields.io/badge/SpringBoot-1.5.14-990066.svg"></a>
55
<img src="https://img.shields.io/badge/Database-MySQL%7CPostgreSQL-brightgreen.svg">
66
<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">
88

99
</p>
1010

1111
# oauthserver
1212
## 简介
1313
oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server微服务。仅仅需要创建相关数据表,修改数据库的连接信息,你就可以得到一个Oauth2 Server微服务。
1414

15+
Oauth2 Client通常是要被保护的资源,例如app接口。配套的Oauth2 Client示例可以在这里找到[https://github.com/jeesun/qbankserver](https://github.com/jeesun/qbankserver)
16+
1517
支持的关系型数据库:
1618
- PostgreSQL
1719
- MySQL
@@ -20,12 +22,20 @@ oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server
2022
1. 集成Spring Boot Oauth2,实现Oauth服务;
2123
2. token保存到关系型数据库;
2224
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信息。
2730

2831
## 更新日志
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+
2939
### v1.1.1(2018-07-07)
3040
- 升级Spring Boot版本从1.5.13.RELEASE到1.5.14.RELEASE;
3141
- 修复检查的access_token无法识别时,返回中文message。
@@ -45,7 +55,7 @@ oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server
4555
- MySQL
4656
请执行`src/main/resources/schema-mysql.sql`,完成数据表的创建和测试数据的导入。
4757
### 2. 修改数据库连接信息
48-
在application.yml中,配置着数据库的连接信息。其中,配置项username和password是要经过jasypt加密的,不能直接填明文。加密密钥由`jasypt.encryptor.password`配置。你需要使用test目录下的UtilTests工具得到加密字符串。
58+
在application.yml中,配置着数据库的连接信息。其中,配置项username和password是要经过jasypt加密的,~~不能直接填明文~~,也可直接填明文。加密密钥由`jasypt.encryptor.password`配置。你需要使用test目录下的UtilTests工具得到加密字符串。
4959
- PostgreSQL
5060
```
5161
# PostgreSQL连接信息
@@ -59,9 +69,9 @@ oauthserver是一个基于Spring Boot Oauth2的完整的独立的Oauth2 Server
5969
```
6070
# MySQL连接信息
6171
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
6373
username: ENC(YiYjVwTulDGN//YaB3KbuA==) #明文root
64-
password: ENC(9oaIJkFgGSDFaHH3OXY63RHWQ+amDmiJ) #明文19941017
74+
password: ENC(BZUIhx1tIC75T4JTYROwbsPIRuhrPQMx) #明文19941017
6575
```
6676
### 3. 运行
6777
现在,一切已准备就绪。运行项目,当程序成功启动时,即表明你已配置成功。
@@ -176,6 +186,29 @@ status=401,返回的json数据:
176186
"data": null
177187
}
178188
```
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+
179212
## app实践指南
180213
app获取到token信息后,需要保存token信息和请求时间。在传access_token之前,需要检查access_token是否过期。为了减少后台压力,检查access_token是否过期应该是在app本地完成。通过token的key`expires_in`(剩余有效期)的值,以及本地记录的请求时间,和当前时间做对比,可以很方便地判断出access_token是否过期。如果过期了,需要通过refresh_token获取新的access_token。因为access_token的有效期只有2个小时,这个验证是必须的。
181214
refresh_token同理。

screenshots/approve.png

9.42 KB
Loading

screenshots/login.png

6.11 KB
Loading

src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ spring:
1616
datasource:
1717
# PostgreSQL连接信息
1818
driver-class-name: org.postgresql.Driver
19-
url: jdbc:postgresql://127.0.0.1:5432/thymeltetest?useUnicode=true&amp;characterEncoding=UTF-8
19+
url: jdbc:postgresql://127.0.0.1:5432/thymelte?useUnicode=true&amp;characterEncoding=UTF-8
2020
username: ENC(hTpbG9fq+7P3SntmXuNtDxbtWDqRuPV+) #明文postgres
2121
password: ENC(abdq6LyOspryFQHCqzEMTxRozyJVjIA4) #明文19961120
2222
# MySQL连接信息

src/main/resources/schema-mysql.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ create table if NOT EXISTS oauth_client_details (
1010
access_token_validity INTEGER,
1111
refresh_token_validity INTEGER,
1212
additional_information VARCHAR(4096),
13-
autoapprove tinyint
13+
autoapprove VARCHAR(255)
1414
);
1515

1616
create table if NOT EXISTS oauth_client_token (
@@ -132,7 +132,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS ix_auth_username ON authorities (user_id, auth
132132
ALTER TABLE authorities ADD FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION;
133133

134134
-- 密码经过了加密,是secret
135-
INSERT INTO oauth_client_details VALUES ('clientIdPassword', 'oauth2-resource', '$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6', 'read,write,trust', 'password,authorization_code,refresh_token', null, 'ROLE_ADMIN,ROLE_USER', 7200, 5184000, null, null);
135+
INSERT INTO oauth_client_details VALUES ('clientIdPassword', 'oauth2-resource', '$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6', 'read,write,trust', 'password,authorization_code,refresh_token', null, 'ROLE_ADMIN,ROLE_USER', 7200, 5184000, null, 'false');
136136

137137
-- 密码经过了加密,全都是1234567890c
138138
INSERT INTO users (id, username, password, enabled, email, phone) VALUES (1, 'jeesun', '$2a$11$t4akVchfgOv00XxB/ZKLlOmweUoL/Aed4CiJqQjaiRLZpBU3AWfxu', true, '[email protected]', '18362102427');

src/main/resources/scheme-pg.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ CREATE UNIQUE INDEX if NOT EXISTS ix_auth_username ON authorities (user_id, auth
159159
ALTER TABLE authorities ADD FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION;
160160

161161
-- 密码经过了加密,是secret
162-
INSERT INTO oauth_client_details VALUES ('clientIdPassword', 'oauth2-resource', '$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6', 'read,write,trust', 'password,authorization_code,refresh_token', null, 'ROLE_ADMIN,ROLE_USER', 7200, 5184000, null, null);
162+
INSERT INTO oauth_client_details VALUES ('clientIdPassword', 'oauth2-resource', '$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6', 'read,write,trust', 'password,authorization_code,refresh_token', null, 'ROLE_ADMIN,ROLE_USER', 7200, 5184000, null, 'false');
163163

164164
-- 密码经过了加密,全都是1234567890c
165165
INSERT INTO users (id, username, password, enabled, email, phone) VALUES (1, 'jeesun', '$2a$11$t4akVchfgOv00XxB/ZKLlOmweUoL/Aed4CiJqQjaiRLZpBU3AWfxu', true, '[email protected]', '18362102427');

0 commit comments

Comments
 (0)