從零開始 寫乙個認證授權sdk

2021-10-13 13:13:25 字數 3699 閱讀 4120

---

# 主題列表:juejin, github, smartblue, cyanosis, channing-cyan, fancy, hydrogen, condensed-night-purple, greenwillow, v-green, vue-pro, healer-readable, mk-cute, jzman, geek-black, awesome-green

# 貢獻主題:

theme: juejin

highlight:

---#### 用到技術棧

sdk 基於 aop和redis

依賴以及版本

```org.springframework.boot

spring-boot-starter-aop

2.1.8.release

provided

org.springframework.boot

spring-boot-starter-data-redis

2.1.8.release

provided

org.springframework.boot

spring-boot-starter-web

2.1.8.release

provided

org.apache.commons

commons-lang3

3.11

com.alibaba

fastjson

1.2.62

#### sdk引入專案

1、引入響應的provided依賴和sdk

```liuxf.live

spring-boot-valid-token

1.0-snapshot

```2、配置sdk需要的redis

```spring.redis.database=0

spring.redis.port=6379

spring.redis.password=

spring.redis.host=127.0.0.1

```3、開啟功能

啟動類上加上啟動註解

```@enablevalidtoken

```#### 功能列表

##### 1、設定token

引入實現,模擬登陸介面,設定token和角色

設定token 有兩個過載方法乙個系統生成token,乙個通過sdk生成,角色可以設定也可以不設定,自定義超時時間單位分鐘

```@resource

usertokendetailservice usertokendetailservice;

//使用者名稱密碼正確

string token = usertokendetailservice.puttoken(user.getid()+"",50,"admin");

userdto userdto = new userdto();

userdto.setusername(user.getusername());

userdto.setid(user.getid());

userdto.settoken(token);

return userdto;

}```

##### 2、移除token

模擬登出介面 呼叫移除方法會清除該使用者的token和角色資訊

usertokendetailservice.removetoken(user.getid()+"");

return "移除成功";

}```

##### 3、許可權驗證

分為三種情況

1、不帶註解@validtoken 該模式模擬遊客訪問模式

2、帶註解不設定角色驗證 該模式只認證token

3、帶註解設定角色驗證  即校驗token也校驗登入時的角色設定

return "data介面";

}```

##### 4、校驗失敗丟擲異常

使用時需要全域性異常捕獲

```public class validtokenexception extends runtimeexception implements serializable

public validtokenexception(validtokenexceptionenum validtokenexceptionenum)

public validtokenexceptionenum getvalidtokenexceptionenum()

}```

全域性異常捕獲

```@exceptionhandler(value = validtokenexception.class)

public jsonobject exceptionhandler(validtokenexception ex)

```#### 實現思路

##### 利用aop獲取token

利用aop的@before功能來獲取token進行驗證,在利用token獲取使用者角色進行角色的驗證

```@before("validtokencut()")

public void before(joinpoint point) throws validtokenexception

```1、驗證token

```private void validtoken(string token)

}```

2、驗證角色

```private void validroles(joinpoint point, string token)

listredisroles = jsonobject.parseobject(redisrolesstr, list.class);

string roles = method.getannotation(validtoken.class).roles();

if (roles != null || roles.length > 0)

listintersection = redisroles.stream().filter(item -> roleslist.contains(item)).collect(tolist());

if (collectionutils.isempty(intersection))

}```

##### 利用redis進行token和角色的儲存查詢

1、儲存token和角色,使用者生成token

```@override

public void puttoken(string userid, string token, long timeout, string... roles)

```2、儲存token和角色,sdk生成token

```@override

public string puttoken(string userid, long timeout, string... roles)

```3、移除token

```@override

public void removetoken(string userid)

stringredistemplate.delete(userid);

}```

#### 總體思路

通過登入獲取許可權資訊儲存到redis,通過aop進行獲取校驗,丟擲異常,認證和鑑權的功能,每隔方法可以多角色賦值,登入也可以多角色錄入。

#### 最後

[github位址](

有問題歡迎指正,不用吝嗇你的小星星哦

從零開始寫乙個Redis 1

為了學習redis和更好地理解redis,決定自己寫乙個c 版本的redis 2.redis協議解析 第一,我這裡僅僅只是學習redis的思想。所以使用的是c 而且很多資料結構使用stl。第二,我這裡不追求效能,所以不會極致地去考慮效能問題,而是以實現功能為主。對於看過redis原始碼的我來說,實際...

怎麼從零開始寫乙個秒殺專案

一,環境搭建 技術選型,該秒殺專案是採用springboot2.0和springcloud來開發的 1首先搭建父專案,所有的模組都是依賴該父專案 2搭建服務註冊中心模組 eureka 3秒殺使用者模組 整合mybatis,web,thymeleaf等 相當於買家 4公共模組,對應的是資料庫表對應的實...

從零開始寫乙個jquery日曆外掛程式(一)

簡單來說,jquery外掛程式為擴充套件jquery原型物件的乙個方法。編寫jquery外掛程式其實就是給jquery原型新增乙個自定義的方法。我們先來看乙個最簡單的例子 a attr href 這裡使用jquery選擇器,來查詢a元素,並返回乙個jquery物件。我們就可以通過這個物件來使用jqu...