05.验证码与登录和session
# 01.生成验证码
# 1.1 models/captcha.go
package models
import (
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/utils/captcha"
)
var Cpt *captcha.Captcha
func init() {
store := cache.NewMemoryCache()
Cpt = captcha.NewWithFilter("/captcha/", store)
Cpt.ChallengeNums = 4
Cpt.StdWidth = 100
Cpt.StdHeight = 40
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 1.2 模板中显示验证码
views\admin\login\login.html
- 在模板中使用 显示验证码
<html>
<head>
<title>用户登录</title>
<link rel="stylesheet" href="/static/admin/css/login.css">
</head>
<body>
<div class="container">
<div id="login">
<form action="/{{config "String" "adminPath" ""}}/login/doLogin" method="post" id="myform">
<input type="hidden" name="ajaxlogin" id="ajaxlogin">
<input type="hidden" name="ajaxcode" id="ajaxcode">
<div class="l_title">小米商城后台管理系统-IT营</div>
<dl>
<dd>管理员姓名:<input class="text" type="text" name="username" id="username"></dd>
<dd>管理员密码:<input class="text" type="password" name="password" id="password"></dd>
<dd>验 证 码:<input id="verify" type="text" name="captcha">
{{create_captcha}}
</dd>
<dd><input type="submit" class="submit" name="dosubmit" value=""></dd>
</dl>
</form>
</div>
</div>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 1.3 controllers\admin\login.go
controllers\admin\login.go
package admin
import (
"github.com/astaxie/beego"
)
type LoginController struct {
beego.Controller
}
func (c *LoginController) Get() {
//获取user表的数据验证数据库是否连接成功
c.TplName = "admin/login/login.html"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1.4 添加路由
routers\adminRouter.go
package routers
import (
"beegosc/controllers/admin"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
func init() {
ns :=
beego.NewNamespace("/admin",
//中间件:匹配路由前会执,可以用于权限验证
//注意引入的包: github.com/astaxie/beego/context
beego.NSBefore(func(ctx *context.Context) {
fmt.Println("我是一个中间件,匹配路由之前执行")
}),
beego.NSRouter("/login", &admin.LoginController{}),
)
//注册 namespace
beego.AddNamespace(ns)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1.5 测试验证码刷新
http://localhost:8080/beego_admin/login (opens new window)
# 02.配置session
# 2.1 conf/app.conf
sessionon=true
sessiongcmaxlifetime=3600
SessionName="itying"
1
2
3
2
3
# 2.2 session存储到redis
main.go
- 注:session默认存储到内存中,重启会丢失
package main
import (
_ "github.com/astaxie/beego/session/redis"
)
func main() {
beego.BConfig.WebConfig.Session.SessionProvider = "redis"
beego.BConfig.WebConfig.Session.SessionProviderConfig = "127.0.0.1:6379"
beego.Run()
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 2.3 session基本使用
- 通过这种方式就可以开启 session,如何使用 session
c.SetSession("username", "张三")
username := c.GetSession("username")
// 另一种获取 session 的方法
username := c.Ctx.Input.Session("username")
1
2
3
4
5
2
3
4
5
# 03.配置Golang Md5加密
- 打开 golang 包对应的网站:https://pkg.go.dev/,搜索 md5
# 3.1 方法一
data := []byte("123456")
has := md5.Sum(data)
md5str := fmt.Sprintf("%x", has)
fmt.Println(md5str)
1
2
3
4
2
3
4
# 3.2 方法二
h := md5.New()
io.WriteString(h, "123456")
fmt.Printf("%x\n", h.Sum(nil))
1
2
3
2
3
# 3.3 models\tools.go
package models
import (
"crypto/md5"
"encoding/hex"
"time"
)
func Md5(str string) string {
m := md5.New()
m.Write([]byte(str))
return string(hex.EncodeToString(m.Sum(nil)))
}
func GetUnix() int64 {
return time.Now().Unix()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 04.登录模块
# 4.1 登录控制器
controllers\admin\login.go
package admin
import (
"beegosc/models"
"github.com/astaxie/beego"
"strings"
)
type LoginController struct {
beego.Controller
}
// 1、登录页面
func (c *LoginController) Get() {
//获取user表的数据验证数据库是否连接成功
c.TplName = "admin/login/login.html"
}
// 2、登录接口
func (c *LoginController) DoLogin() {
//1、验证用户输入的验证码是否正确
var flag = models.Cpt.VerifyReq(c.Ctx.Request)
if flag {
//2、获取表单传过来的用户名和密码
username := strings.Trim(c.GetString("username"), " ")
password := models.Md5(strings.Trim(c.GetString("password"), " "))
//3、去数据库匹配
manager := []models.Manager{}
models.DB.Where("username=? AND password=? AND status=1", username, password).Find(&manager)
if len(manager) > 0 {
//登录成功 1、保存用户信息 2、跳转到后台管理系统
c.SetSession("userinfo", manager[0])
c.Ctx.WriteString("登录成功")
} else {
c.Ctx.WriteString("用户名获取密码错误")
}
} else {
c.Ctx.WriteString("验证码错误")
}
}
// 3、退出登录
func (c *LoginController) LoginOut() {
c.DelSession("userinfo")
c.Ctx.WriteString("退出登录成功")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 4.2 登录路由
func init() {
ns :=
beego.NewNamespace("/admin",
//中间件:匹配路由前会执,可以用于权限验证
//注意引入的包: github.com/astaxie/beego/context
beego.NSBefore(func(ctx *context.Context) {
fmt.Println("我是一个中间件,匹配路由之前执行")
}),
beego.NSRouter("/login", &admin.LoginController{}),
beego.NSRouter("/login/doLogin", &admin.LoginController{}, "post:DoLogin"),
beego.NSRouter("/login/loginOut", &admin.LoginController{}, "get:LoginOut"),
)
//注册 namespace
beego.AddNamespace(ns)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上次更新: 2024/3/13 15:35:10