Go语言实战案例:用户注册与登录(无数据库)

在实际 Web 应用中,用户注册与登录 是最常见的功能之一。

本案例不使用数据库,而是将用户信息存储在内存中,主要用于学习和演示。


一、功能目标

    1. 注册接口 /register
    • • 提交用户名和密码
    • • 保存到内存 map 中
    • • 返回注册成功信息
    1. 登录接口 /login
    • • 验证用户名和密码
    • • 登录成功后,创建 Session
    • • 在 Cookie 中存储 session_id
    1. 用户信息接口 /profile
    • • 读取 Cookie 中的 Session
    • • 返回当前登录用户信息

二、核心知识点

  • • 表单数据解析 :r.FormValue("username")
  • • 内存用户存储 :map[string]string(用户名 → 密码)
  • • Session 管理:
    • • map[string]string(session_id → username)
    • • Cookie 存储 Session ID
  • • 随机 Session ID 生成 :crypto/rand + encoding/hex

三、完整代码

go 复制代码
package main

import (
    "crypto/rand"
    "encoding/hex"
    "fmt"
    "net/http"
    "sync"
)

var (
    users       = make(map[string]string) // 用户存储: username -> password
    sessions    = make(map[string]string) // 会话存储: session_id -> username
    mu          sync.Mutex
)

// 生成随机 Session ID
func generateSessionID() string {
    b := make([]byte, 16)
    rand.Read(b)
    return hex.EncodeToString(b)
}

// 注册
func registerHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method != http.MethodPost {
        http.Error(w, "只支持 POST", http.StatusMethodNotAllowed)
        return
    }

    username := r.FormValue("username")
    password := r.FormValue("password")

    if username == "" || password == "" {
        http.Error(w, "用户名和密码不能为空", http.StatusBadRequest)
        return
    }

    mu.Lock()
    defer mu.Unlock()

    if _, exists := users[username]; exists {
        http.Error(w, "用户已存在", http.StatusConflict)
        return
    }

    users[username] = password
    fmt.Fprintf(w, "注册成功: %s\n", username)
}

// 登录
func loginHandler(w http.ResponseWriter, r *http.Request) {
    if r.Method != http.MethodPost {
        http.Error(w, "只支持 POST", http.StatusMethodNotAllowed)
        return
    }

    username := r.FormValue("username")
    password := r.FormValue("password")

    mu.Lock()
    storedPwd, exists := users[username]
    mu.Unlock()

    if !exists || storedPwd != password {
        http.Error(w, "用户名或密码错误", http.StatusUnauthorized)
        return
    }

    // 创建 Session
    sessionID := generateSessionID()
    mu.Lock()
    sessions[sessionID] = username
    mu.Unlock()

    http.SetCookie(w, &http.Cookie{
        Name:  "session_id",
        Value: sessionID,
        Path:  "/",
    })

    fmt.Fprintf(w, "登录成功: %s\n", username)
}

// 获取用户信息
func profileHandler(w http.ResponseWriter, r *http.Request) {
    cookie, err := r.Cookie("session_id")
    if err != nil {
        http.Error(w, "未登录", http.StatusUnauthorized)
        return
    }

    mu.Lock()
    username, exists := sessions[cookie.Value]
    mu.Unlock()

    if !exists {
        http.Error(w, "会话无效", http.StatusUnauthorized)
        return
    }

    fmt.Fprintf(w, "当前登录用户: %s\n", username)
}

func main() {
    http.HandleFunc("/register", registerHandler)
    http.HandleFunc("/login", loginHandler)
    http.HandleFunc("/profile", profileHandler)

    fmt.Println("服务器已启动:http://localhost:8080")
    http.ListenAndServe(":8080", nil)
}

四、运行与测试

    1. 启动服务器
go 复制代码
go run main.go
    1. 注册用户
bash 复制代码
curl -X POST -d "username=tom&password=123456" http://localhost:8080/register

返回:

makefile 复制代码
注册成功: tom
    1. 登录用户
bash 复制代码
curl -i -X POST -d "username=tom&password=123456" http://localhost:8080/login

返回(同时设置了 Set-Cookie):

makefile 复制代码
登录成功: tom
    1. 获取当前用户信息
bash 复制代码
curl --cookie "session_id=xxxxx" http://localhost:8080/profile

返回:

makefile 复制代码
当前登录用户: tom

五、注意事项

  • • 内存存储:
    • • 服务器重启后数据会丢失
    • • 不适合生产环境
  • • 安全性:
    • • 真实环境必须加密存储密码(如 bcrypt)
    • • 建议使用 HTTPS 保护 Cookie
  • • Session 过期:
    • • 生产环境要设置 Session 过期时间并定期清理

六、进阶扩展

  • • 使用 bcrypt 对密码进行哈希处理
  • • 将用户数据和 Session 存储到 Redis 或 数据库
  • • 添加 登出接口 清除 Session
  • • 使用第三方 Session 管理库(如 gorilla/sessions)

相关推荐
FRing031 小时前
for循环、while循环、Java高级语法、数组
java·开发语言·后端·算法
星核0penstarry1 小时前
接中国大模型 API 之前,先搞清楚国际站、地域隔离和备案材料
后端·openai·api
AAA修煤气灶刘哥2 小时前
Agent 面试题|上下文压缩怎么兼顾 token 开销和决策效果?
人工智能·后端·面试
geovindu3 小时前
python: Gradient Boosting
开发语言·后端·python·算法·boosting
AAA修煤气灶刘哥3 小时前
Agent 面试题|工具调用结果不符合预期,什么时候该重规划?
人工智能·后端·面试
沉着宇宙3 小时前
一个跑了四十轮的会话,最后靠一份日志文件续上了
后端
亦暖筑序3 小时前
准备升级 AgentScope Java 2.0.5?这几个会话坑先避开
人工智能·后端·agent
疯华整猫3 小时前
go vet 一行不报,单测全绿,服务却 leaking:AI 时代学 Go,重点是这三个它看不到的地方
后端·go·编程语言
不要试图纠正别人3 小时前
同一个问题,九条 SQL 都能跑通,答案差三倍
后端·rails
杨工踩坑录3 小时前
别再手工填数据字典了:MySQL DDL + Java 实体类自动生成 Excel 登记文档
后端