我使用golang web框架作为我的web框架。但会议不会使用。我的代码编写如下:如何使用会话与golang狂欢框架
package controllers
import (
"fmt"
"net/http"
"strconv"
"GoRBAC/app/base"
"GoRBAC/app/models"
r "github.com/revel/revel"
)
type App struct {
*r.Controller
}
// 首页
func (this App) Index() r.Result {
userDao := models.NewUserDao()
user, err := userDao.QueryById(1)
if err != nil {
fmt.Println(err)
}
fmt.Println(user)
return this.Render()
}
// 跳转至登录页面
func (this App) ToLogin() r.Result {
return this.RenderTemplate("App/login.html")
}
func (this App) Login() r.Result {
username := this.Params.Get("username")
password := this.Params.Get("password")
securityCode := this.Params.Get("securityCode")
fmt.Println(this.Session["securityCode"])
fmt.Println(username)
fmt.Println(password)
fmt.Println(securityCode)
if securityCode != this.Session["securityCode"] {
return this.RenderText("error securityCode")
} else {
userDao := models.NewUserDao()
user, err := userDao.QueryByUsername(username)
if err != nil || user == nil {
return this.RenderText("username not exsit")
} else if user.Password != password {
return this.RenderText("error password")
} else {
delete(this.Session, "securityCode")
this.RenderText("ok")
}
}
return this.RenderText("ok")
}
// 获取验证码图片
func (this App) GetSecurityCode(timestamp int64) r.Result {
// 时间戳参数,第一次加载为1,后续加载为当前的时间戳,可以用来验证客户端刷新频率
// 如:刷新频率过高,直接限制当前客户端等
fmt.Println("GetSecurityCode", timestamp)
d := make([]byte, 4)
s := base.NewLen(4)
ss := ""
d = []byte(s)
for v := range d {
d[v] %= 10
ss += strconv.FormatInt(int64(d[v]), 32)
}
// 将验证码字符串存入到session
this.Session["securityCode"] = ss
this.Session.SetNoExpiration()
fmt.Println(this.Session["securityCode"])
fmt.Println(ss)
this.Response.Status = http.StatusOK
this.Response.ContentType = "image/png"
base.NewImage(d, 100, 40).WriteTo(this.Response.Out)
return this.Render()
}
我的方法GetSecurityCode()设置securityCode值在会话中,这种方法的使用,以产生用于登录的安全码验证
我使用的securityCode值Login()方法。但我没有得到任何价值
任何人都可以帮助我,我该如何解决这个问题?
谢谢!!