验证码存redis
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
|||||||
"git.rosy.net.cn/jx-print/model"
|
"git.rosy.net.cn/jx-print/model"
|
||||||
"git.rosy.net.cn/jx-print/utils"
|
"git.rosy.net.cn/jx-print/utils"
|
||||||
"github.com/dchest/captcha"
|
"github.com/dchest/captcha"
|
||||||
"github.com/gin-contrib/sessions"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -93,10 +92,9 @@ func callFunc(c *gin.Context, worker func() (retVal interface{}, errCode string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func captchaVerify(c *gin.Context, code string) bool {
|
func captchaVerify(c *gin.Context, code string) bool {
|
||||||
session := sessions.Default(c)
|
//session := sessions.Default(c)
|
||||||
if captchaId := session.Get(c.ClientIP() + model.SessionKey); captchaId != nil {
|
if captchaId := utils.GetKet(c.ClientIP() + model.SessionKey); captchaId != nil {
|
||||||
session.Delete(c.ClientIP() + model.SessionKey)
|
utils.DelKey(c.ClientIP() + model.SessionKey)
|
||||||
_ = session.Save()
|
|
||||||
if captcha.VerifyString(captchaId.(string), code) {
|
if captcha.VerifyString(captchaId.(string), code) {
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
@@ -105,6 +103,17 @@ func captchaVerify(c *gin.Context, code string) bool {
|
|||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
//if captchaId := session.Get(c.ClientIP() + model.SessionKey); captchaId != nil {
|
||||||
|
// session.Delete(c.ClientIP() + model.SessionKey)
|
||||||
|
// _ = session.Save()
|
||||||
|
// if captcha.VerifyString(captchaId.(string), code) {
|
||||||
|
// return true
|
||||||
|
// } else {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
//} else {
|
||||||
|
// return false
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildErrJson(c *gin.Context, err error) {
|
func buildErrJson(c *gin.Context, err error) {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"git.rosy.net.cn/jx-print/globals"
|
"git.rosy.net.cn/jx-print/globals"
|
||||||
"git.rosy.net.cn/jx-print/model"
|
"git.rosy.net.cn/jx-print/model"
|
||||||
"github.com/dchest/captcha"
|
"github.com/dchest/captcha"
|
||||||
"github.com/gin-contrib/sessions"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -65,11 +64,13 @@ func Captcha(c *gin.Context, length ...int) {
|
|||||||
h = length[2]
|
h = length[2]
|
||||||
}
|
}
|
||||||
captchaId := captcha.NewLen(l)
|
captchaId := captcha.NewLen(l)
|
||||||
session := sessions.Default(c)
|
//session := sessions.Default(c)
|
||||||
session.Set(c.ClientIP()+model.SessionKey, captchaId)
|
//session.Set(c.ClientIP()+model.SessionKey, captchaId)
|
||||||
_ = session.Save()
|
//_ = session.Save()
|
||||||
|
if err := SetKey(c.ClientIP()+model.SessionKey, captchaId, time.Minute*5); err == nil {
|
||||||
_ = Serve(c.Writer, c.Request, captchaId, ".png", "zh", false, w, h)
|
_ = Serve(c.Writer, c.Request, captchaId, ".png", "zh", false, w, h)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
|
func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
|
||||||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||||
|
|||||||
Reference in New Issue
Block a user