feat: 会话令牌与守卫、看板组件及工位终端功能完善
- 用户表扩展: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 监控与帮助文档,刷新构建产物 - 更新问题记录文档
This commit is contained in:
@@ -11,31 +11,35 @@ import (
|
||||
)
|
||||
|
||||
// Claims 自定义 JWT 声明
|
||||
// StationNo 与 MES tokenx.Claims 同名同 tag(stationNo):终端解析 MES 签发的工位登录 JWT
|
||||
// 时带出工位号,滑动续签本地重签时原样保留(MES StationSessionAuth 据此取工位)。
|
||||
type Claims struct {
|
||||
UserID int64 `json:"userId"`
|
||||
Username string `json:"username"`
|
||||
RealName string `json:"realName"`
|
||||
Role string `json:"role"`
|
||||
UserID int64 `json:"userId"`
|
||||
Username string `json:"username"`
|
||||
RealName string `json:"realName"`
|
||||
Role string `json:"role"`
|
||||
StationNo int `json:"stationNo,omitempty"`
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// TokenInfo 签发 token 所需的用户信息
|
||||
type TokenInfo struct {
|
||||
UserID int64
|
||||
Username string
|
||||
RealName string
|
||||
Role string
|
||||
UserID int64
|
||||
Username string
|
||||
RealName string
|
||||
Role string
|
||||
StationNo int
|
||||
}
|
||||
|
||||
const issuer = "bj_power_workstation"
|
||||
|
||||
// SessionValidator 由 main 注入:校验请求 token 是否仍是当前有效登录会话
|
||||
// (与 station_session.token 比对,实现「后登录踢前一个」)。nil 时跳过该校验。
|
||||
// SessionValidator 由 main 注入:校验请求 token 是否存在于任一有效登录会话
|
||||
// (多会话版:同工位互踢后旧 token 不再命中任何会话,即被判为已下线)。nil 时跳过该校验。
|
||||
var SessionValidator func(token string) bool
|
||||
|
||||
// SessionTokenSync 由 main 注入:滑动续签重签出新 token 时同步写回当前登录会话,
|
||||
// SessionTokenSync 由 main 注入:滑动续签重签出新 token 时按旧 token 定位会话同步写回,
|
||||
// 保证前端 token 与会话 token 始终一致,避免续签后被误判为旧会话。
|
||||
var SessionTokenSync func(token string)
|
||||
var SessionTokenSync func(oldToken, newToken string)
|
||||
|
||||
func defaultExpire(expire int64) int64 {
|
||||
if expire <= 0 {
|
||||
@@ -48,10 +52,11 @@ func defaultExpire(expire int64) int64 {
|
||||
func SignToken(secret string, expire int64, info TokenInfo) (string, int64, error) {
|
||||
exp := time.Now().Add(time.Duration(defaultExpire(expire)) * time.Second)
|
||||
claims := &Claims{
|
||||
UserID: info.UserID,
|
||||
Username: info.Username,
|
||||
RealName: info.RealName,
|
||||
Role: info.Role,
|
||||
UserID: info.UserID,
|
||||
Username: info.Username,
|
||||
RealName: info.RealName,
|
||||
Role: info.Role,
|
||||
StationNo: info.StationNo,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(exp),
|
||||
IssuedAt: jwt.NewNumericDate(time.Now()),
|
||||
@@ -81,20 +86,22 @@ func ParseToken(secret, tokenStr string) (*Claims, error) {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
// renewIfNearExpiry 滑动续签:剩余有效期不足一半时重签,写入响应头 X-Renewed-Token
|
||||
func renewIfNearExpiry(secret string, expire int64, w http.ResponseWriter, claims *Claims) {
|
||||
// renewIfNearExpiry 滑动续签:剩余有效期不足一半时重签,写入响应头 X-Renewed-Token。
|
||||
// 多会话版:按请求旧 token 定位会话同步更新(SessionTokenSync(old, new))。
|
||||
func renewIfNearExpiry(secret string, expire int64, w http.ResponseWriter, claims *Claims, oldToken string) {
|
||||
remaining := time.Until(claims.ExpiresAt.Time)
|
||||
if remaining > 0 && remaining < time.Duration(defaultExpire(expire))*time.Second/2 {
|
||||
if newToken, _, err := SignToken(secret, expire, TokenInfo{
|
||||
UserID: claims.UserID,
|
||||
Username: claims.Username,
|
||||
RealName: claims.RealName,
|
||||
Role: claims.Role,
|
||||
UserID: claims.UserID,
|
||||
Username: claims.Username,
|
||||
RealName: claims.RealName,
|
||||
Role: claims.Role,
|
||||
StationNo: claims.StationNo,
|
||||
}); err == nil {
|
||||
w.Header().Set("X-Renewed-Token", newToken)
|
||||
// 前端会据此更新本地 token,同步写回会话,保证两者始终一致。
|
||||
// 前端会据此更新本地 token,同步写回对应会话,保证两者始终一致。
|
||||
if SessionTokenSync != nil {
|
||||
SessionTokenSync(newToken)
|
||||
SessionTokenSync(oldToken, newToken)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,8 +115,10 @@ func unauthorized(w http.ResponseWriter, msg string) {
|
||||
}
|
||||
|
||||
// Interceptor 登录鉴权中间件。
|
||||
// 白名单:/api/health、/api/auth/login、/pdfopen/*。
|
||||
// 白名单:/api/health、/api/auth/login、/api/alerts/stream、/pdfopen/*。
|
||||
// 非 /api/ 且非 /pdfopen/ 的路径为前端静态资源,直接放行。
|
||||
// 2026-11-11 修复:补上 /api/alerts/stream(工位报警 SSE)——EventSource 无法携带 JWT,
|
||||
// 此前中间件白名单缺这一条,SSE 一直被 401 拦截(与 routes.go 注册白名单不一致)。
|
||||
func Interceptor(secret string, accessExpire int64) func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -120,6 +129,7 @@ func Interceptor(secret string, accessExpire int64) func(next http.HandlerFunc)
|
||||
path == "/api/auth/login" ||
|
||||
path == "/api/config" ||
|
||||
path == "/api/station-options" ||
|
||||
path == "/api/alerts/stream" ||
|
||||
strings.HasPrefix(path, "/pdfopen/")
|
||||
if notAPI || whitelisted {
|
||||
next.ServeHTTP(w, r)
|
||||
@@ -137,13 +147,14 @@ func Interceptor(secret string, accessExpire int64) func(next http.HandlerFunc)
|
||||
unauthorized(w, "登录已失效")
|
||||
return
|
||||
}
|
||||
// 在线态校验(后登录踢前一个):请求 token 必须与当前登录会话一致。
|
||||
// 在线态校验(多会话版):请求 token 必须存在于任一登录会话;
|
||||
// 同工位被新登录踢掉后旧 token 不再命中任何会话,即被判为已下线。
|
||||
// 白名单与静态资源已在上方放行,不会被此处拦截。
|
||||
if SessionValidator != nil && !SessionValidator(tokenStr) {
|
||||
unauthorized(w, "已在其他设备登录,请重新登录")
|
||||
unauthorized(w, "登录已失效或工位已被其他人员登录,请重新登录")
|
||||
return
|
||||
}
|
||||
renewIfNearExpiry(secret, accessExpire, w, claims)
|
||||
renewIfNearExpiry(secret, accessExpire, w, claims, tokenStr)
|
||||
r.Header.Set("X-Username", claims.Username)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user