douba3975 2019-03-12 09:53
浏览 43
已采纳

注销后如何停止会话缓存经过身份验证的用户数据

This code was leverage here

The code below was used to create users session in Go. The session is working fine.

The issue am having is that after user logout, if I click on browser back button. I can then still see the details of the logout user.

I have leverage stackoverflow solution here but no luck

stackoverflow link

I have also added the following code to logout handler

w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("X-Accel-Expires", "0")

here is the code

package main

import (
    "fmt"
    "net/http"
   "github.com/gorilla/sessions"
)

var (
    // key must be 16, 24 or 32 bytes long (AES-128, AES-192 or AES-256)
    key = []byte("super-secret-key")
    store = sessions.NewCookieStore(key)
)

func secret(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "cookie-name")

    // Check if user is authenticated
    if auth, ok := session.Values["authenticated"].(bool); !ok || !auth {
        http.Error(w, "Forbidden", http.StatusForbidden)
        return
    }

    // Print secret message
    fmt.Fprintln(w, "The cake is a lie!")
        fmt.Fprintln(w, session.Values["foo"])
}

func login(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "cookie-name")

    // Authentication goes here

    // Set user as authenticated
  session.Values["authenticated"] = true
  session.Values["foo"] = "bar"
  session.Values[42] = 43
    session.Save(r, w)

fmt.Fprintln(w, session.Values["authenticated"])
fmt.Fprintln(w, session.Values["foo"])
fmt.Fprintln(w, session.Values["2"])
}

func logout(w http.ResponseWriter, r *http.Request) {
    session, _ := store.Get(r, "cookie-name")

    // Revoke users authentication
    session.Values["authenticated"] = false
        session.Values["foo"] = ""
        session.Values[42] = ""
    session.Save(r, w)

// prevent caching
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("X-Accel-Expires", "0")


}

func main() {
    http.HandleFunc("/secret", secret)
    http.HandleFunc("/login", login)
    http.HandleFunc("/logout", logout)

    http.ListenAndServe(":8000", nil)
}
  • 写回答

1条回答 默认 最新

  • doubingqi5829 2019-03-12 11:23
    关注

    I am afraid there is no way to "fix" your code without using JavaScript and changing the "secret" page before leaving it.

    The problem is not in Go, or in gorilla/sessions, or even in HTTP: it is in the way the web browsers perform caching: at least in Firefox and Chrome, when going "back" and "forward" the browsers just display the cached page from disk, ignoring completely the headers.

    Oh, and BTW: to try to force the browsers to stop caching a page, you should add those lines to the "secret" handler, and not to the "logout" one.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。