master
wubin 5 years ago
parent 3d334ba2cb
commit ded9ce1380

@ -1,32 +0,0 @@
package CommonUtil
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
)
var swaggerFile = "docs/swagger.json"
/**
swaggerjson
2020-03-16
*/
func ReadSwaggerJson() interface{} {
//读取doc目录下的swagger.json
f, err := os.Open(swaggerFile)
if err != nil {
log.Fatal("读取swagger.json失败")
return nil
}
content, _ := ioutil.ReadAll(f)
var jsonBody interface{}
err = json.Unmarshal(content, &jsonBody)
if err != nil {
fmt.Println("ERROR: ", err.Error())
return nil
}
return jsonBody
}

@ -1,7 +0,0 @@
package CookieUtil
import "github.com/gin-gonic/gin"
func (c *gin.Context) GetString() string {
}

@ -1,178 +0,0 @@
package SsoUtil
import (
"dsBigData/Utils/ConfigUtil"
"dsBigData/Utils/RedisUtil"
"encoding/base64"
"github.com/gin-gonic/gin"
"github.com/gomodule/redigo/redis"
"github.com/sirupsen/logrus"
"strconv"
"strings"
)
/**
2020-02-22
*/
func IsAuthority(c *gin.Context) (bool, string) {
//检查Header头中是不是存在ds_sso_sessionid,就是说如果客户端不支持Cookie(比如手机)可以在Header中构建ds_sso_sessionid
sessionId := ""
//从 Header中获取会话ID
sessionId = c.Request.Header.Get(ConfigUtil.SessionId)
//如果没有找到,就到 cookie里继续寻找
if len(sessionId) == 0 {
cookie, e := c.Request.Cookie(ConfigUtil.SessionId)
if e == nil { //如果成功获取到的话
sessionId = cookie.Value
}
}
//如果两个位置都没有找到
if len(sessionId) == 0 {
return false, ""
} else { //有这个值,判断一下是不是正确
_map := RedisUtil.HGETALL(ConfigUtil.SessionId + "_" + sessionId)
if len(_map) == 0 {
//清除非法cookie资料参考https://www.tizi365.com/archives/273.html
c.SetCookie(ConfigUtil.SessionId, "", -1, "/", "", false, true)
return false, sessionId
} else {
return true, sessionId
}
}
}
/**
2020-02-25
*/
func IsLoginStatus(username string) (bool, string) {
var value = ""
var r = true
value, err := redis.String(RedisUtil.GET(ConfigUtil.SearchSessionIdPrefix + username))
if err != nil {
r = false
}
return r, value
}
/**
2020-02-22
*/
func WriteSsoInfo(c *gin.Context, device string, identityId int64, personId int64, username string, personName string, dsSsoSessionid string) {
//下面应该是双向的redis存储1通过登录名能找到对应的会话ID2通过会话ID可以解析出是哪种设备哪个身份哪个人员ID
//一、写入登录名与会话的对应关系,由四部分组成,前缀+设备类型+身份+人员编号,通过这个键值可以查找到对应的会话
SearchSsoKey := ConfigUtil.SearchSessionIdPrefix + "_" + device + "_" + strconv.FormatInt(identityId, 10) + "_" + strconv.FormatInt(personId, 10)
//写入redis
RedisUtil.SET(SearchSsoKey, ConfigUtil.SessionId+"_"+dsSsoSessionid)
//加上过期时间
RedisUtil.EXPIRE(SearchSsoKey)
//二、写入SESSION_ID
_map := make(map[string]interface{})
_map["device"] = device
_map["identity_id"] = identityId
_map["person_id"] = personId
_map["username"] = username
_map["person_name"] = personName
RedisUtil.HMSET(ConfigUtil.SessionId+"_"+dsSsoSessionid, _map)
//加上过期时间
RedisUtil.EXPIRE(ConfigUtil.SessionId + "_" + dsSsoSessionid)
//三、写入主域的COOKIE值
c.SetCookie(ConfigUtil.SessionId, dsSsoSessionid, 0, "/", "", false, true)
}
/**
: cookie+redis
2020-02-22
*/
func ClearSsoInfo(sessionId string, c *gin.Context) string {
//一、删除人员与 sessionId的映射关系
DeletePersonSessionReleation(sessionId)
//二、删除redis中的会话id
DeleteSessionId(sessionId)
//三、清除cookie
deleteSsoCookie(c)
return sessionId
}
/**
2020-02-25
*/
func DeletePersonSessionReleation(sessionId string) {
//一、删除人员与 sessionId的映射关系
var identityId = -1
var personId = -1
var deviceId = -1
deviceId, identityId, personId = AnalyzeSessionId(sessionId) //通过会话ID获取到人员的信息
//删除人员与会话的对应关系
SearchSsoKey := ConfigUtil.SearchSessionIdPrefix + "_" + strconv.Itoa(deviceId) + "_" + strconv.Itoa(identityId) + "_" + strconv.Itoa(personId)
RedisUtil.DEL(SearchSsoKey)
}
/**
SessionIdID
2020-02-25
*/
func AnalyzeSessionId(sessionId string) (int, int, int) {
var identityId = -1
var personId = -1
var deviceId = -1
r, err := base64.RawURLEncoding.DecodeString(sessionId)
if err != nil {
logrus.Errorln(err)
} else {
s := string(r)
array := strings.Split(s, "_")
if len(array) > 1 {
deviceId, _ = strconv.Atoi(array[0])
identityId, _ = strconv.Atoi(array[1])
personId, _ = strconv.Atoi(array[2])
}
}
return deviceId, identityId, personId
}
/**
redisid
2020-02-25
*/
func DeleteSessionId(sessionId string) {
//删除redis中的记录
RedisUtil.DEL(ConfigUtil.SessionId + "_" + sessionId)
}
/**
cookie
2020-02-22
*/
func ReadSsoCookie(c *gin.Context) string {
cookie, e := c.Request.Cookie(ConfigUtil.SessionId)
if e == nil {
return cookie.Value
} else {
return ""
}
}
/**
cookie
2020-02-22
*/
func deleteSsoCookie(c *gin.Context) {
c.SetCookie(ConfigUtil.SessionId, "", -1, "/", "", false, true)
}

Binary file not shown.

@ -3,7 +3,6 @@ module dsBigData
go 1.14
require (
github.com/0x5010/grpcp v0.0.0-20180912032145-6d4772332891
github.com/Chronokeeper/anyxml v0.0.0-20160530174208-54457d8e98c6 // indirect
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
github.com/CloudyKit/jet v2.1.2+incompatible // indirect

Loading…
Cancel
Save