<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>

                Session === 通過cookie獲取session 生成sessionId與用戶關聯儲存到MAP中,這里就不考慮內存問題了 ``` var ( SessionMap sync.Map ) type SessionNode struct { Name string Time int64 OutTime int64 } func main() { // 用戶登陸就設置cookie http.HandleFunc("/login",login) // 用戶訪問主業驗證cookie http.HandleFunc("/home",home) err := http.ListenAndServe(":9085", nil) if err!= nil { panic(err.Error()) } } func login(w http.ResponseWriter,r *http.Request) { r.ParseForm() name := r.PostForm.Get("name") password := r.PostForm.Get("password") // 用戶驗證 if name == "dollarkiller" && password == "password" { //用戶登陸成功設置cookie id := generateSessionId() cookie := &http.Cookie{Name: "session",Value:id} http.SetCookie(w,cookie) // 存儲用戶session到map中 outtime := time.Now().Unix() + 6*60*60 i := &SessionNode{ Name:name, Time:time.Now().Unix(), OutTime:outtime, } SessionMap.Store(id,i) fmt.Println(i) fmt.Println(id) w.WriteHeader(200) w.Header().Set("Content-Type","application/json") resp := map[string]interface{} { "msg":"login OK", } bytes, _ := json.Marshal(resp) w.Write(bytes) return } w.WriteHeader(400) w.Header().Set("Content-Type","application/json") resp := map[string]interface{} { "msg":"error", } bytes, _ := json.Marshal(resp) w.Write(bytes) } // 用戶攜帶sessionId前來 func home(w http.ResponseWriter,r *http.Request) { // 檢測sessionId cookie, e := r.Cookie("session") if e != nil { fmt.Println("錯誤:",e.Error()) http.Redirect(w,r,"/login",301) return } session := cookie.Value // 根據session去map中查詢是否存在 value, ok := SessionMap.Load(session) if ok!=true { fmt.Println("用戶不存在") http.Redirect(w,r,"/login",301) return } nowTime := time.Now().Unix() i := value.(*SessionNode) ctime := i.Time outTime := i.OutTime fmt.Println("ss: ",nowTime) fmt.Println("ssb: ",outTime) if nowTime > ctime && nowTime < outTime { i2 := map[string]interface{} { "msg":"ok", } w.WriteHeader(200) w.Header().Set("Content-Type","application/json") bytes, _ := json.Marshal(i2) w.Write(bytes) return } http.Redirect(w,r,"/login",301) } // 生成session id func generateSessionId() string { intn := rand.Intn(100000) // 隨機數 unix := time.Now().UnixNano() // 時間 encode := Md5Encode(strconv.FormatInt(unix, 10) + strconv.Itoa(intn)) return encode } func Md5Encode(str string) string { data := []byte(str) md5Ctx := md5.New() md5Ctx.Write(data) cipherStr := md5Ctx.Sum(nil) return hex.EncodeToString(cipherStr) } ``` 我在這里也實現了一個seeion簡單的 ``` package utils import ( "math/rand" "strconv" "sync" "time" ) // session 庫 var ( SessionMap sync.Map ) type SessionNode struct { Name string CreationTime int64 // 創建時間 ExpirationTime int64 // 過期時間 } // 獲得session func GetSession(name string) string { timeNano := time.Now().UnixNano() time := time.Now().Unix() outtime := time + 6*60*60 intn := rand.Intn(100000) encode := Md5Encode(strconv.FormatInt(timeNano, 10) + strconv.Itoa(intn)) node := &SessionNode{ Name: name, CreationTime: time, ExpirationTime: outtime, } SessionMap.Store(encode,node) return encode } // 驗證session func CheckSession(sessionId string) bool { if sessionId == "" || len(sessionId) == 0 { return false } value, ok := SessionMap.Load(sessionId) if ok != true { return false } node := value.(*SessionNode) nowTime := time.Now().Unix() if nowTime >= node.CreationTime && nowTime < node.ExpirationTime { return true } return false } ```
                  <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>

                              哎呀哎呀视频在线观看