feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期 - 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度 - 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题 - 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险 - 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件 - 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"bj_power_workstation/internal/auth"
|
||||
"bj_power_workstation/internal/svc"
|
||||
)
|
||||
|
||||
@@ -50,6 +52,29 @@ func loginHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
fail(w, http.StatusBadGateway, "MES 服务不可达:"+errString(err))
|
||||
return
|
||||
}
|
||||
// 方案 B:落盘工位登录态 JWT,作为后续报工/拧紧的可信身份锚点
|
||||
var loginResp struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
StationNo int `json:"stationNo"`
|
||||
User struct {
|
||||
Username string `json:"username"`
|
||||
Name string `json:"name"`
|
||||
} `json:"user"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &loginResp); err == nil && loginResp.Data.AccessToken != "" {
|
||||
// 解析 MES JWT 取出用户 ID,随登录态保存:离线数据落库 operator_id(按操作人隔离发送)
|
||||
userID := int64(0)
|
||||
if claims, perr := auth.ParseToken(ctx.Config.Auth.AccessSecret, loginResp.Data.AccessToken); perr == nil {
|
||||
userID = claims.UserID
|
||||
}
|
||||
_ = ctx.Store.SaveStationSession(loginResp.Data.AccessToken, loginResp.Data.User.Username,
|
||||
loginResp.Data.User.Name, loginResp.Data.StationNo, loginResp.Data.ExpireAt, userID)
|
||||
ctx.Mes.SetStationToken(loginResp.Data.AccessToken)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(body)
|
||||
@@ -65,6 +90,12 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
StationNo int `json:"stationNo"`
|
||||
}
|
||||
_ = parseJSON(r, &req)
|
||||
// 退出前先统计「当前登录人」尚未成功上报的本地数据条数,供前端提醒本人处理
|
||||
pendingQueue, pendingTorque := int64(0), int64(0)
|
||||
if ss, sErr := ctx.Store.GetStationSession(); sErr == nil && ss != nil {
|
||||
pendingQueue, _ = ctx.Store.PendingQueueCountForOperator(ss.UserID)
|
||||
pendingTorque, _ = ctx.Store.PendingTorqueCountForOperator(ss.UserID)
|
||||
}
|
||||
username := strings.TrimSpace(req.Username)
|
||||
if username != "" {
|
||||
no := req.StationNo
|
||||
@@ -75,10 +106,66 @@ func logoutHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
_, _, _ = ctx.Mes.Post("/api/internal/station/logout", map[string]any{"username": u, "stationNo": n})
|
||||
}(username, no)
|
||||
}
|
||||
// 方案 B:退出即清除本地登录态与 Bearer 凭证
|
||||
_ = ctx.Store.ClearStationSession()
|
||||
ctx.Mes.SetStationToken("")
|
||||
ok(w, map[string]any{
|
||||
"pendingQueue": pendingQueue,
|
||||
"pendingTorque": pendingTorque,
|
||||
"pendingTotal": pendingQueue + pendingTorque,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// touchHandler POST /api/auth/touch 滑动续签心跳(修订需求):任何人类触屏行为触发,
|
||||
// 立即刷新本地登录态空闲计时 last_active_at(零成本本地写),防止作业中误登出。
|
||||
// 由前端根组件全局事件监听(click/touchstart/keydown/scroll 捕获阶段)调用,节流由前端控制。
|
||||
func touchHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := ctx.Store.TouchStationSession(); err != nil {
|
||||
fail(w, http.StatusInternalServerError, "刷新登录态失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
ok(w, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// refreshTokenHandler POST /api/auth/refresh-token 滑动续签 Bearer:
|
||||
// 用当前有效 MES JWT 向 MES 换取新签发的 JWT(延长有效期),避免作业中令牌过期被登出。
|
||||
// 前置 Interceptor 已校验前端登录态;此处再代理 MES 刷新并回写本地 Bearer。
|
||||
func refreshTokenHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ss, err := ctx.Store.GetStationSession()
|
||||
if err != nil || ss == nil || ss.Token == "" {
|
||||
fail(w, http.StatusUnauthorized, "未登录或登录态已失效")
|
||||
return
|
||||
}
|
||||
respBody, _, err := ctx.Mes.Post("/api/internal/station/refresh-token", nil)
|
||||
if err != nil {
|
||||
fail(w, http.StatusUnauthorized, "MES 续签失败:"+errString(err))
|
||||
return
|
||||
}
|
||||
var rr struct {
|
||||
Code int `json:"code"`
|
||||
Data struct {
|
||||
AccessToken string `json:"accessToken"`
|
||||
ExpireAt int64 `json:"expireAt"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(respBody, &rr); err == nil && rr.Data.AccessToken != "" {
|
||||
userID := ss.UserID
|
||||
if claims, perr := auth.ParseToken(ctx.Config.Auth.AccessSecret, rr.Data.AccessToken); perr == nil {
|
||||
userID = claims.UserID
|
||||
}
|
||||
_ = ctx.Store.SaveStationSession(rr.Data.AccessToken, ss.Username, ss.Name, ss.StationNo, rr.Data.ExpireAt, userID)
|
||||
ctx.Mes.SetStationToken(rr.Data.AccessToken)
|
||||
ok(w, map[string]any{"accessToken": rr.Data.AccessToken, "expireAt": rr.Data.ExpireAt})
|
||||
return
|
||||
}
|
||||
fail(w, http.StatusUnauthorized, "MES 续签响应异常")
|
||||
}
|
||||
}
|
||||
|
||||
// configHandler GET /api/config 返回工位配置:固定工位号 stationNo、固定接驳台 dock
|
||||
// (均由 yaml 配置在启动时加载,终端不可修改)。
|
||||
func configHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
@@ -88,4 +175,4 @@ func configHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||||
"dock": ctx.Config.Station.Dock,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user