🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ go get github.com/astaxie/beego/cache go get github.com/astaxie/beego/utils/captcha ~~~ 在会话控制的基础上实现: 代码实例: ~~~ project |-- conf | `-- app.conf ~~~ ~~~ appname = project httpport = 8080 runmode = dev #开启 session sessionon = true ~~~ ~~~ |-- routers | `-- router.go ~~~ ~~~ package routers import ( admin "project/admin/controllers" "github.com/astaxie/beego/context" "github.com/astaxie/beego" ) func init() { // 固定路由也就是全匹配的路由 beego.Router("/admin/user/login", &admin.UserController{}, "*:Login") beego.Router("/admin/user/index", &admin.UserController{}, "*:Index") beego.Router("/admin/user/exit", &admin.UserController{}, "*:Exit") // 验证用户是否已经登录 beego.InsertFilter("/*", beego.BeforeExec, FilterUser) } var FilterUser = func(ctx *context.Context) { _, ok := ctx.Input.Session("user_name").(string) if !ok && ctx.Request.RequestURI != "/admin/user/login" { ctx.Redirect(302, "login") } } ~~~ ~~~ |-- admin | |--controllers | `-- user.go ~~~ ~~~ package admin import ( "github.com/astaxie/beego" "github.com/astaxie/beego/cache" "github.com/astaxie/beego/utils/captcha" "github.com/astaxie/beego/validation" ) type UserController struct { beego.Controller } var cpt *captcha.Captcha func init() { // use beego cache system store the captcha data store := cache.NewMemoryCache() cpt = captcha.NewWithFilter("/captcha/", store) cpt.ChallengeNums = 6 cpt.StdWidth = 200 cpt.StdHeight = 80 } func (this *UserController) Login() { if this.Ctx.Input.IsGet() { // 获取 session userName := this.GetSession("user_name") userPwd := this.GetSession("user_pwd") _, nameOk := userName.(string) _, pwdOk := userPwd.(string) if nameOk && pwdOk { // 重定向 this.Redirect("index", 302) } else { // 获取 cookie this.Data["user_name"] = this.Ctx.GetCookie("user_name") this.Data["user_pwd"] = this.Ctx.GetCookie("user_pwd") this.TplName = "admin/user/login.html" } } else { userName := this.GetString("user_name") userPwd := this.GetString("user_pwd") // 表单验证 valid := validation.Validation{} resName := valid.Required(userName, "user_name") resPwd := valid.Required(userPwd, "user_pwd") // 验证输入的验证码 captcha := cpt.VerifyReq(this.Ctx.Request) if !resName.Ok || !resPwd.Ok || !captcha { // 重定向 this.Redirect("login", 302) } else { // 设置 cookie this.Ctx.SetCookie("user_name", userName) this.Ctx.SetCookie("user_pwd", userPwd) // 设置 session this.SetSession("user_name", userName) this.SetSession("user_pwd", userPwd) this.Redirect("index", 302) } } } func (this *UserController) Index() { user_name := this.GetSession("user_name") this.Data["user_name"] = user_name this.TplName = "admin/user/index.html" } func (this *UserController) Exit() { // 清空 session ,清空后 key 对应的 session value 是 nil this.DelSession("user_name") this.DelSession("user_pwd") this.Data["json"] = nil this.ServeJSON() // this.Redirect("login", 302) } ~~~ ~~~ |-- views | |--admin | |--user | `-- index.html ~~~ ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>admin/user/add</title> </head> <body> <h3>Welcome {{.user_name}}</h3> <!-- js post 请求 前端 js 跳转 --> <a href="javascript:void(0)" onclick="do_exit()">退出</a> <!-- a 标签 get 访问 后台重定向跳转 --> <!-- <a href='{{urlfor "UserController.Exit"}}' onclick="do_exit()">退出</a> --> </body> </html> <script src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script> function do_exit(){ $.ajax({ url:'{{urlfor "UserController.Exit"}}', data:{}, type:"post", dataType:'json', success:function(){ window.location.href = '/admin/user/login' } }); } </script> ~~~ ~~~ |-- views | |--admin | |--user | `-- login.html ~~~ ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>admin/user/add</title> </head> <body> <form action='{{urlfor "UserController.Login"}}' method="post" enctype="multipart/form-data"> <div class="field-content"> User Name:<input name="user_name" value="{{.user_name}}" type="text" /> </div> <div class="field-content"> Password:<input name="user_pwd" value="{{.user_pwd}}" type="password" /> </div> <div class="field-content"> <!-- 验证码 input name 必须是 captcha --> 验证码:<input name="captcha" value="" type="text" /><br/> {{create_captcha}} </div> <div class="field-content"> <input type="submit" value="提交" /> </div> </form> </body> </html> ~~~ 测试: 浏览器访问: http://127.0.0.1:8080/admin/user/index 在未登录情况下跳转到 http://127.0.0.1:8080/admin/user/login