解决Cookie记住登录不生效的BUG

pull/219/merge
Minho 2018-03-09 10:55:50 +08:00
parent 7db72f2049
commit 2a9d8cc5df
3 changed files with 32 additions and 16 deletions

View File

@ -5,7 +5,8 @@ runmode = dev
sessionon = true
sessionname = mindoc_id
copyrequestbody = true
#系统完整URL(http://doc.iminho.me),如果该项不设置,会从请求头中获取地址。
baseurl=
#默认Session生成Key的秘钥
beegoserversessionkey=123456

View File

@ -25,11 +25,6 @@ func (c *AccountController) Login() {
c.Prepare()
c.TplName = "account/login.tpl"
var remember struct {
MemberId int
Account string
Time time.Time
}
if member, ok := c.GetSession(conf.LoginSessionName).(models.Member); ok && member.MemberId > 0 {
u := c.GetString("url")
@ -41,13 +36,12 @@ func (c *AccountController) Login() {
}
c.Redirect(u,302)
}
var remember CookieRemember
// 如果 Cookie 中存在登录信息
if cookie, ok := c.GetSecureCookie(conf.GetAppKey(), "login"); ok {
if err := utils.Decode(cookie, &remember); err == nil {
if member, err := models.NewMember().Find(remember.MemberId); err == nil {
c.SetMember(*member)
c.LoggedIn(false)
c.StopRun()
}
@ -58,7 +52,7 @@ func (c *AccountController) Login() {
account := c.GetString("account")
password := c.GetString("password")
captcha := c.GetString("code")
is_remember := c.GetString("is_remember")
isRemember := c.GetString("is_remember")
// 如果开启了验证码
if v, ok := c.Option["ENABLED_CAPTCHA"]; ok && strings.EqualFold(v, "true") {
@ -74,13 +68,14 @@ func (c *AccountController) Login() {
member.Update()
c.SetMember(*member)
if strings.EqualFold(is_remember, "yes") {
beego.Info("是否记住密码:" , isRemember)
if strings.EqualFold(isRemember, "yes") {
remember.MemberId = member.MemberId
remember.Account = member.Account
remember.Time = time.Now()
v, err := utils.Encode(remember)
if err == nil {
c.SetSecureCookie(conf.GetAppKey(), "login", v)
c.SetSecureCookie(conf.GetAppKey(), "login", v,time.Now().Add(time.Hour * 24 * 30).Unix())
}
}
u,_ := url.PathUnescape(c.GetString("url"))

View File

@ -4,11 +4,14 @@ import (
"bytes"
"encoding/json"
"io"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models"
"io"
"strings"
"github.com/lifei6671/mindoc/utils"
)
type BaseController struct {
@ -19,11 +22,17 @@ type BaseController struct {
EnableDocumentHistory bool
}
type CookieRemember struct {
MemberId int
Account string
Time time.Time
}
// Prepare 预处理.
func (c *BaseController) Prepare() {
c.Data["SiteName"] = "MinDoc"
c.Data["Member"] = models.Member{}
controller,action := c.GetControllerAndAction()
c.Data["Member"] = models.NewMember()
controller, action := c.GetControllerAndAction()
c.Data["ActionName"] = action
c.Data["ControllerName"] = controller
@ -36,6 +45,17 @@ func (c *BaseController) Prepare() {
c.Member = &member
c.Data["Member"] = c.Member
} else {
var remember CookieRemember
// //如果Cookie中存在登录信息从cookie中获取用户信息
if cookie, ok := c.GetSecureCookie(conf.GetAppKey(), "login"); ok {
if err := utils.Decode(cookie, &remember); err == nil {
if member, err := models.NewMember().Find(remember.MemberId); err == nil {
c.Member = member
c.Data["Member"] = member
c.SetMember(*member)
}
}
}
//c.Member = models.NewMember()
//c.Member.Find(1)
//c.Data["Member"] = *c.Member
@ -138,5 +158,5 @@ func (c *BaseController) ShowErrorPage(errCode int, errMsg string) {
c.Abort("500")
}
c.CustomAbort(200,buf.String())
c.CustomAbort(200, buf.String())
}