zhangzengfei
2023-08-11 a335f66c4c520728be640ca4e7029ce6f45b8f3d
web 接口添加token
1个文件已删除
1个文件已添加
8个文件已修改
789 ■■■■■ 已修改文件
.gitignore 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
collector/device.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
config.json 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
config/config.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
go.mod 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
go.sum 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
log/plc-recorder.log 722 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
msg/send.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
util/httpClient.go 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
util/jwt.go 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.gitignore
@@ -1 +1,2 @@
plc-recorder.exe
plc-recorder.exe*
log
collector/device.go
@@ -10,7 +10,8 @@
)
func getDeviceList() ([]msg.PLCDevice, error) {
    responseBody, err := util.HttpPost(config.Options.DeviceListWebApi, nil)
    token, _ := util.CreateToken()
    responseBody, err := util.HttpPost(config.Options.DeviceListWebApi, nil, token)
    if err != nil {
        logger.Warn("get device list from aps error:%s", err.Error())
        return nil, err
config.json
@@ -1,5 +1,7 @@
{
    "nsq_server": "fai365.com:4150",
    "parent_id": "guangsheng",
    "jwt_key": "abc",
    "nsq_server": "fai365.com:4150",
    "pub_plc_data_topic": "aps.guangsheng.plc.livedata",
    "write_plc_data_topic": "aps.guangsheng.plc.write",
    "sub_device_topic": "aps.guangsheng.plc.device",
config/config.go
@@ -9,6 +9,8 @@
)
type Config struct {
    ParentId          string `json:"parent_id"`            // 主账号id, 用于请求web接口
    JWTKey            string `json:"jwt_key"`              // 生成jwt的key
    NsqServer         string `json:"nsq_server"`           // nsq TCP服务端地址
    PubPLCDataTopic   string `json:"pub_plc_data_topic"`   // 发布plc数据的topic
    WritePLCDataTopic string `json:"write_plc_data_topic"` // 接收plc配置数据的topic
@@ -22,6 +24,8 @@
var Options Config
func DefaultConfig() {
    Options.ParentId = "guangsheng"
    Options.JWTKey = "abcdefghijklmn"
    Options.NsqServer = "fai365.com:4150"
    Options.PubPLCDataTopic = "aps.factory.plc.livedata"
    Options.WritePLCDataTopic = "aps.factory.plc.write"
go.mod
@@ -5,6 +5,7 @@
require (
    github.com/apache/plc4x/plc4go v0.0.0-20230731062314-d0b9a9d01b04
    github.com/fatedier/beego v1.7.2
    github.com/golang-jwt/jwt/v4 v4.5.0
    github.com/nsqio/go-nsq v1.1.0
    github.com/rs/zerolog v1.30.0
)
go.sum
@@ -10,6 +10,8 @@
github.com/fatedier/beego v1.7.2 h1:kVw3oKiXccInqG+Z/7l8zyRQXrsCQEfcUxgzfGK+R8g=
github.com/fatedier/beego v1.7.2/go.mod h1:wx3gB6dbIfBRcucp94PI9Bt3I0F2c/MyNEWuhzpWiwk=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
log/plc-recorder.log
File was deleted
msg/send.go
@@ -19,7 +19,8 @@
    // aps 发布
    if config.Options.PostPLCDataWebApi != "" {
        _, err := util.HttpPost(config.Options.PostPLCDataWebApi, b)
        token, _ := util.CreateToken()
        _, err := util.HttpPost(config.Options.PostPLCDataWebApi, b, token)
        if err != nil {
            logger.Warn(err.Error())
        }
util/httpClient.go
@@ -6,7 +6,7 @@
    "net/http"
)
func HttpPost(uri string, param []byte) ([]byte, error) {
func HttpPost(uri string, param []byte, jwtToken string) ([]byte, error) {
    request, err := http.NewRequest(http.MethodPost, uri, bytes.NewReader(param))
    if err != nil {
        return nil, err
@@ -14,6 +14,10 @@
    request.Header.Set("Content-Type", "application/json;charset=UTF-8")
    if jwtToken != "" {
        request.Header.Set("Authorization", "Bearer "+jwtToken)
    }
    response, err := http.DefaultClient.Do(request)
    if err != nil {
        return nil, err
util/jwt.go
New file
@@ -0,0 +1,41 @@
package util
import (
    "plc-recorder/config"
    "time"
    "github.com/golang-jwt/jwt/v4"
)
type CustomClaims struct {
    BaseClaims
    jwt.StandardClaims
}
type BaseClaims struct {
    UserId   string
    Username string
    ParentId string
}
func CreateToken() (string, error) {
    baseClaims := BaseClaims{
        UserId:   "plc-recorder",
        Username: "plc-recorder",
        ParentId: config.Options.ParentId,
    }
    claims := CustomClaims{
        BaseClaims: baseClaims,
        StandardClaims: jwt.StandardClaims{
            NotBefore: time.Now().Unix() - 1000,               // 签名生效时间
            ExpiresAt: time.Now().Add(1 * time.Minute).Unix(), // 过期时间
            Issuer:    "plc-recorder",                         // 签名的发行者
        },
    }
    token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
    signKey := []byte(config.Options.JWTKey)
    return token.SignedString(signKey)
}