- 用户表扩展:MES/WMS user 实体新增字段,新增 active_token 迁移 SQL(2026-11-11) - 会话机制:MES 新增 session_guard,重构 token/auth/jwt/perm 与路由;WMS/工作站同步令牌续签与鉴权 - 看板(board):调整 MES 看板产线/生产/侧栏/仓库组件及样式;WMS 看板展示页同步调整 - 工位终端后端:完善报警流、绑定、PLC 对账、拧紧、proxy、上报、站点告警/操作等 handler,补充 mes client/store - 工位终端前端:登录/主界面/各操作面板调整,新增滚动分页与多语言 yaml 文案,刷新构建产物 - MES 业务:新增流程链 logic,调整待审核/产能支撑/产品工艺/设置页、PLC 监控与帮助文档,刷新构建产物 - 更新问题记录文档
153 lines
5.5 KiB
Go
153 lines
5.5 KiB
Go
package handler
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"bj_power_wms/ent/user"
|
||
"bj_power_wms/internal/svc"
|
||
|
||
"github.com/golang-jwt/jwt/v4"
|
||
)
|
||
|
||
// JWTClaims 自定义声明
|
||
type JWTClaims struct {
|
||
UserID int `json:"userId"`
|
||
Username string `json:"username"`
|
||
RealName string `json:"realName"`
|
||
Role string `json:"role"`
|
||
// SID 后台登录会话标识(同账号互踢,2026-11-11):后台 login 生成并写入 users.active_token;
|
||
// 滑动续签(renewIfNearExpiry)原样保留 SID——不能比对原始 token 串(每请求重签会误踢并发请求)。
|
||
SID string `json:"sid,omitempty"`
|
||
jwt.RegisteredClaims
|
||
}
|
||
|
||
// sessionExpire 会话闲置窗口(秒),默认 1 小时
|
||
func sessionExpire(ctx *svc.ServiceContext) int64 {
|
||
if ctx.Config.Auth.AccessExpire > 0 {
|
||
return ctx.Config.Auth.AccessExpire
|
||
}
|
||
return 3600
|
||
}
|
||
|
||
// SignToken 签发 JWT;sid=后台登录会话标识(互踢用,续签时传 claims.SID 原样保留)
|
||
func SignToken(ctx *svc.ServiceContext, userID int, username, realName, role, sid string) (string, int64, error) {
|
||
expire := sessionExpire(ctx)
|
||
exp := time.Now().Add(time.Duration(expire) * time.Second)
|
||
claims := &JWTClaims{
|
||
UserID: userID,
|
||
Username: username,
|
||
RealName: realName,
|
||
Role: role,
|
||
SID: sid,
|
||
RegisteredClaims: jwt.RegisteredClaims{
|
||
ExpiresAt: jwt.NewNumericDate(exp),
|
||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||
Issuer: "bj_power_wms",
|
||
},
|
||
}
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||
signed, err := token.SignedString([]byte(ctx.Config.Auth.AccessSecret))
|
||
return signed, exp.Unix(), err
|
||
}
|
||
|
||
// ParseToken 校验并解析 JWT
|
||
func ParseToken(ctx *svc.ServiceContext, tokenStr string) (*JWTClaims, error) {
|
||
token, err := jwt.ParseWithClaims(tokenStr, &JWTClaims{}, func(t *jwt.Token) (any, error) {
|
||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||
return nil, errors.New("非法签名算法")
|
||
}
|
||
return []byte(ctx.Config.Auth.AccessSecret), nil
|
||
})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
claims, okk := token.Claims.(*JWTClaims)
|
||
if !okk || !token.Valid {
|
||
return nil, errors.New("token 无效")
|
||
}
|
||
return claims, nil
|
||
}
|
||
|
||
// renewIfNearExpiry 滑动续签:每个携带有效 token 的请求都重签,
|
||
// 会话有效期 = 距最后一次请求 sessionExpire 秒(默认 1 小时),闲置超时即 401。
|
||
func renewIfNearExpiry(ctx *svc.ServiceContext, w http.ResponseWriter, claims *JWTClaims) {
|
||
if time.Until(claims.ExpiresAt.Time) > 0 {
|
||
if newToken, _, err := SignToken(ctx, claims.UserID, claims.Username, claims.RealName, claims.Role, claims.SID); err == nil {
|
||
w.Header().Set("X-Renewed-Token", newToken)
|
||
}
|
||
}
|
||
}
|
||
|
||
// authInterceptor 登录鉴权中间件(白名单之外的 /api/* 需要 JWT)
|
||
// go-zero rest.Middleware 签名:func(next http.HandlerFunc) http.HandlerFunc
|
||
func authInterceptor(ctx *svc.ServiceContext) func(next http.HandlerFunc) http.HandlerFunc {
|
||
return func(next http.HandlerFunc) http.HandlerFunc {
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
path := r.URL.Path
|
||
|
||
// 白名单:健康检查 / 登录注册 / 免登录大屏 / 内部API(自带写死token校验) / 附件静态文件
|
||
// /uploads/* 为附件预览(window.open 新窗口打开,浏览器无法携带 Authorization 头),必须免鉴权。
|
||
if path == "/api/health" ||
|
||
strings.HasPrefix(path, "/api/auth/") ||
|
||
strings.HasPrefix(path, "/api/display/") ||
|
||
strings.HasPrefix(path, "/api/internal/") ||
|
||
strings.HasPrefix(path, "/uploads/") {
|
||
next.ServeHTTP(w, r)
|
||
return
|
||
}
|
||
|
||
authHeader := r.Header.Get("Authorization")
|
||
if authHeader == "" || !strings.HasPrefix(authHeader, "Bearer ") {
|
||
failCode(w, http.StatusUnauthorized, 401, "未登录或 token 缺失")
|
||
return
|
||
}
|
||
tokenStr := strings.TrimPrefix(authHeader, "Bearer ")
|
||
claims, err := ParseToken(ctx, tokenStr)
|
||
if err != nil {
|
||
failCode(w, http.StatusUnauthorized, 401, "登录已失效,请重新登录")
|
||
return
|
||
}
|
||
// 后台同账号互踢守卫(2026-11-11):JWT 内 SID 与 users.active_token 不一致 → 已被新登录顶掉。
|
||
// active_token 为空(存量旧会话/未后台登录过)放行,下次后台登录后生效。
|
||
if claims.UserID > 0 {
|
||
if u, uerr := ctx.EntClient.User.Query().Where(user.IDEQ(claims.UserID)).Only(ctx0()); uerr == nil &&
|
||
u.ActiveToken != "" && u.ActiveToken != claims.SID {
|
||
failCode(w, http.StatusUnauthorized, 401, "账号已在其他地方登录,本会话已下线,请重新登录")
|
||
return
|
||
}
|
||
}
|
||
renewIfNearExpiry(ctx, w, claims)
|
||
r.Header.Set("X-Username", claims.Username)
|
||
r.Header.Set("X-Role", claims.Role)
|
||
r.Header.Set("X-UserID", strconv.Itoa(claims.UserID))
|
||
next.ServeHTTP(w, r)
|
||
})
|
||
}
|
||
}
|
||
|
||
// internalTokenInterceptor 项目间 API 写死 token 校验
|
||
func internalTokenInterceptor(ctx *svc.ServiceContext) func(next http.Handler) http.Handler {
|
||
return func(next http.Handler) http.Handler {
|
||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
expect := ctx.Config.Internal.Token
|
||
got := r.Header.Get("X-API-TOKEN")
|
||
if expect != "" && got != expect {
|
||
failCode(w, http.StatusUnauthorized, 401, "内部接口 token 错误")
|
||
return
|
||
}
|
||
next.ServeHTTP(w, r)
|
||
})
|
||
}
|
||
}
|
||
|
||
// wrapInternal 组合内部 token 校验与 handler
|
||
func wrapInternal(ctx *svc.ServiceContext) func(h http.HandlerFunc) http.HandlerFunc {
|
||
return func(h http.HandlerFunc) http.HandlerFunc {
|
||
return internalTokenInterceptor(ctx)(h).ServeHTTP
|
||
}
|
||
}
|