Files
bj_power/bj_power_mes/internal/svc/service_context.go
T
SunYF 756d978900 feat: 实现工位终端滑动续签机制,强化安全与身份追溯
- 新增滑动续签功能:人工触屏触发本地心跳 + 30秒节流刷新JWT,避免作业中令牌过期
- 拆分质量检验权限:过程检/完工检使用不同权限码,细化权限粒度
- 新增操作人用户ID字段:报工、拧紧记录落库时携带可信用户ID,解决身份追溯问题
- 移除编译期默认密钥,启动期校验所有必要密钥非空,防止密钥泄露风险
- 修复WMS附件越权遍历问题:使用UUID文件名替代顺序ID访问附件
- 实现登录态过期数据隔离:仅返回当前操作人的待同步数据,避免身份混淆
2026-10-06 19:36:22 +08:00

69 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package svc
import (
"strings"
"bj_power_mes/ent"
"bj_power_mes/internal/config"
"bj_power_mes/internal/db"
"bj_power_mes/internal/eventlog"
"bj_power_mes/internal/plc"
"bj_power_mes/internal/sse"
"bj_power_mes/internal/wmsclient"
"github.com/zeromicro/go-zero/core/stores/redis"
)
// ServiceContext 全局依赖
type ServiceContext struct {
Config config.Config
EntClient *ent.Client
RedisClient *redis.Redis
EventLog *eventlog.Writer
SSE *sse.Hub
PLC *plc.Manager
Wms *wmsclient.Client
}
func NewServiceContext(c config.Config) *ServiceContext {
// 启动期密钥校验:编译期默认密钥已移除,yaml 缺失/为空则直接 panic,
// 杜绝「空 token 绕过内部接口鉴权」的攻击面(安全审计 #6/#13)。
validateMesSecrets(c)
entClient, _ := db.MustNewDB(c.Database)
redisClient := redis.New(c.Redis.Host)
plcMgr, _ := plc.NewManager(c.Plc, true) // 本项目默认 mock 对接 S7
return &ServiceContext{
Config: c,
EntClient: entClient,
RedisClient: redisClient,
EventLog: eventlog.NewWriter(entClient),
SSE: sse.NewHub(),
PLC: plcMgr,
Wms: wmsclient.New(c.Wms.BaseURL, c.Wms.Token),
}
}
// validateMesSecrets 启动期校验必要密钥非空(编译期默认密钥已移除)。
func validateMesSecrets(c config.Config) {
missing := make([]string, 0, 4)
if strings.TrimSpace(c.Auth.AccessSecret) == "" {
missing = append(missing, "Auth.AccessSecret")
}
if strings.TrimSpace(c.Internal.Token) == "" {
missing = append(missing, "Internal.Token")
}
if strings.TrimSpace(c.Cli.AdminPassword) == "" {
missing = append(missing, "Cli.AdminPassword")
}
if strings.TrimSpace(c.Wms.Token) == "" {
missing = append(missing, "Wms.Token")
}
if len(missing) > 0 {
panic("配置缺失必要密钥(已禁止编译期默认密钥,必须在 etc/*.yaml 中显式配置):" + strings.Join(missing, ", "))
}
}