mirror of
https://github.com/tiger1103/gfast.git
synced 2026-09-21 10:12:59 +00:00
gtoken整合完成,添加字符串加密功能
This commit is contained in:
@@ -1,42 +1,14 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"gfast/app/service/user_service"
|
||||
"gfast/library/response"
|
||||
"gfast/library/utils"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/util/gvalid"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
type Public struct{}
|
||||
|
||||
//Login 用户登陆验证
|
||||
func (p *Public) Login(r *ghttp.Request) {
|
||||
data := r.GetPostMapStrStr()
|
||||
//判断验证码是否正确
|
||||
if !base64Captcha.VerifyCaptchaAndIsClear(data["idKeyC"], data["idValueC"], true) {
|
||||
response.JsonExit(r, response.ErrorCode, "验证失败")
|
||||
}
|
||||
rules := map[string]string{
|
||||
"username": "required",
|
||||
"password": "required",
|
||||
}
|
||||
msgs := map[string]interface{}{
|
||||
"username": "账号不能为空",
|
||||
"password": "密码不能为空",
|
||||
}
|
||||
if e := gvalid.CheckMap(data, rules, msgs); e != nil {
|
||||
response.JsonExit(r, response.ErrorCode, e.String())
|
||||
}
|
||||
if err := user_service.SignIn(data["username"], data["password"], r.Session); err != nil {
|
||||
response.RJson(r, response.NotAcceptableCode, err.Error())
|
||||
} else {
|
||||
response.SusJson(false, r, "ok")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Public) Verify(r *ghttp.Request) {
|
||||
idKeyC, base64stringC := utils.GetVerifyImg()
|
||||
response.RJson(r, 200, "ok", g.MapStrStr{"idKeyC": idKeyC, "base64stringC": base64stringC})
|
||||
|
||||
@@ -8,14 +8,13 @@ import (
|
||||
)
|
||||
|
||||
// 用户登录,成功返回用户信息,否则返回nil; passport应当会md5值字符串
|
||||
func SignIn(username, password string, session *ghttp.Session) error {
|
||||
qxkjUser, err := qxkj_user.Model.Where("username=? and user_password=?", username, password).One()
|
||||
func SignIn(username, password string, session *ghttp.Session) (error, *qxkj_user.QxkjUser) {
|
||||
qxkjUser, err := qxkj_user.Model.Where("user_name=? and user_password=?", username, password).One()
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
return err, nil
|
||||
}
|
||||
if qxkjUser == nil {
|
||||
return errors.New("账号或密码错误")
|
||||
return errors.New("账号或密码错误"), nil
|
||||
}
|
||||
//session.Set(USER_SESSION_MARK, record)
|
||||
return nil
|
||||
return nil, qxkjUser
|
||||
}
|
||||
|
||||
+11
-1
@@ -1,11 +1,21 @@
|
||||
package boot
|
||||
|
||||
import (
|
||||
"gfast/library/utils"
|
||||
"github.com/goflyfox/gtoken/gtoken"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func init() {
|
||||
g.Server().SetPort(8200)
|
||||
g.Server().AddStaticPath("/public", g.Cfg().Get("server.ServerRoot").(string))
|
||||
|
||||
// 启动gtoken
|
||||
gtoken := >oken.GfToken{
|
||||
LoginPath: "/sysLogin/login",
|
||||
LoginBeforeFunc: utils.AdminLogin,
|
||||
LogoutPath: "/sysLogin/logout",
|
||||
AuthPaths: g.SliceStr{"/system/*"},
|
||||
LogoutBeforeFunc: utils.AdminLoginOut,
|
||||
}
|
||||
gtoken.Start()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package adapterUtils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -276,4 +276,4 @@ func rawDelete(a *Adapter, line CasbinRule) error {
|
||||
|
||||
_, err := db.Delete()
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,17 @@
|
||||
package utils
|
||||
|
||||
import "github.com/mojocn/base64Captcha"
|
||||
import (
|
||||
"gfast/app/service/user_service"
|
||||
"gfast/library/response"
|
||||
"github.com/gogf/gf/crypto/gaes"
|
||||
"github.com/gogf/gf/encoding/gbase64"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
"github.com/gogf/gf/util/gvalid"
|
||||
"github.com/mojocn/base64Captcha"
|
||||
)
|
||||
|
||||
const AESPublicKey = "HqmP1KLMuz09Q0Bu"
|
||||
|
||||
//获取验证码
|
||||
func GetVerifyImg() (idKeyC string, base64stringC string) {
|
||||
@@ -27,3 +38,45 @@ func GetVerifyImg() (idKeyC string, base64stringC string) {
|
||||
base64stringC = base64Captcha.CaptchaWriteToBase64Encoding(capC)
|
||||
return idKeyC, base64stringC
|
||||
}
|
||||
|
||||
//AdminLogin 后台用户登陆验证
|
||||
func AdminLogin(r *ghttp.Request) (string, interface{}) {
|
||||
data := r.GetPostMapStrStr()
|
||||
//判断验证码是否正确
|
||||
/*if !base64Captcha.VerifyCaptchaAndIsClear(data["idKeyC"], data["idValueC"], true) {
|
||||
response.JsonExit(r, response.ErrorCode, "验证码输入错误")
|
||||
}*/
|
||||
rules := map[string]string{
|
||||
"username": "required",
|
||||
"password": "required",
|
||||
}
|
||||
msgs := map[string]interface{}{
|
||||
"username": "账号不能为空",
|
||||
"password": "密码不能为空",
|
||||
}
|
||||
if e := gvalid.CheckMap(data, rules, msgs); e != nil {
|
||||
response.JsonExit(r, response.ErrorCode, e.String())
|
||||
}
|
||||
if err, user := user_service.SignIn(data["username"], EncryptCBC(data["password"]), r.Session); err != nil {
|
||||
response.JsonExit(r, response.NotAcceptableCode, err.Error())
|
||||
} else {
|
||||
return data["username"], user
|
||||
}
|
||||
return data["username"], nil
|
||||
}
|
||||
|
||||
//后台退出登陆
|
||||
func AdminLoginOut(r *ghttp.Request) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
//字符串加密
|
||||
func EncryptCBC(plainText string) string {
|
||||
key := []byte(AESPublicKey)
|
||||
b, e := gaes.EncryptCBC([]byte(plainText), key, key)
|
||||
if e != nil {
|
||||
glog.Error(e.Error())
|
||||
return ""
|
||||
}
|
||||
return gbase64.EncodeToString(b)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
// 统一路由注册.
|
||||
func init() {
|
||||
s := g.Server()
|
||||
s.BindMiddleware("/sysLogin/login", MiddlewareCORS)
|
||||
s.BindMiddleware("/sysLogin/logout", MiddlewareCORS)
|
||||
group := s.Group("/")
|
||||
group.Middleware(MiddlewareCORS)
|
||||
systemGroup := group.Group("/system")
|
||||
|
||||
+21
-10
@@ -2,10 +2,12 @@ package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gfast/library/utils"
|
||||
"gfast/library/adapterUtils"
|
||||
"github.com/casbin/casbin/v2"
|
||||
"github.com/casbin/casbin/v2/util"
|
||||
"github.com/goflyfox/gtoken/gtoken"
|
||||
"github.com/gogf/gf/crypto/gaes"
|
||||
"github.com/gogf/gf/encoding/gbase64"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/glog"
|
||||
@@ -18,7 +20,16 @@ func TestDemo(t *testing.T) {
|
||||
//t.Run("Adapters_test", Adapters)
|
||||
//t.Run("CaptchaDemo", CaptchaDemo)
|
||||
//t.Run("CaptchaVerify", CaptchaVerify)
|
||||
t.Run("GTokenTest", GTokenTest)
|
||||
//t.Run("GTokenTest", GTokenTest)
|
||||
t.Run("CbcEncrypt", CbcEncrypt)
|
||||
}
|
||||
|
||||
func CbcEncrypt(t *testing.T) {
|
||||
b, e := gaes.EncryptCBC([]byte("yxh123456"), []byte("HqmP1KLMuz09Q0Bu"), []byte("HqmP1KLMuz09Q0Bu"))
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
fmt.Println(gbase64.EncodeToString(b))
|
||||
}
|
||||
|
||||
func Demo1(t *testing.T) {
|
||||
@@ -122,7 +133,7 @@ func Adapters(t *testing.T) {
|
||||
testAutoSave(t, a)
|
||||
testSaveLoad(t, a)
|
||||
|
||||
a = initAdapterFormOptions(t, &utils.Adapter{
|
||||
a = initAdapterFormOptions(t, &adapterUtils.Adapter{
|
||||
DriverName: "mysql",
|
||||
DataSourceName: "root:123456@tcp(127.0.0.1:3306)/test2",
|
||||
})
|
||||
@@ -130,9 +141,9 @@ func Adapters(t *testing.T) {
|
||||
testSaveLoad(t, a)
|
||||
}
|
||||
|
||||
func initAdapterFormOptions(t *testing.T, adapter *utils.Adapter) *utils.Adapter {
|
||||
func initAdapterFormOptions(t *testing.T, adapter *adapterUtils.Adapter) *adapterUtils.Adapter {
|
||||
// Create an adapter
|
||||
a, _ := utils.NewAdapterFromOptions(adapter)
|
||||
a, _ := adapterUtils.NewAdapterFromOptions(adapter)
|
||||
// Initialize some policy in DB.
|
||||
initPolicy(t, a)
|
||||
// Now the DB has policy, so we can provide a normal use case.
|
||||
@@ -142,7 +153,7 @@ func initAdapterFormOptions(t *testing.T, adapter *utils.Adapter) *utils.Adapter
|
||||
return a
|
||||
}
|
||||
|
||||
func initPolicy(t *testing.T, a *utils.Adapter) {
|
||||
func initPolicy(t *testing.T, a *adapterUtils.Adapter) {
|
||||
// Because the DB is empty at first,
|
||||
// so we need to load the policy from the file adapter (.CSV) first.
|
||||
e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", "casbin_conf/rbac_policy.csv")
|
||||
@@ -179,9 +190,9 @@ func testGetPolicy(t *testing.T, e *casbin.Enforcer, res [][]string) {
|
||||
}
|
||||
}
|
||||
|
||||
func initAdapter(t *testing.T, driverName string, dataSourceName string) *utils.Adapter {
|
||||
func initAdapter(t *testing.T, driverName string, dataSourceName string) *adapterUtils.Adapter {
|
||||
// Create an adapter
|
||||
a, err := utils.NewAdapter(driverName, dataSourceName)
|
||||
a, err := adapterUtils.NewAdapter(driverName, dataSourceName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -195,7 +206,7 @@ func initAdapter(t *testing.T, driverName string, dataSourceName string) *utils.
|
||||
return a
|
||||
}
|
||||
|
||||
func testAutoSave(t *testing.T, a *utils.Adapter) {
|
||||
func testAutoSave(t *testing.T, a *adapterUtils.Adapter) {
|
||||
|
||||
// NewEnforcer() will load the policy automatically.
|
||||
e, err := casbin.NewEnforcer("casbin_conf/rbac_model.conf", a)
|
||||
@@ -237,7 +248,7 @@ func testAutoSave(t *testing.T, a *utils.Adapter) {
|
||||
testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}})
|
||||
}
|
||||
|
||||
func testSaveLoad(t *testing.T, a *utils.Adapter) {
|
||||
func testSaveLoad(t *testing.T, a *adapterUtils.Adapter) {
|
||||
// Initialize some policy in DB.
|
||||
initPolicy(t, a)
|
||||
// Note: you don't need to look at the above code
|
||||
|
||||
Reference in New Issue
Block a user