Browse Source

增加应答加密配置

kobe6258 8 months ago
parent
commit
5307148385
2 changed files with 120 additions and 15 deletions
  1. 101 12
      controllers/base_controller.go
  2. 19 3
      controllers/list_contoller.go

+ 101 - 12
controllers/base_controller.go

@@ -9,9 +9,11 @@ import (
 	"eta/eta_mini_ht_api/common/exception"
 	"eta/eta_mini_ht_api/common/http"
 	"eta/eta_mini_ht_api/common/utils/auth"
+	stringUtils "eta/eta_mini_ht_api/common/utils/string"
 	"fmt"
 	"github.com/beego/beego/v2/server/web"
 	"net/url"
+	"strings"
 )
 
 var (
@@ -63,7 +65,16 @@ func (b *BaseController) JsonResult(status int, errCode int, errMsg string, msg
 		Success: success}
 
 	b.Ctx.Output.SetStatus(status)
-
+	//content, err := json.Marshal(retData)
+	//if err != nil {
+	//	logger.Error("加密失败")
+	//} else {
+	//	if htConfig.NeedEncode() {
+	//		content = auth.DesBase64Encrypt(content, htConfig.GetDesCode())
+	//	}
+	//}
+	//fmt.Printf("%s", content)
+	//b.Data["json"] = content
 	b.Data["json"] = retData
 	b.ServeJSON()
 }
@@ -118,6 +129,10 @@ func (b *BaseController) Finish() {
 		return
 	}
 	baseRes := b.Data["json"].(BaseResponse)
+	content, err := json.Marshal(baseRes)
+	if err != nil {
+		logger.Error("apiRequest:[应答json格式化失败:%s]", err)
+	}
 	if !baseRes.Success {
 		logger.Info("apiRequest:[异常提醒:%v接口:URI:%v;ErrMsg:&v;Msg:%v]", b.Ctx.Input.URI(), baseRes.ErrMsg, baseRes.Msg)
 	} else {
@@ -125,19 +140,9 @@ func (b *BaseController) Finish() {
 			logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, b.Ctx.Input.URI())
 			return
 		} else {
-			logger.Info("apiRequest:[uri:%s, resData:%s, ip:%s]", b.Ctx.Input.URI(), baseRes.Data)
+			logger.Info("apiRequest:[uri:%s, resData:%s, ip:%s]", b.Ctx.Input.URI(), content)
 		}
 	}
-	content, err := json.Marshal(baseRes)
-	if err != nil {
-		logger.Error("加密失败")
-	} else {
-		if htConfig.NeedEncode() {
-			content = auth.DesBase64Encrypt(content, htConfig.GetDesCode())
-		}
-	}
-	fmt.Printf("%s", content)
-	b.Data["json"] = content
 }
 
 type RequestInfo struct {
@@ -146,3 +151,87 @@ type RequestInfo struct {
 	Method    string `json:"method"`
 	Params    string `json:"params"`
 }
+
+func (b *BaseController) ServeJSON(encoding ...bool) {
+	var (
+		hasIndent   = false
+		hasEncoding = false
+	)
+	if web.BConfig.RunMode == web.PROD {
+		hasIndent = false
+	}
+	if len(encoding) > 0 && encoding[0] == true {
+		hasEncoding = true
+	}
+	b.JSON(b.Data["json"], hasIndent, hasEncoding)
+}
+
+func (b *BaseController) JSON(data interface{}, hasIndent bool, coding bool) error {
+	b.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
+	//desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
+	//c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
+	// 设置Cookie为HTTPOnly
+	b.Ctx.SetCookie("", "", -1, "/", "", false, true, "")
+
+	var content []byte
+	var err error
+	if hasIndent {
+		content, err = json.MarshalIndent(data, "", "  ")
+	} else {
+		content, err = json.Marshal(data)
+	}
+	ip := b.Ctx.Input.IP()
+	requestBody, err := url.QueryUnescape(string(b.Ctx.Input.RequestBody))
+	if err != nil {
+		logger.Info("apiRequest:[err:%s]", err.Error())
+	}
+	b.logUri(content, requestBody, ip)
+	if htConfig.NeedEncode() {
+		content = auth.DesBase64Encrypt(content, htConfig.GetDesCode())
+		content = []byte(`"` + string(content) + `"`)
+	}
+	if coding {
+		content = []byte(stringUtils.StringsToJSON(string(content)))
+	}
+	return b.Ctx.Output.Body(content)
+}
+
+func (b *BaseController) logUri(respContent []byte, requestBody, ip string) {
+	authorization := ""
+	method := b.Ctx.Input.Method()
+	uri := b.Ctx.Input.URI()
+	if method != "HEAD" {
+		if method == "POST" || method == "GET" {
+			authorization = b.Ctx.Input.Header("authorization")
+			if authorization == "" {
+				authorization = b.Ctx.Input.Header("Authorization")
+			}
+			if authorization == "" {
+				newAuthorization := b.GetString("authorization")
+				if newAuthorization != `` {
+					authorization = "authorization=" + newAuthorization
+				} else {
+					newAuthorization = b.GetString("Authorization")
+					authorization = "authorization=" + newAuthorization
+				}
+			} else {
+				if strings.Contains(authorization, ";") {
+					authorization = strings.Replace(authorization, ";", "$", 1)
+				}
+			}
+			if authorization == "" {
+				strArr := strings.Split(uri, "?")
+				for k, v := range strArr {
+					fmt.Println(k, v)
+				}
+				if len(strArr) > 1 {
+					authorization = strArr[1]
+					authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
+					fmt.Println(authorization)
+				}
+			}
+		}
+	}
+	logger.Info("apiRequest:[uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s]", b.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 19 - 3
controllers/list_contoller.go

@@ -61,6 +61,22 @@ func (l *ListController) CheckMediaType(mediaType string) bool {
 	return false
 }
 
-func (l *ListController) Finish() {
-	l.PageInfo.Reset()
-}
+//func (l *ListController) Finish() {
+//	l.PageInfo.Reset()
+//	runMode := web.BConfig.RunMode
+//	if l.Data["json"] == nil {
+//		logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, l.Ctx.Input.URI())
+//		return
+//	}
+//	baseRes := l.Data["json"].(BaseResponse)
+//	if !baseRes.Success {
+//		logger.Info("apiRequest:[异常提醒:%v接口:URI:%v;ErrMsg:&v;Msg:%v]", l.Ctx.Input.URI(), baseRes.ErrMsg, baseRes.Msg)
+//	} else {
+//		if baseRes.Data == nil {
+//			logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, l.Ctx.Input.URI())
+//			return
+//		} else {
+//			logger.Info("apiRequest:[uri:%s, resData:%s, ip:%s]", l.Ctx.Input.URI(), baseRes.Data)
+//		}
+//	}
+//}