这是一个基于Go语言开发的单点登录系统,实现手机号注册、手机号+验证码登录、手机号+密码登录、账号登出等功能,用户认证采用cookie和jwt两种方式。收发短信相关方法已提供,仅需根据短信通道提供商提供的接口做相应的参数配置即可使用。
golang语言:go1.13.3+ 、
数据库:mysql5.7
缓存:redis3.0
https://github.com/guyan0319/...
github.com/dgrijalva/jwt-go
github.com/gin-gonic/gin
github.com/go-xorm/xorm
github.com/go-sql-driver/mysql
注意:项目代码依赖管理工具采用的go-modules,需要了解的移步https://github.com/guyan0319/...
Content-Type: application/json
注:这个一定要注意,其他类型服务端不识别。
sql文件在data目录下,新建数据库名,通过mysql管理工具或登录mysql数据库直接创建数据库名,并导入ssodb.sql文件到数据库,完成数据库结构创建。具体方法这里不再赘述。
配置文件在conf目录下,修改 mysql.go、redis.go配置成你自己的实际环境。
go run main.go
我们可以采用postman等工具,进行客户端浏览器模拟操作,也可以通过curl实现。
这里以curl工具为例:
$ curl -X POST "http://127.0.0.1:8282/signup/mobile" -i -d '{"mobile":"13522227564","passwd":"123456","code": "111111"}'
结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 86 100 28 100 58 1750 3625 --:--:-- --:--:-- --:--:-- 86000HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Tue, 09 Jun 2020 08:59:36 GMT
Content-Length: 28
{"code":200,"msg":"success"}
注:code参数值只要随意6位数即可,示例中服务端并没有配置短信通道,验证code代码已注释。
$ curl -X POST "http://127.0.0.1:8282/login" -i -d '{"mobile":"13522227564","passwd":"123456"}'
结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 69 100 28 100 41 451 661 --:--:-- --:--:-- --:--:-- 1112HTTP/1.1 200 OK
Access_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTg1ODIwMX0.OMpRhdX2wXm1BuRmeaZtSH7L3skXhNhYc0YYUTQf7WI
Content-Type: application/json; charset=utf-8
Refresh_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTg2MDAwMX0.Hglo9i8pxFGcRcvMw8mUvXlT2JLrHSC6ocQqJseknq8
Set-Cookie: Access_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTg1ODIwMX0.OMpRhdX2wXm1BuRmeaZtSH7L3skXhNhYc0YYUTQf7WI; Path=/; Max-Age=86400; HttpOnly
Set-Cookie: Refresh_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTg2MDAwMX0.Hglo9i8pxFGcRcvMw8mUvXlT2JLrHSC6ocQqJseknq8; Path=/; Max-Age=86400; HttpOnly
Set-Cookie: UserId=1; Path=/; Max-Age=86400; HttpOnly
Date: Wed, 10 Jun 2020 06:50:01 GMT
Content-Length: 28
{"code":200,"msg":"success"}
$ curl -X POST "http://127.0.0.1:8282/login/mobile" -i -d '{"mobile":"13522227564","code":"123456"}'
结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 68 100 28 100 40 163 233 --:--:-- --:--:-- --:--:-- 397HTTP/1.1 200 OK
Access_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NDIxOX0.FK-6ofW-ni8E7BcQ1tH9Z4vzQHDBbnyIcDZLEytRrfQ
Content-Type: application/json; charset=utf-8
Refresh_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NjAxOX0.gJzzho2gP1nNrkUKdtvMh0R3jGFZpA-ku0dWDvLftu0
Set-Cookie: Access_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NDIxOX0.FK-6ofW-ni8E7BcQ1tH9Z4vzQHDBbnyIcDZLEytRrfQ; Path=/; Max-Age=86400; HttpOnly
Set-Cookie: Refresh_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NjAxOX0.gJzzho2gP1nNrkUKdtvMh0R3jGFZpA-ku0dWDvLftu0; Path=/; Max-Age=86400; HttpOnly
Set-Cookie: UserId=1; Path=/; Max-Age=86400; HttpOnly
Date: Thu, 11 Jun 2020 09:30:19 GMT
Content-Length: 28
{"code":200,"msg":"success"}
使用上面登录获取的Access_Token获取用户信息。
$ curl "http://127.0.0.1:8282/my/info" -b "Access_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NDIxOX0.FK-6ofW-ni8E7BcQ1tH9Z4vzQHDBbnyIcDZLEytRrfQ"
结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 73 100 73 0 0 73000 0 --:--:-- --:--:-- --:--:-- 73000{"code":200,"data":{"id":1,"name":"","email":"","mobile":"135****27564"}}
$ curl -X POST "http://127.0.0.1:8282/signup/mobile/exist" -i -d '{"mobile":"13522227564"}'
结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 61 100 37 100 24 37000 24000 --:--:-- --:--:-- --:--:-- 61000HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Wed, 10 Jun 2020 07:40:50 GMT
Content-Length: 37
{"code":200,"data":{"is_exist":true}}
$ curl -X POST "http://127.0.0.1:8282/renewal" -i -b "Access_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NDIxOX0.FK-6ofW-ni8E7BcQ1tH9Z4vzQHDBbnyIcDZLEytRrfQ;Rfresh_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NjAxOX0.gJzzho2gP1nNrkUKdtvMh0R3jGFZpA-ku0dWDvLftu0"
结果:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 28 100 28 0 0 595 0 --:--:-- --:--:-- --:--:-- 595HTTP/1.1 200 OK
Access_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NTEzMn0.KCNVQ39HoMZFG-Xl0xwDrVdhQO_w4-tDxWY0ebyhfyk
Content-Type: application/json; charset=utf-8
Refresh_token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NjkzMn0.XijN8ZjPbZkhjJB33igti3dSm1tfd2kp_iNRtPqpwyM
Set-Cookie: Access_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NTEzMn0.KCNVQ39HoMZFG-Xl0xwDrVdhQO_w4-tDxWY0ebyhfyk; Path=/; Max-Age=86400; HttpOnly
Set-Cookie: Refresh_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NjkzMn0.XijN8ZjPbZkhjJB33igti3dSm1tfd2kp_iNRtPqpwyM; Path=/; Max-Age=86400; HttpOnly
Date: Thu, 11 Jun 2020 09:45:32 GMT
Content-Length: 28
{"code":400,"msg":"success"}
$ curl -X POST "http://127.0.0.1:8282/logout" -b "Access_Token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsImV4cCI6MTU5MTk1NDIxOX0.FK-6ofW-ni8E7BcQ1tH9Z4vzQHDBbnyIcDZLEytRrfQ"
结果
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 28 100 28 0 0 28000 0 --:--:-- --:--:-- --:--:-- 28000{"code":200,"msg":"success"}
仅使用jwt实现单点登录会遇到两个问题
在使用过程如遇到任何问题,欢迎issues。