mysql表的設計幾種方式_支持多種登錄方式的數據表設計 | 六阿哥博客

一個帶有用戶系統的應用最基本登錄方式是站內賬號登錄,但這種方式往往不能滿足我們的需求。現在的應用基本都有站內賬號、email、手機和一堆第三方登錄,那么如果需要支持這么多種登錄方式,或者還有銀行卡登錄、身份證登錄等等更多的登錄方式,我們的數據表應該怎么設計才更合理呢?

需求分析

實現多種登錄方式,并且除了站內賬號登錄方式以外的登錄方式,都能夠進行綁定和解綁或者更換綁定。

如果按照傳統的數據表設計,我們用戶表會存儲用戶的賬號和密碼等授權相關的字段,類似下面:

id

username

password

nickname

sex

...

1

2

3

4

5

6

id

username

password

nickname

sex

...

但是如果登錄方式非常多的情況下,這種數據表結構不再適用。那么應該怎么設計呢?在查閱了一些資料后,本渣渣終于有了一個自我感覺很合理的設計方式。

首先,一個用戶不管有多少種登錄方式,用戶還是只有那一個用戶,但登錄方式卻有多種。這就形成了一對多的關系:一個用戶對應多個登錄方式。

所以,我們就可以把用戶表拆分成2張表,一張表存儲用戶基本的數據,另一張表存儲登錄授權相關的數據。我們可以向下面這樣設計:

users

id

nickname

sex

age

email

mobile

status

...

1

2

3

4

5

6

7

8

id

nickname

sex

age

email

mobile

status

...

user_auths

id # 自增id

user_id # users表對應的id

identity_type # 身份類型(站內username 郵箱email 手機mobile 或者第三方的qq weibo weixin等等)

identifier # 身份唯一標識(存儲唯一標識,比如賬號、郵箱、手機號、第三方獲取的唯一標識等)

credential # 授權憑證(比如密碼 第三方登錄的token等)

verified # 是否已經驗證(存儲 1、0 來區分是否已經驗證通過)

1

2

3

4

5

6

id#自增id

user_id#users表對應的id

identity_type#身份類型(站內username郵箱email手機mobile或者第三方的qqweiboweixin等等)

identifier#身份唯一標識(存儲唯一標識,比如賬號、郵箱、手機號、第三方獲取的唯一標識等)

credential#授權憑證(比如密碼第三方登錄的token等)

verified#是否已經驗證(存儲1、0來區分是否已經驗證通過)

這樣我們創建一個用戶,首先需要創建一條 users 表的用戶基礎信息記錄和一條或者多條 user_auths 表的授權記錄。注意修改密碼時也需要同時修改多條 user_auths 記錄,保證需要密碼的登錄方式憑證需要同步更新。而第三方的授權憑證和需要密碼的授權憑證則不需要同步。

代碼實現

這里我使用 laravel 來實現簡單的用戶注冊、登錄、修改密碼等操作,僅供參考。

首先創建2張數據表,結構如下:

users

public function up()

