使用 Gorilla 会话时,Golang 中的会话变量未保存
问题描述
在 Gorilla 会话实现中,不会跨请求维护会话变量。登录并设置会话变量后,新选项卡应该维护会话,但用户会被重定向到登录页面。
代码分析
<code class="go">sessionNew.Save(req, res)</code>
此代码丢失错误sessionNew.Save() 的处理。如果保存过程失败,该错误将被忽略,从而导致意外行为。应更新为:
<code class="go">err := sessionNew.Save(req, res) if err != nil { // Handle the error }</code>
会话路径
会话路径设置为“/loginSession”,这将会话的范围限制为仅该特定路径。这可能会导致混乱,因为访问其他路由的用户将无法访问该会话。为了确保会话在所有路由上可用,路径应设置为“/”。
会话检查
在 SessionHandler 中,会话检查在提供静态文件后执行。这可能会导致问题,因为静态文件是在验证会话之前提供的。应在提供任何内容之前执行会话检查。
经过改进的完整代码
<code class="go">package main import ( "crypto/md5" "encoding/hex" "fmt" "github.com/gocql/gocql" "github.com/gorilla/mux" "github.com/gorilla/sessions" "net/http" "time" ) var store = sessions.NewCookieStore([]byte("something-very-secret")) var router = mux.NewRouter() func init() { store.Options = &sessions.Options{ Domain: "localhost", Path: "/", MaxAge: 3600 * 8, // 8 hours HttpOnly: true, } } func main() { //session handling router.HandleFunc("/", sessionHandler) router.HandleFunc("/signIn", signInHandler) router.HandleFunc("/signUp", signUpHandler) router.HandleFunc("/logOut", logOutHandler) http.Handle("/", router) http.ListenAndServe(":8100", nil) } //handler for signIn func signInHandler(res http.ResponseWriter, req *http.Request) { // Get the session session, err := store.Get(req, "loginSession") if err != nil { // Handle the error } // Set session values session.Values["email"] = req.FormValue("email") session.Values["name"] = req.FormValue("password") // Save the session err = session.Save(req, res) if err != nil { // Handle the error } } //handler for signUp func signUpHandler(res http.ResponseWriter, req *http.Request) { // ... } //handler for logOut func logOutHandler(res http.ResponseWriter, req *http.Request) { // Get the session session, err := store.Get(req, "loginSession") if err != nil { // Handle the error } // Save the session (with updated values) err = session.Save(req, res) if err != nil { // Handle the error } } //handler for Session func sessionHandler(res http.ResponseWriter, req *http.Request) { // Get the session session, err := store.Get(req, "loginSession") if err != nil { // Handle the error } // Check if the session is valid if session.Values["email"] == nil { http.Redirect(res, req, "html/login.html", http.StatusFound) } else { http.Redirect(res, req, "html/home.html", http.StatusFound) } }</code>
以上是为什么我的 Golang 会话变量没有保存在 Gorilla 会话中?的详细内容。更多信息请关注PHP中文网其他相关文章!

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Golang在并发性上优于C ,而C 在原始速度上优于Golang。1)Golang通过goroutine和channel实现高效并发,适合处理大量并发任务。2)C 通过编译器优化和标准库,提供接近硬件的高性能,适合需要极致优化的应用。

选择Golang的原因包括:1)高并发性能,2)静态类型系统,3)垃圾回收机制,4)丰富的标准库和生态系统,这些特性使其成为开发高效、可靠软件的理想选择。

Golang适合快速开发和并发场景,C 适用于需要极致性能和低级控制的场景。1)Golang通过垃圾回收和并发机制提升性能,适合高并发Web服务开发。2)C 通过手动内存管理和编译器优化达到极致性能,适用于嵌入式系统开发。

Golang在编译时间和并发处理上表现更好,而C 在运行速度和内存管理上更具优势。1.Golang编译速度快,适合快速开发。2.C 运行速度快,适合性能关键应用。3.Golang并发处理简单高效,适用于并发编程。4.C 手动内存管理提供更高性能,但增加开发复杂度。

Golang在Web服务和系统编程中的应用主要体现在其简洁、高效和并发性上。1)在Web服务中,Golang通过强大的HTTP库和并发处理能力,支持创建高性能的Web应用和API。2)在系统编程中,Golang利用接近硬件的特性和对C语言的兼容性,适用于操作系统开发和嵌入式系统。

Golang和C 在性能对比中各有优劣:1.Golang适合高并发和快速开发,但垃圾回收可能影响性能;2.C 提供更高性能和硬件控制,但开发复杂度高。选择时需综合考虑项目需求和团队技能。

Golang适合高性能和并发编程场景,Python适合快速开发和数据处理。 1.Golang强调简洁和高效,适用于后端服务和微服务。 2.Python以简洁语法和丰富库着称,适用于数据科学和机器学习。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver Mac版
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中