123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package middleware
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "lims-extend/response"
- "net/http"
- "strings"
- "time"
- )
- var (
- pyBackendIp string
- pyPort string
- )
- func InitMiddleware(ip string, port string) {
- pyBackendIp = ip
- pyPort = port
- }
- func AuthMiddleware() gin.HandlerFunc {
- return func(c *gin.Context) {
-
- accessToken := c.GetHeader("accessToken")
- client := &http.Client{Timeout: 1 * time.Second}
- req, _ := http.NewRequest("GET",
- fmt.Sprintf("http://%s:%s/api/v1/users/TestLaboratory_V1_User_1?format=json", pyBackendIp, pyPort), nil)
- req.Header.Add("accessToken", accessToken)
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println(err)
- fmt.Println(resp.StatusCode)
- response.Fail(c, nil, "无法连接Python_Backend权限认证服务器")
- c.Abort()
- return
- }
- defer resp.Body.Close()
- body, _ := ioutil.ReadAll(resp.Body)
- res := string(body)
- if strings.Contains(res, "username") {
- } else {
- response.Fail(c, nil, "权限不足")
- c.Abort()
- return
- }
- }
- }
|