<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ~~~ 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
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看