从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 ...
随机推荐
- SQL高效分页(百万条数据)
第一种方法:效率最高 SELECT TOP 页大小 * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1 ...
- 找到MySQL配置文件默认路径
如果不知道当前使用的配置文件的路径,可以尝试下面的操作: # which mysqld /usr/local/mysql/bin/mysqld # /usr/local/mysql/bin/mysql ...
- 使用Java Servlet进行简单登录
效果图 登录页面代码:login.html <%@ page language="java" contentType="text/html; charset=UTF ...
- ubuntu init启动流程
ubuntu的init方式有两种:一种是System V initialization,一种是Upstart.ubuntu6.10以前的版本是第一种方式,之后的版本是第二种方式. 在旧式的System ...
- SQL语句实现不存在即插入,存在则increase某字段的功能insert into … on duplicate key update
前提条件:必须是唯一主键: CREATE UNIQUE INDEX idx_vote_object ON test_customers_vote (`vote_object`, `vote_objec ...
- 开始使用 Vuejs 2.0 ---简单总结2
Vuejs的常用指令 v-html v-show v-if v-for v-on 1 .v-html v-html 更新元素或者变量的innerHTML,按普通html解析,和v-text的区别是在变 ...
- DDD学习笔记(一)
最近开始筹备一个电商项目. 其实是公司的老本行了. 但今年公司希望在做项目的同时, 沉淀出一套针对电商的基础产品. 这样可以提高新项目的开发效率, 减少重复劳动. 那现如今, DDD(领域驱动设计)应 ...
- [SQL Server] 无法连接到本地数据库
打开SQL Server配置管理器 启用下图两个协议 打开SQL Server服务 这一步可能出现这种情况: 故障原因是,安装Visual Studio 2012的时候,自动安装“Microsoft ...
- 用手机访问管理mysql
移动办公的情况及需求越来越多,平时MySQL,Oracle,SQLServer等数据库的管理都要通过客户端工具操作,现在有一款基于web网页的软件:TreeSoft数据库管理系统,在服务器布署一套后, ...
- 使用TreeDMS进行MySQL数据库的Web页面远程管理
在互联网应用蓬勃发展的时代背景下,各种各样的网络平台,网络应用,移动应用层出不穷,那么这些应用及平台都需要使用到数据库.如何高效的对数据进行日常维护.管理.监控成为迫切需要解决的问题. 基于web的方 ...