{

Schema::create('users', function (Blueprint $table) {

$table->increments('id');

$table->string('nickname', 30)->default('寶寶')->comment('昵稱');

$table->string('say')->nullable()->comment('心情寄語');

$table->string('avatar', 50)->default('uploads/user/avatar.jpg')->comment('頭像');

$table->string('mobile', 11)->nullable()->comment('手機號碼');

$table->string('email', 50)->nullable()->comment('郵箱');

$table->tinyInteger('sex')->default(0)->comment('性別 0女 1男');

$table->tinyInteger('status')->default(1)->comment('狀態 1可用 0 不可用');

$table->tinyInteger('is_admin')->default(0)->comment('是否是管理員');

$table->tinyInteger('qq_binding')->default(0)->comment('QQ登錄是否綁定');

$table->tinyInteger('weixin_binding')->default(0)->comment('微信登錄是否綁定');

$table->tinyInteger('weibo_binding')->default(0)->comment('微博登錄是否綁定');

$table->tinyInteger('email_binding')->default(0)->comment('郵箱登錄是否綁定');

$table->tinyInteger('phone_binding')->default(0)->comment('手機登錄是否綁定');

$table->timestamps();

});

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

publicfunctionup()

{

Schema::create('users',function(Blueprint$table){

$table->increments('id');

$table->string('nickname',30)->default('寶寶')->comment('昵稱');

$table->string('say')->nullable()->comment('心情寄語');

$table->string('avatar',50)->default('uploads/user/avatar.jpg')->comment('頭像');

$table->string('mobile',11)->nullable()->comment('手機號碼');

$table->string('email',50)->nullable()->comment('郵箱');

$table->tinyInteger('sex')->default(0)->comment('性別 0女 1男');

$table->tinyInteger('status')->default(1)->comment('狀態 1可用 0 不可用');

$table->tinyInteger('is_admin')->default(0)->comment('是否是管理員');

$table->tinyInteger('qq_binding')->default(0)->comment('QQ登錄是否綁定');

$table->tinyInteger('weixin_binding')->default(0)->comment('微信登錄是否綁定');

$table->tinyInteger('weibo_binding')->default(0)->comment('微博登錄是否綁定');

$table->tinyInteger('email_binding')->default(0)->comment('郵箱登錄是否綁定');

$table->tinyInteger('phone_binding')->default(0)->comment('手機登錄是否綁定');

$table->timestamps();

});

}

user_auths

public function up()

{

Schema::create('user_auths', function (Blueprint $table) {

$table->increments('id');

$table->integer('user_id')->index()->comment('用戶id');

$table->string('identity_type')->comment('登錄類型(手機號phone 郵箱email 用戶名username)或第三方應用名稱(微信weixin 微博weibo 騰訊QQqq等)');

$table->string('identifier')->unique()->index()->comment('標識(手機號 郵箱 用戶名或第三方應用的唯一標識)');

$table->string('credential')->nullable()->comment('密碼憑證(站內的保存密碼,站外的不保存或保存token)');

$table->tinyInteger('verified')->default(0)->comment('是否已經驗證');

$table->timestamps();

});

}

1

2

3

4

5

6

7

8

9

10

11

12

publicfunctionup()

{

Schema::create('user_auths',function(Blueprint$table){

$table->increments('id');

$table->integer('user_id')->index()->comment('用戶id');

$table->string('identity_type')->comment('登錄類型(手機號phone 郵箱email 用戶名username)或第三方應用名稱(微信weixin 微博weibo 騰訊QQqq等)');

$table->string('identifier')->unique()->index()->comment('標識(手機號 郵箱 用戶名或第三方應用的唯一標識)');

$table->string('credential')->nullable()->comment('密碼憑證(站內的保存密碼,站外的不保存或保存token)');

$table->tinyInteger('verified')->default(0)->comment('是否已經驗證');

$table->timestamps();

});

}

實現注冊功能,創建站內賬號,一個用戶 + 一個站內賬號登錄授權。

public function register(Request $request)

{

// 已經登錄則直接跳轉

if (Session::has('user')) {

return redirect()->route('admin.index');

}

if ($request->method() === 'GET') {

return view('admin.user.register');

}

// 驗證表單

$validator = Validator::make($request->all(), [

'identifier' => ['required', 'between:6,16', 'unique:user_auths'],

'credential' => ['required', 'between:6,16', 'confirmed'],

], [

'identifier.required' => '用戶名為必填項',

'identifier.unique' => '用戶名已經存在',

'identifier.between' => '用戶名長度必須是6-16',

'credential.required' => '密碼為必填項',

'credential.between' => '密碼長度必須是6-16',

'credential.confirmed' => '兩次輸入的密碼不一致',

]);

if ($validator->fails()) {

return back()->withErrors($validator);

}

// 創建用戶

$user = new User();

$user->save();

// 創建授權

$userAuth = new UserAuth();

$userAuth->user_id = $user->id;

$userAuth->identity_type = 'username';

$userAuth->identifier = $request->identifier;

$userAuth->credential = bcrypt($request->credential);

$userAuth->save();

return redirect()->route('admin.login');

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

publicfunctionregister(Request$request)

{

// 已經登錄則直接跳轉

if(Session::has('user')){

returnredirect()->route('admin.index');

}

if($request->method()==='GET'){

returnview('admin.user.register');

}

// 驗證表單

$validator=Validator::make($request->all(),[

'identifier'=>['required','between:6,16','unique:user_auths'],

'credential'=>['required','between:6,16','confirmed'],

],[

'identifier.required'=>'用戶名為必填項',

'identifier.unique'=>'用戶名已經存在',

'identifier.between'=>'用戶名長度必須是6-16',

'credential.required'=>'密碼為必填項',

'credential.between'=>'密碼長度必須是6-16',

'credential.confirmed'=>'兩次輸入的密碼不一致',

]);

if($validator->fails()){

returnback()->withErrors($validator);

}

// 創建用戶

$user=newUser();

$user->save();

// 創建授權

$userAuth=newUserAuth();

$userAuth->user_id=$user->id;

$userAuth->identity_type='username';

$userAuth->identifier=$request->identifier;

$userAuth->credential=bcrypt($request->credential);

$userAuth->save();

returnredirect()->route('admin.login');

}

實現登錄,站內賬號、郵箱、手機號碼登錄方式。

public function login(Request $request)

{

// 已經登錄則直接跳轉

if (Session::has('user')) {

return redirect()->route('admin.index');

}

if ($request->method() === 'GET') {

return view('admin.user.login');

}

// 驗證表單

$validator = Validator::make($request->all(), [

'identifier' => ['required', 'exists:user_auths'],

'credential' => ['required', 'between:6,16'],

], [

'identifier.exists' => '用戶不存在',

'identifier.required' => '用戶名為必填項',

'credential.required' => '密碼為必填項',

'credential.between' => '密碼長度必須是6-16',

]);

if ($validator->fails()) {

return back()->withErrors($validator);

}

// 查詢授權記錄 - 查詢3種登錄方式的授權記錄

$userAuth = UserAuth::where('identifier' , $request->identifier)

->whereIn('identity_type', ['username', 'phone', 'email'])

->first();

if (isset($userAuth) && Hash::check($request->credential, $userAuth->credential)) {

// 查詢用戶表

$user = User::find($userAuth->user_id);

if ($user->status == 0) {

return back()->with('errors', '用戶已經被禁用');

}

if ($user->is_admin == 0) {

return back()->with('errors', '普通用戶禁止登陸后臺');

}

Session::put('user', $user);

return redirect()->route('admin.index');

} else {

return back()->with('errors', '管理員密碼錯誤');

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

publicfunctionlogin(Request$request)

{

// 已經登錄則直接跳轉

if(Session::has('user')){

returnredirect()->route('admin.index');

}

if($request->method()==='GET'){

returnview('admin.user.login');

}

// 驗證表單

$validator=Validator::make($request->all(),[

'identifier'=>['required','exists:user_auths'],

'credential'=>['required','between:6,16'],

],[

'identifier.exists'=>'用戶不存在',

'identifier.required'=>'用戶名為必填項',

'credential.required'=>'密碼為必填項',

'credential.between'=>'密碼長度必須是6-16',

]);

if($validator->fails()){

returnback()->withErrors($validator);

}

// 查詢授權記錄 - 查詢3種登錄方式的授權記錄

$userAuth=UserAuth::where('identifier',$request->identifier)

->whereIn('identity_type',['username','phone','email'])

->first();

if(isset($userAuth)&&Hash::check($request->credential,$userAuth->credential)){

// 查詢用戶表

$user=User::find($userAuth->user_id);

if($user->status==0){

returnback()->with('errors','用戶已經被禁用');

}

if($user->is_admin==0){

returnback()->with('errors','普通用戶禁止登陸后臺');

}

Session::put('user',$user);

returnredirect()->route('admin.index');

}else{

returnback()->with('errors','管理員密碼錯誤');

}

}

實現修改密碼,站內登錄、郵箱登錄、手機登錄同步修改。

public function modifyPassword(Request $request)

{

if ($request->method() === 'GET') {

return view('admin.user.modify');

}

// 驗證輸入字段

$validator = Validator::make($request->all(), [

'credential' => 'required|between:6,20|confirmed',

], [

'credential.required' => '新密碼不能為空!',

'credential.between' => '新密碼必須在6-20位之間',

'credential.confirmed' => '新密碼和確認密碼不一致',

]);

if ($validator->fails()) {

return back()->withErrors($validator);

}

// 判斷當前Session里的用戶是否還有效

$user = Session::get('user');

if (! isset($user)) {

return redirect()->route('admin.login')->with('errors', '登錄超時');

}

// 查詢用戶權限表,修改密碼

$userAuths = UserAuth::where('user_id', $user->id)

->whereIn('identity_type', ['username', 'email', 'phone'])

->get();

if (count($userAuths) && Hash::check($request->credential_o, $userAuths[0]->credential)) {

UserAuth::where('user_id', $user->id)

->whereIn('identity_type', ['username', 'email', 'phone'])

->update(['credential' => bcrypt($request->credential)]);

return back()->with('errors', '修改密碼成功!');

}

return back()->with('errors', '原密碼錯誤!');

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

publicfunctionmodifyPassword(Request$request)

{

if($request->method()==='GET'){

returnview('admin.user.modify');

}

// 驗證輸入字段

$validator=Validator::make($request->all(),[

'credential'=>'required|between:6,20|confirmed',

],[

'credential.required'=>'新密碼不能為空!',

'credential.between'=>'新密碼必須在6-20位之間',

'credential.confirmed'=>'新密碼和確認密碼不一致',

]);

if($validator->fails()){

returnback()->withErrors($validator);

}

// 判斷當前Session里的用戶是否還有效

$user=Session::get('user');

if(!isset($user)){

returnredirect()->route('admin.login')->with('errors','登錄超時');

}

// 查詢用戶權限表,修改密碼

$userAuths=UserAuth::where('user_id',$user->id)

->whereIn('identity_type',['username','email','phone'])

->get();

if(count($userAuths)&&Hash::check($request->credential_o,$userAuths[0]->credential)){

UserAuth::where('user_id',$user->id)

->whereIn('identity_type',['username','email','phone'])

->update(['credential'=>bcrypt($request->credential)]);

returnback()->with('errors','修改密碼成功!');

}

returnback()->with('errors','原密碼錯誤!');

}

例子中路由相關代碼直接無視!如果后期需要新增或刪除登錄方式,只需要新增或刪除 user_auths 表中的記錄。如果是判斷郵箱、手機是否已經驗證,也只是操作 user_auths 表中的?verified 字段即可。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/530010.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/530010.shtml
英文地址,請注明出處:http://en.pswp.cn/news/530010.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

將Go語言開發的Web程序部署到K8S

搭建K8S基礎環境 如果已經有K8S環境的同學可以跳過,如果沒有,推薦你看看我的《Ubuntu22加Minikue搭建K8S環境》,課程目錄如下: Ubuntu22安裝Vscode 下載:https://code.visualstudio.com/Download 安裝命令&#…

python 掃描儀_基于Opencv和Python的多選掃描儀

首先,我檢測到圖像右側的20個黑框,然后將x和寬度添加到列表中:image cv2.imread(args["image"])gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)(_, thresh) cv2.threshold(gray, 220, 255,cv2.THRESH_BINARY)kernel cv2.getStr…

mysql dmz_MySQL 中LIMIT的使用詳解

MySQL的Limit子句Limit子句可以被用于強制 SELECT 語句返回指定的記錄數。Limit接受一個或兩個數字參數。參數必須是一個整數常量。如果給定兩個參數,第一個參數指定第一個返回記錄行的偏移量,第二個參數指定返回記錄行的最大數目。//初始記錄行的偏移量…

python編程入門到實踐筆記習題_Python編程從入門到實踐筆記——列表簡介

python編程從入門到實踐筆記——列表簡介#codingutf-8#列表——我的理解等于c語言和java中的數組bicycles ["trek","cannondale","readline","specialized"]print(bicycles)#列表索引從0開始print(bicycles[0].title())#訪問列表元素…

informatica mysql odbc_Informatica 配置mysql community odbc連接

Informatica linux 版本內置的DataDirect 驅動支持各種數據庫例如oracle、sybase、postgreSQL、Greenplum、mysql等等但是mysql 只支持企業版本,如果我們使用的是community 社區版本便不能使用自帶的DataDirect方式了,那我們就需要手動配置其他odbc連接。…

mysql分表 動態擴容_數據庫hash分表后的擴容方案

postgres的hash分表不停機擴容方案原來我們hash分表之后,數據擴容采用的是rehash,這樣遷移全部的數據,比較麻煩。本次擴容利用hash環原理,并在此基礎上做一些適應性的改動。首先假定哈希環的范圍為0-1023,總共1024的數…

php mysql長連接聊天室_PHP之探索MySQL 長連接、連接池

PHP連接MysqL的方式,用的多的是MysqL擴展、MysqLi擴展、pdo_MysqL擴展,是官方提供的。PHP的運行機制是頁面執行完會釋放所有該PHP進程中的所有資源的,如果有多個并發訪問本地的測試頁面 http://127.0.0.1/1.php 根據PHP跟web服務器的不同,會開…

python 讀取地震道頭數據_python地震數據可視化詳解

本文實例為大家分享了python地震數據可視化的具體代碼,供大家參考,具體內容如下準備工作:在windows10下安裝python3.7,下載參考源碼到本地。1. demo繪圖測試demo繪圖指令cmd> python seisplot.py --demo問題1)缺少依賴包File &…

在MySQL查詢山東省男生信息_MySQL-查詢

來一波英語單詞解釋(意思)create 創建show 顯示database 數據庫use 使用select 選擇table 表from 來自…distinct 消除重復行as 同樣地(用于其別名)where 范圍like 模糊查詢rlike 正則查詢In 范圍查詢not in 不非連續的范圍之內between ... and …表示…

java 導入world數據_java讀取world文件,把world文件中的內容,原樣輸出到頁面上。...

POI,處理可以。樣式在Java代碼中添加就可以。給了一個例子這個是Excel的。package cn.com.my.common;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.sql.Connection;import java.sql.ResultSet…

java程序員 css_Java程序員從笨鳥到菜鳥之(十七)CSS基礎積累總結(下)

七.組織元素(span和div)span和div元素用于組織和結構化文檔,并經常聯合class和id屬性一起使用。在這一課中,我們將進一步探究span和div的用法,因為這兩個HTML元素對于CSS是很重要的。用span組織元素用div組織元素用span組織元素span元素可以說…

redlock java_Redlock分布式鎖

這篇文章主要是對 Redis 官方網站刊登的 Distributed locks with Redis 部分內容的總結和翻譯。什么是 RedLockRedis 官方站這篇文章提出了一種權威的基于 Redis 實現分布式鎖的方式名叫 Redlock,此種方式比原先的單節點的方法更安全。它可以保證以下特性&#xff1…

java 兩個數組交叉_java – 如何交叉兩個沒有重復的排序整數數組?

這個問題本質上減少到一個連接操作,然后是一個過濾器操作(刪除重復,只保留內部匹配).由于輸入都已經排序,所以可以通過O(O(size(a)size(b))的merge join來有效地實現連接.過濾器操作將為O(n),因為連接的輸出被排序,并且要刪除重復項,所有您需要做的是檢查每個元素是否與之??前…

java retentionpolicy_Java注解之如何利用RetentionPolicy.SOURCE生存周期

上一篇文章簡單講了下Java注解的學習之元注解說明,學習了Java注解是如何定義的,怎么使用的,但是并沒有介紹Java的注解是怎么起作用的,像Spring Boot里面的那些注解,到底是怎么讓程序這樣子運行起來的?特別是…

在java程序中定義的類有兩種成員_java試題 急需答案 謝謝!!!

三、填空(每小題2分,共10分)1.在Applet中,創建一個具有10行45列的多行文本區對象ta的語句為:2.創建一個標識有“關閉”字樣的標簽對象gb的語句為。3.方法是一種僅有方法頭,沒...三、填空(每小題…

java 同步 變量,在java中的對象上同步,然后更改同步的變量的值

I came across a code like thissynchronized(obj) {obj new Object();}Something does not feel right about this , I am unable to explain, Is this piece of code OK or there is something really wrong in it, please point it out.Thanks解決方案Its probably not wha…

java set泛型_Java 集合二 泛型、Set相關

泛型1、在定義一個類的方法時,因為不確定返回值類型,所以用一個符號代替,這個符號就是泛型eg:ArrayList list new ArrayList();2、泛型的好處:1、提高了數據的安全性,將運行時的問題提前暴露在編譯階段2、避免了強轉的…

java annotation 實現_在Java中如何實現自己的annotation

1. 先定義annotation2. 使用annotation例子:import java.lang.annotation.*;import java.lang.reflect.Method;Target(ElementType.METHOD)Retention(RetentionPolicy.RUNTIME)interface Test {String info() default "";}class Annotated {Test(info &q…

登錄界面攔截java_java攔截通過url訪問頁面,必須通過登錄頁面訪問目標頁面

在web.xml中配置過濾:LoginFiltercom.verification.action.LoginFilterLoginFiltery/form/dealParse.do/* 攔截所有請求/.do 攔截以“.do”結尾的請求/index.jsp 攔截指定的jsp/artery/form/* 攔截該目錄下的所有請求等等攔截器,攔截請求類&#xf…

python textwrap_[Python標準庫]textwrap——格式化文本段落

textwrap——格式化文本段落作用:通過調整換行符在段落中出現的位置來格式化文本。 Python 版本:2.5 及以后版本 需要美觀打印時,可以用 textwrap 模塊來格式化要輸出的文本。這個模塊允許通過編程提供類似段落自動換行或填充…