You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package Handler
import (
"bufio"
"dsSdsf/Utils/CommonUtil"
"dsSdsf/Utils/RedisUtil"
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"strings"
)
var WhiteArray []string
// 初始化白名单
func init() {
configIniFile := "./Config/White.txt"
if !CommonUtil.Exists(configIniFile) {
configIniFile = "/usr/local/dsMin/dsBaseWeb/Config/White.txt"
}
f, err := os.Open(configIniFile)
if err != nil {
fmt.Println("没有找到白名单文件!")
}
buf := bufio.NewReader(f)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
if strings.Index(line, "#") < 0 && len(line) > 0 {
WhiteArray = append(WhiteArray, line)
}
if err != nil || io.EOF == err {
break
}
}
}
func LoginHandler() gin.HandlerFunc {
return func(c *gin.Context) {
requestUri := c.Request.RequestURI
//白名单中的放行
for i := 0; i < len(WhiteArray); i++ {
if strings.Index(requestUri, WhiteArray[i]) >= 0 {
//放行~
c.Next()
return
}
}
//是否需要登录默认为true需要登录
needLoginFlag := true
//获取cookie中的token
cookieToken, _ := c.Request.Cookie("token")
if cookieToken != nil {
_, error := RedisUtil.GET(cookieToken.Value)
if error == nil {
needLoginFlag = false
}
}
if needLoginFlag {
//跳转到登录页面
c.Redirect(http.StatusMovedPermanently, "/sdsf/login/doLogin?redirect_uri="+requestUri)
} else {
//将redis中的token有效期重置
RedisUtil.EXPIRE(cookieToken.Value, 1800)
c.Next()
return
}
}
}