package main

import(
"fmt"
"github.com/gin-gonic/gin"

)

func Hello(c *gin.Context) {
resp := map[string]string{"hello":"world"}
c.JSON(200, resp)
}

func DummyMiddleware(c *gin.Context) {
fmt.Println("Im a dummy!")
c.Next()
}

func main() {
api := gin.Default()
api.Use(DummyMiddleware)
api.GET("/hello", Hello)
api.Run(":5000")
}

Gin middleware中间件_身份验证

package main

import (
"fmt"
"github.com/gin-gonic/gin"
)

func Hello(c *gin.Context) {
resp := map[string]string{"hello":"world"}
c.JSON(200, resp)
}

func DummyMiddleware() gin.HandlerFunc{
fmt.Println("hello")
return func(c *gin.Context) {
fmt.Println("world")
c.Next()
}
}

func main() {
api := gin.Default()
api.Use(DummyMiddleware())
api.GET("/hello", Hello)
api.Run(":5000")
}

Gin middleware中间件_json_02
其中hello只会执行一遍,world每次都会执行。

权限认证

package main

import (
"crypto/md5"
"encoding/hex"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)

const (
// 可自定义盐值
TokenSalt = "default_salt"
)

func MD5(data []byte) string {
_md5 := md5.New()
_md5.Write(data)
return hex.EncodeToString(_md5.Sum([]byte("")))
}

func Authorize() gin.HandlerFunc{
return func(c *gin.Context){
username := c.Query("username") // 用户名
ts := c.Query("ts") // 时间戳
token := c.Query("token") // 访问令牌
if strings.ToLower(MD5([]byte(username+ts+TokenSalt))) == strings.ToLower(token) {
// 验证通过,会继续访问下一个中间件
c.Next()
} else {
// 验证不通过,不再调用后续的函数处理
c.Abort()
c.JSON(http.StatusUnauthorized,gin.H{"message":"访问未授权"})
// return可省略, 只要前面执行Abort()就可以让后面的handler函数不再执行
return
}
}
}

func ServiceWithoutAuth(c *gin.Context){
c.JSON(http.StatusOK, gin.H{"message":"这是一个不用经过认证就能访问的接口"})
}

func ServiceWithAuth(c *gin.Context){
c.JSON(http.StatusOK, gin.H{"message":"这是一个需要经过认证才能访问的接口,看到此信息说明验证已通过"})
}


func main(){
router:=gin.Default()
// Use(Authorize())之前的接口,都不用经过身份验证
router.GET("/service_without_auth", ServiceWithoutAuth)
//以下的接口,都使用Authorize()中间件身份验证
router.Use(Authorize())
router.GET("/service_with_auth", ServiceWithAuth)
router.Run(":9999")
}

Gin middleware中间件_github_03