mirror of
https://github.com/tiger1103/gfast.git
synced 2026-09-21 10:12:59 +00:00
框架升级,验证码优化,token redis存储
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"gfast/boot"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
type Index struct{}
|
||||
|
||||
func (c *Index) Index(r *ghttp.Request) {
|
||||
resp := boot.GfToken.GetTokenData(r)
|
||||
r.Response.Write("hello Index-", gconv.Map(resp.Get("data"))["user_nickname"])
|
||||
}
|
||||
+12
-2
@@ -6,16 +6,26 @@ import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
var GfToken *gtoken.GfToken
|
||||
|
||||
func init() {
|
||||
g.Server().SetPort(8200)
|
||||
g.Server().AddStaticPath("/public", g.Cfg().Get("server.ServerRoot").(string))
|
||||
// 启动gtoken
|
||||
gtoken := >oken.GfToken{
|
||||
GfToken = >oken.GfToken{
|
||||
CacheMode: int8(g.Cfg().Get("gToken.CacheMode").(float64)),
|
||||
CacheKey: g.Cfg().Get("gToken.CacheKey").(string),
|
||||
Timeout: int(g.Cfg().Get("gToken.Timeout").(float64)),
|
||||
MaxRefresh: int(g.Cfg().Get("gToken.MaxRefresh").(float64)),
|
||||
TokenDelimiter: g.Cfg().Get("gToken.TokenDelimiter").(string),
|
||||
EncryptKey: []byte(g.Cfg().Get("gToken.EncryptKey").(string)),
|
||||
AuthFailMsg: g.Cfg().Get("gToken.AuthFailMsg").(string),
|
||||
MultiLogin: g.Cfg().Get("gToken.MultiLogin").(bool),
|
||||
LoginPath: "/sysLogin/login",
|
||||
LoginBeforeFunc: utils.AdminLogin,
|
||||
LogoutPath: "/sysLogin/logout",
|
||||
AuthPaths: g.SliceStr{"/system/*"},
|
||||
LogoutBeforeFunc: utils.AdminLoginOut,
|
||||
}
|
||||
gtoken.Start()
|
||||
GfToken.Start()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# 数据库连接
|
||||
[database]
|
||||
link = "mysql:root:123456@tcp(127.0.0.1:3306)/gfast"
|
||||
|
||||
#web服务器配置
|
||||
[server]
|
||||
Address = ":8080"
|
||||
@@ -13,3 +14,25 @@
|
||||
SessionPath = "./data/session"
|
||||
SessionMaxAge = "24h"
|
||||
DumpRouterMap = true
|
||||
|
||||
|
||||
# Redis数据库配置
|
||||
[redis]
|
||||
default = "127.0.0.1:6379,1"
|
||||
|
||||
#jwt配置
|
||||
[gToken]
|
||||
CacheMode = 2
|
||||
CacheKey = "GToken:"
|
||||
Timeout = 0
|
||||
MaxRefresh = 0
|
||||
TokenDelimiter="_"
|
||||
EncryptKey = "koi29a83idakguqjq29asd9asd8a7jhq"
|
||||
AuthFailMsg = "登录超时,请重新登录"
|
||||
MultiLogin = true
|
||||
|
||||
#单例日志配置 g.Log(单例名称)获取Logger单例对象时
|
||||
[logger]
|
||||
path = "./data/log/run_log"
|
||||
level = "all"
|
||||
stdout = true
|
||||
@@ -3,7 +3,7 @@ module gfast
|
||||
require (
|
||||
github.com/casbin/casbin/v2 v2.1.2
|
||||
github.com/goflyfox/gtoken v1.3.9
|
||||
github.com/gogf/gf v1.10.1
|
||||
github.com/gogf/gf v1.11.2
|
||||
github.com/mojocn/base64Captcha v1.2.2
|
||||
)
|
||||
|
||||
|
||||
@@ -7,11 +7,12 @@ import (
|
||||
"github.com/gogf/gf/encoding/gbase64"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/util/grand"
|
||||
"github.com/gogf/gf/util/gvalid"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
const AESPublicKey = "HqmP1KLMuz09Q0Bu"
|
||||
const adminCbcPublicKey = "HqmP1KLMuz09Q0Bu"
|
||||
|
||||
//获取验证码
|
||||
func GetVerifyImg() (idKeyC string, base64stringC string) {
|
||||
@@ -33,7 +34,7 @@ func GetVerifyImg() (idKeyC string, base64stringC string) {
|
||||
//创建字符公式验证码.
|
||||
//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
|
||||
var capC base64Captcha.CaptchaInterface
|
||||
idKeyC, capC = base64Captcha.GenerateCaptcha("8nM77YhE2xOvU6GMQ33A", configC)
|
||||
idKeyC, capC = base64Captcha.GenerateCaptcha(grand.Str(20), configC)
|
||||
//以base64编码
|
||||
base64stringC = base64Captcha.CaptchaWriteToBase64Encoding(capC)
|
||||
return idKeyC, base64stringC
|
||||
@@ -41,7 +42,7 @@ func GetVerifyImg() (idKeyC string, base64stringC string) {
|
||||
|
||||
//AdminLogin 后台用户登陆验证
|
||||
func AdminLogin(r *ghttp.Request) (string, interface{}) {
|
||||
data := r.GetPostMapStrStr()
|
||||
data := r.GetFormMapStrStr()
|
||||
rules := map[string]string{
|
||||
"idValueC": "required",
|
||||
"username": "required",
|
||||
@@ -60,7 +61,7 @@ func AdminLogin(r *ghttp.Request) (string, interface{}) {
|
||||
response.JsonExit(r, response.ErrorCode, "验证码输入错误")
|
||||
}
|
||||
|
||||
if err, user := user_service.SignIn(data["username"], EncryptCBC(data["password"]), r.Session); err != nil {
|
||||
if err, user := user_service.SignIn(data["username"], EncryptCBC(data["password"], adminCbcPublicKey), r.Session); err != nil {
|
||||
response.JsonExit(r, response.NotAcceptableCode, err.Error())
|
||||
} else {
|
||||
return data["username"], user
|
||||
@@ -74,8 +75,8 @@ func AdminLoginOut(r *ghttp.Request) bool {
|
||||
}
|
||||
|
||||
//字符串加密
|
||||
func EncryptCBC(plainText string) string {
|
||||
key := []byte(AESPublicKey)
|
||||
func EncryptCBC(plainText, publicKey string) string {
|
||||
key := []byte(publicKey)
|
||||
b, e := gaes.EncryptCBC([]byte(plainText), key, key)
|
||||
if e != nil {
|
||||
glog.Error(e.Error())
|
||||
@@ -85,8 +86,8 @@ func EncryptCBC(plainText string) string {
|
||||
}
|
||||
|
||||
//字符串解密
|
||||
func DecryptCBC(plainText string) string {
|
||||
key := []byte(AESPublicKey)
|
||||
func DecryptCBC(plainText, publicKey string) string {
|
||||
key := []byte(publicKey)
|
||||
plainTextByte, e := gbase64.DecodeString(plainText)
|
||||
if e != nil {
|
||||
glog.Error(e.Error())
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 基本事件回调使用
|
||||
p := "/:name/info/{uid}"
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE : func(r *ghttp.Request){ glog.Println(ghttp.HOOK_BEFORE_SERVE) },
|
||||
ghttp.HOOK_AFTER_SERVE : func(r *ghttp.Request){ glog.Println(ghttp.HOOK_AFTER_SERVE) },
|
||||
ghttp.HOOK_BEFORE_OUTPUT : func(r *ghttp.Request){ glog.Println(ghttp.HOOK_BEFORE_OUTPUT) },
|
||||
ghttp.HOOK_AFTER_OUTPUT : func(r *ghttp.Request){ glog.Println(ghttp.HOOK_AFTER_OUTPUT) },
|
||||
})
|
||||
s.BindHandler(p, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.Get("uid"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
+2
-1
@@ -12,7 +12,8 @@ func init() {
|
||||
s.BindMiddleware("/sysLogin/logout", MiddlewareCORS)
|
||||
group := s.Group("/")
|
||||
group.Middleware(MiddlewareCORS)
|
||||
//systemGroup := group.Group("/system")
|
||||
sysLoginGroup := group.Group("/sysLogin")
|
||||
sysLoginGroup.ALL("/public", new(admin.Public))
|
||||
systemGroup := group.Group("/system")
|
||||
systemGroup.ALL("/index", new(admin.Index))
|
||||
}
|
||||
|
||||
+19
-2
@@ -17,11 +17,28 @@ import (
|
||||
|
||||
func TestDemo(t *testing.T) {
|
||||
//t.Run("demo1" ,Demo1)
|
||||
//t.Run("Adapters_test", Adapters)
|
||||
t.Run("Adapters_test", Adapters)
|
||||
//t.Run("CaptchaDemo", CaptchaDemo)
|
||||
//t.Run("CaptchaVerify", CaptchaVerify)
|
||||
//t.Run("GTokenTest", GTokenTest)
|
||||
t.Run("CbcEncrypt", CbcEncrypt)
|
||||
//t.Run("CbcEncrypt", CbcEncrypt)
|
||||
}
|
||||
|
||||
func HookDemo(t *testing.T) {
|
||||
// 基本事件回调使用
|
||||
p := "/:name/info/{uid}"
|
||||
s := g.Server()
|
||||
s.BindHookHandlerByMap(p, map[string]ghttp.HandlerFunc{
|
||||
ghttp.HOOK_BEFORE_SERVE: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_SERVE) },
|
||||
ghttp.HOOK_AFTER_SERVE: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_SERVE) },
|
||||
ghttp.HOOK_BEFORE_OUTPUT: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_BEFORE_OUTPUT) },
|
||||
ghttp.HOOK_AFTER_OUTPUT: func(r *ghttp.Request) { glog.Println(ghttp.HOOK_AFTER_OUTPUT) },
|
||||
})
|
||||
s.BindHandler(p, func(r *ghttp.Request) {
|
||||
r.Response.Write("用户:", r.Get("name"), ", uid:", r.Get("uid"))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
func CbcEncrypt(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user