|
@@ -0,0 +1,49 @@
|
|
|
+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", pyBackendIp, pyPort), nil)
|
|
|
+ req.Header.Add("accessToken", accessToken)
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil || resp.StatusCode != 200 {
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|