从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 ...
随机推荐
- IDE神器intellij idea的基本使用 (转载)
一.关于新建工程,导入工程,配置jdk,tomcat这里不做过多的讲述,必定网络上关于此类配置一堆一堆的. 二.编码快捷键(比较常用的快捷键)该套快捷键选择的是:Mac OS X 10.5+ 1. a ...
- Java的三个基础排序算法(其余将在以后补充)
第一个:冒泡排序算法 原理:相邻的两个值进行比较,如果前面的比后面的大就交换位置 eg:假设有5个元素的一个array 第一次:会比较4次,将最大的值放在最右边 第二次:会比较3次,又排出剩余4个元素 ...
- Guava源码解析之EventBus
最近看Elastic-Job源码,看到它里面实现的任务运行轨迹的持久化,使用的是Guava的AsyncEventBus,一个内存级别的异步事件总线服务,实现了简单的生产-消费者模式,从而在不影响任务执 ...
- Microsoft Power BI Desktop概念学习系列之Microsoft Power BI Desktop是什么?
不多说,直接上干货! 官网 https://powerbi.microsoft.com/zh-cn/desktop/ Microsoft Power BI Desktop是什么? https://p ...
- JavaScript数据结构-3.List
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- POJ 3481 Double Queue(set实现)
Double Queue The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Buchares ...
- centOS 7下无法启动网络(service network start)错误解决办法(应该是最全的了。。。)
今天在centOS 7下更改完静态ip后发现network服务重启不了,翻遍了网络,尝试了各种方法,终于解决了. 现把各种解决方法归纳整理,希望能让后面的同学少走点歪路... 首先看问题:执行serv ...
- <数据挖掘导论>读书笔记9聚类分析
1. 聚类分析仅根据在数据中发现的描述对象及其关系的信息,将数据对象分组. 其目标是组内的对象相互之间是相似的或者相关的,而不同组中的对象是不同的或者不相关的. 2.聚类分析的重要技术 K均值:K均值 ...
- js 继承介绍
js中继承的方式并不是明确的,这里介绍常用的几种 一.对象冒充(构造函数绑定) 原理:使用对象冒充继承基类,实质上是使用call或apply方法改变this 指针的指向 function Monkey ...
- JAVA-3NIO之Buffer和Buffer之Scatter/Gather
注意:转载自并发编程网,java nio系列教程 1.Buffer Java NIO中的Buffer用于和NIO通道进行交互.如你所知,数据是从通道读入缓冲区,从缓冲区写入到通道中的. 缓冲区本质上是 ...