SM2 兴业登录数据解密

This commit is contained in:
wuchao 2024-06-11 18:47:02 +08:00
parent 47f7a8324f
commit acc1108cec
11 changed files with 133 additions and 17 deletions

View File

@ -4,4 +4,5 @@ const (
TOKEN_PRE = "player_token_" TOKEN_PRE = "player_token_"
TOKEN_Admin = "Admin_token_" TOKEN_Admin = "Admin_token_"
ADMIN_V1 = "/admin/api/v1" ADMIN_V1 = "/admin/api/v1"
FRONT_API_V1 = "/v1"
) )

View File

@ -0,0 +1,13 @@
package front
import (
"github.com/gin-gonic/gin"
"qteam/app/http/controllers"
"qteam/app/http/entities/front"
"qteam/app/services"
)
func XyLogin(c *gin.Context) {
var request = controllers.GetRequest(c).(*front.LoginRequest)
services.XyYouKuLogin(request)
}

View File

@ -0,0 +1,28 @@
package front
import (
"qteam/app/models/usersmodel"
"time"
)
type LoginRequest struct {
Data string `form:"data" validate:"required"`
}
type XyDecryptData struct {
Mobile string `json:"phone"`
CerNo string `json:"cerNo"`
Time string `json:"time"`
}
type LoginResponse struct {
Token string `json:"token"`
}
func (this *XyDecryptData) RequestToDb() (user usersmodel.Users) {
user.Phone = this.Mobile
user.Name = this.Mobile
user.CreateTime = time.Now()
user.UpdateTime = time.Now()
return
}

View File

@ -1,5 +1,13 @@
package requestmapping package requestmapping
import (
"qteam/app/constants/common"
"qteam/app/http/entities/front"
)
var FrontRequestMap = map[string]func() interface{}{ var FrontRequestMap = map[string]func() interface{}{
"/v1/GenerateSm2Key": func() interface{} { return new(struct{}) }, "/v1/GenerateSm2Key": func() interface{} { return new(struct{}) },
// 兴业银行登录
common.FRONT_API_V1 + "/xy/login": func() interface{} { return new(front.LoginRequest) },
} }

View File

@ -14,8 +14,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/qit-team/snow-core/http/middleware" "github.com/qit-team/snow-core/http/middleware"
"github.com/qit-team/snow-core/log/logger" "github.com/qit-team/snow-core/log/logger"
"github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
) )
// api路由配置 // api路由配置
@ -46,14 +44,6 @@ func RegisterRoute(router *gin.Engine) {
//api版本 //api版本
v1 := router.Group("/v1", middlewares.ValidateRequest()) v1 := router.Group("/v1", middlewares.ValidateRequest())
{ {
v1.GET("/GenerateSm2Key", front.GetGenerateSm2Key) v1.POST("/xy/login", front.XyLogin)
} }
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
//router.GET("/hello", controllers.HelloHandler)
//router.GET("/create", controllers.HelloCreateHandler)
//router.GET("/update", controllers.UpdateHandler)
//router.GET("/delete", controllers.DeleteHandler)
//router.GET("/query", controllers.QueryHandler)
} }

View File

@ -98,7 +98,7 @@ func decryptLoc(publicKeyStr, privateKeyStr, cipherText string) (string, error)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
resultStr := hex.EncodeToString(decrypt) resultStr := string(decrypt)
fmt.Println("解密后的字符串:", resultStr) fmt.Println("解密后的字符串:", resultStr)
return resultStr, nil return resultStr, nil
} }

View File

@ -0,0 +1,32 @@
package services
import (
"qteam/app/constants/errorcode"
"qteam/app/http/entities/front"
"qteam/app/models/usersmodel"
)
func XyYouKuLogin(req *front.LoginRequest) (code int, user usersmodel.Users) {
var err error
info := DecryptXyData(req.Data)
if info.Mobile == "" {
code = errorcode.ParamError
return
}
var addUser = false
var session = usersmodel.GetInstance().GetDb().Where("phone = ?", info.Mobile)
_, err = session.Get(&user)
code = errorcode.Success
if user.Id < 1 {
addUser = true
}
if addUser {
user = info.RequestToDb()
_, err = usersmodel.GetInstance().GetDb().Insert(&user)
}
if err != nil {
code = handErr(err)
}
return code, user
}

View File

@ -0,0 +1,29 @@
package services
import (
"encoding/base64"
"encoding/json"
"fmt"
"qteam/app/http/entities/front"
)
func DecryptXyData(data string) (info front.XyDecryptData) {
// 解码
decodedBytes, err := base64.StdEncoding.DecodeString(data)
if err != nil {
fmt.Println("解码错误:", err)
return
}
fmt.Println("解码后的数据:", string(decodedBytes))
// 使用Hex.EncodeToString将字节切片转换为十六进制字符串
decrypt, err := SM2Decrypt(string(decodedBytes))
if err != nil {
return
}
err = json.Unmarshal([]byte(decrypt), &info)
if err != nil {
fmt.Println("解析JSON错误:", err)
return
}
return
}

15
app/services/common.go Normal file
View File

@ -0,0 +1,15 @@
package services
import (
"qteam/app/constants/errorcode"
"qteam/app/utils"
)
func handErr(err error) int {
if err != nil {
utils.Log(nil, "sys err", err.Error())
return errorcode.SystemError
} else {
return errorcode.Success
}
}

View File

@ -57,11 +57,11 @@ func Bootstrap(conf *config.Config) (err error) {
//注册应用停止时调用的关闭服务 //注册应用停止时调用的关闭服务
close.MultiRegister(db.Pr, redis.Pr) close.MultiRegister(db.Pr, redis.Pr)
////Register //Register
//_, err = utils.GetNaocosClient().RegisterInstance(vo.RegisterInstanceParam{ //_, err = utils.GetNaocosClient().RegisterInstance(vo.RegisterInstanceParam{
// Ip: utils.GetHostIp(), // Ip: utils.GetHostIp(),
// Port: uint64(conf.Api.Port), // Port: uint64(conf.Api.Port),
// ServiceName: "snow", // ServiceName: conf.ServiceName,
// GroupName: "group-d", // GroupName: "group-d",
// ClusterName: "cluster-snow", // ClusterName: "cluster-snow",
// Weight: 10, // Weight: 10,