从golang-gin-realworld-example-app项目学写httpapi (五)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/middlewares.go
中间件定义
package users
import (
"net/http"
"strings"
"github.com/dgrijalva/jwt-go/request"
"github.com/gin-gonic/gin"
"github.com/wangzitian0/golang-gin-starter-kit/common"
)
// 请求头 认证参数前缀标注TOKEN处理, 不使用默认的Bearer标注,而是用TOKEN
// 函数 返回TOKEN字符串值
func stripBearerPrefixFromTokenString(tok string) (string, error) {
if len(tok) > 5 && strings.ToUpper(tok[0:6]) == "TOKEN " {
return tok[6:], nil
}
return tok, nil
}
// 从请求头获取 Authorization 参数内容
var AuthorizationHeaderExtractor = &request.PostExtractionFilter{
request.HeaderExtractor{"Authorization"},
stripBearerPrefixFromTokenString,
}
// 从AuthorizationHeaderExtractor获取 access_token 参数内容
var MyAuth2Extractor = &request.MultiExtractor{
AuthorizationHeaderExtractor,
request.ArgumentExtractor{"access_token"},
}
// 函数 更新用户上下文相关内容
func UpdateContextUserModel(c *gin.Context, my_user_id uint) {
var myUserModel UserModel
if my_user_id != 0 {
db := common.GetDB()
db.First(&myUserModel, my_user_id)
}
c.Set("my_user_id", my_user_id)
c.Set("my_user_model", myUserModel)
}
// 函数 用户自定义的中间件, 使用 r.Use(AuthMiddleware(true))
func AuthMiddleware(auto401 bool) gin.HandlerFunc {
return func(c *gin.Context) {
// 初始化用户上下文,用户ID初值为0, 用户初值为nil
UpdateContextUserModel(c, 0)
// 获取请求中的token内容,并解密为token对象
token, err := request.ParseFromRequest(c.Request, MyAuth2Extractor, func(token *jwt.Token) (interface{}, error) {
b := ([]byte(common.NBSecretPassword))
return b, nil
})
if err != nil {
// 中间件开关为true时,返回认证错误信息
if auto401 {
c.AbortWithError(http.StatusUnauthorized, err)
}
return
}
// 成功获取payload信息并且校验成功,调用函数更新用户上下文
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
my_user_id := uint(claims["id"].(float64))
//fmt.Println(my_user_id, claims["id"])
UpdateContextUserModel(c, my_user_id)
}
}
}
从golang-gin-realworld-example-app项目学写httpapi (五)的更多相关文章
- 从golang-gin-realworld-example-app项目学写httpapi (八)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/common/unit_test.go 单元测试 ...
- 从golang-gin-realworld-example-app项目学写httpapi (七)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/hello.go main调用 package ...
- 从golang-gin-realworld-example-app项目学写httpapi (六)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/validators.go 验证器 ...
- 从golang-gin-realworld-example-app项目学写httpapi (四)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/routers.go 路由定义 pa ...
- 从golang-gin-realworld-example-app项目学写httpapi (三)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/serializers.go 序列化 ...
- 从golang-gin-realworld-example-app项目学写httpapi (二)
https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/models.go 模型定义 use ...
- 从golang-gin-realworld-example-app项目学写httpapi (一)
https://wangzitian0.github.io/2013/06/29/zero-to-one-1/ https://github.com/gothinkster/golang-gin-re ...
- Golang Gin 项目包依赖管理 godep 使用
Golang Gin 项目包依赖管理 godep 使用 标签(空格分隔): Go 在按照github.com/tools/godep文档go get完包以后,调整项目结构为$GOPATH/src/$P ...
- Golang Gin 项目使用 Swagger
Golang Gin 项目使用 Swagger 标签(空格分隔): Go 首先需要github.com/swaggo/gin-swagger和github.com/swaggo/gin-swagger ...
随机推荐
- Pycharm的配置和使用
pycharm pycharm是一个比较好的python IDE,可以在MACOS和windows上使用,补全功能强大,而且界面十分友好,特别适合python编程人员使用. pycharm Pycha ...
- WPF 使用依赖属性自定义控件
使用依赖属性自定义控件,依赖属性必须定义在自定义控件中,不能定义在其他文件中 一.先实现一个类继承你要复写的类 using System; using System.Collections.Gener ...
- concurrency基础
Runnable 一个执行任务,没有返回值,也不能抛出受检查异常 Callable 一个执行任务有返回值,也能抛出受检查异常 Future 表示执行任务的生命周期,任务的生命周期为:创建,提交,开始, ...
- JS中深拷贝数组、对象、对象数组方法
我们在JS程序中需要进行频繁的变量赋值运算,对于字符串.布尔值等可直接使用赋值运算符 “=” 即可,但是对于数组.对象.对象数组的拷贝,我们需要理解更多的内容. 首先,我们需要了解JS的浅拷贝与深拷贝 ...
- 使用 Python 编写脚本并发布
使用 Python 编写脚本并发布 P1: 脚本 通常在 Linux 服务器上会遇到在命令行中输入命令的操作,而有些操作包含的命令数目较多或者其中的命令包含的参数较多,如果一个一个的敲命令的话就太麻烦 ...
- 我与ARM的那些事儿1初识ARM
最近一直在研究ARM,说到ARM,我们首先想到了是三星.高通等公司,这些公司都制造CPU的,其实ARM也是一家公司,只不过它是提供最核心的逻辑电路,而且它的赚钱方式是与其他公司进行双赢的!你卖出多少芯 ...
- i.mx android6 输入子系统分析(未完)
参考:http://blog.csdn.net/u010312937/article/details/53285286 https://www.jianshu.com/p/7fca94b330ea ...
- ansible 回调函数处理
https://www.u3v3.com/ar/1421 https://serversforhackers.com/c/running-ansible-2-programmatically
- 使用javascript获取wx.config内部字段解决微信分享
背景 在微信分享开发的时候我们通常的流程是 <?php require_once "jssdk.php"; $jssdk = new JSSDK("yourAppI ...
- laravel数据迁移的时候遇到的字符串长度的问题
问题截图: 问题解决办法: use Illuminate\Support\facades\Schema; Schema::defaultStringLength(191); ...