如何在Golang Revel框架中使用会话(session)?

huangapple go评论89阅读模式
英文:

How to use session with golang revel framework

问题

我使用golang的Web框架作为我的Web框架。但是会话(session)没有使用。我写的代码如下:

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的值设置到session中,该方法用于生成登录验证的验证码。

我在Login()方法中使用了securityCode的值。但是我得不到这个值。

有谁可以帮助我,如何解决这个问题?

谢谢!

英文:

I use the golang web framework as my web framework. But the session doesn`t use. the code I write as follow :

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()
}

I set the securityCode value in session in the method GetSecurityCode() , this method use to generate the security Code for login verify

I use the the securityCode value in the method Login(). But I get nothing for the value

Anyone who can help me , how can I fix this problem ?

thanks !!

答案1

得分: 1

我想知道为什么你的绝对有效的问题被投票否决了。你描述的问题是已知的。这里有一个相关的 GitHub 问题:https://github.com/revel/revel/issues/792

临时解决方法是避免直接写入 c.Response.Out。而是使用缓冲区,然后使用 RenderText 返回它:

this.Response.Status = http.StatusOK
this.Response.ContentType = "image/png"

var buffer bytes.Buffer
base.NewImage(d, 100, 40).WriteTo(&buffer)

return this.RenderText(buffer.String())

或者,修改你的 GetSecurityCode,这样你就不需要在那里使用 Session

英文:

I wonder why was your absolutely valid question downvoted. The problem you have described is known. Here is a related GH issue: https://github.com/revel/revel/issues/792

Temporary workaround would be avoiding to write directly to c.Response.Out. Instead use a buffer and then return it using RenderText:

this.Response.Status = http.StatusOK
this.Response.ContentType = "image/png"
var buffer bytes.Buffer
base.NewImage(d, 100, 40).WriteTo(&buffer)
return this.RenderText(buffer.String())

Or alternatively modify your GetSecurityCode so you don't need Session there.

答案2

得分: 1

@anonx:
谢谢
根据你的说法,我修复了这个问题,代码如下:

// 获取验证码图片
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)
}
this.Session["securityCode"] = ss
fmt.Println(this.Session["securityCode"])
//this.Response.Status = http.StatusOK
//this.Response.ContentType = "image/png"
buff := &bytes.Buffer{}
base.NewImage(d, 100, 40).WriteTo(buff)
return this.RenderBinary(buff, "img", r.Attachment, time.Now())
}
英文:

@anonx:
Thanks
As you say, I fix this problem like :

// 获取验证码图片
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)
}
this.Session["securityCode"] = ss
fmt.Println(this.Session["securityCode"])
//this.Response.Status = http.StatusOK
//this.Response.ContentType = "image/png"
buff := &bytes.Buffer{}
base.NewImage(d, 100, 40).WriteTo(buff)
return this.RenderBinary(buff, "img", r.Attachment, time.Now())
}

huangapple
  • 本文由 发表于 2015年4月7日 10:55:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/29482931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定