您好,登錄后才能下訂單哦!
不懂golang如何實現(xiàn)單點登錄系統(tǒng)??其實想解決這個問題也不難,下面讓小編帶著大家一起學習怎么去解決,希望大家閱讀完這篇文章后大所收獲。
環(huán)境介紹
golang語言:go1.13.3+ 、
數(shù)據(jù)庫:mysql5.7
緩存:redis3.0
依賴包:
github.com/dgrijalva/jwt-go github.com/gin-gonic/gin github.com/go-xorm/xorm github.com/go-sql-driver/mysql
注意:項目代碼依賴管理工具采用的go-modules
數(shù)據(jù)請求類型
Content-Type: application/json
注:這個一定要注意,其他類型服務端不識別。
快速開始
1、數(shù)據(jù)庫
sql文件在data目錄下,新建數(shù)據(jù)庫名,通過mysql管理工具或登錄mysql數(shù)據(jù)庫直接創(chuàng)建數(shù)據(jù)庫名,并導入ssodb.sql文件到數(shù)據(jù)庫,完成數(shù)據(jù)庫結構創(chuàng)建。具體方法這里不再贅述。
2、配置mysql、redis
配置文件在conf目錄下,修改 mysql.go、redis.go配置成你自己的實際環(huán)境。
3、啟動
go run main.go
4、測試
我們可以采用postman等工具,進行客戶端瀏覽器模擬操作,也可以通過curl實現(xiàn)。
這里以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參數(shù)值只要隨意6位數(shù)即可,示例中服務端并沒有配置短信通道,驗證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 }}
access token 續(xù)期
$ 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" }
登出系統(tǒng)
$ 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" }
感謝你能夠認真閱讀完這篇文章,希望小編分享golang如何實現(xiàn)單點登錄系統(tǒng)?內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。