小编Col*_*sen的帖子

Go 中的 Json Web 令牌无效

我正在尝试使用 Go 创建一个 Json Web 令牌身份验证系统,但是我似乎无法解析 Web 令牌。该错误发生在以下函数中。

func RequireTokenAuthentication(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
authBackend := InitJWTAuthenticationBackend()

jwtString := req.Header.Get("Authorization")


token, err := jwt.Parse(jwtString, func(token *jwt.Token) (interface{}, error) {
    if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
        log.Println("Unexpected signing method")
        return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
    } else {
        log.Println("The token has been successfully returned")
        return authBackend.PublicKey, nil
    }
})

log.Println(token)
log.Println(token.Valid)

if err == nil && token.Valid && !authBackend.IsInBlacklist(req.Header.Get("Authorization")) {
    next(rw, req)
} else {
    rw.WriteHeader(http.StatusUnauthorized)
    log.P …
Run Code Online (Sandbox Code Playgroud)

parsing json curl go json-web-token

5
推荐指数
1
解决办法
5664
查看次数

Golang:循环遍历结构体的字段修改它们并返回结构体?

我试图循环遍历结构体的各个字段,将函数应用于每个字段,然后将原始结构体作为一个整体与修改后的字段值返回。显然,如果对于一个结构来说这不会构成挑战,但我需要该函数是动态的。对于本例,我引用 Post 和 Category 结构,如下所示

type Post struct{
    fieldName           data     `check:"value1"
    ...
}

type Post struct{
    fieldName           data     `check:"value2"
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后,我有一个开关函数,它循环遍历结构的各个字段,并根据其具有的值check,将函数应用于该字段,data如下所示

type Datastore interface {
     ...
}

 func CheckSwitch(value reflect.Value){
    //this loops through the fields
    for i := 0; i < value.NumField(); i++ { // iterates through every struct type field
        tag := value.Type().Field(i).Tag // returns the tag string
        field := value.Field(i) // returns the content of the struct type field

        switch tag.Get("check"){
            case "value1": …
Run Code Online (Sandbox Code Playgroud)

reflection struct interface go

5
推荐指数
1
解决办法
5158
查看次数

如何从Go中的外部包访问结构

我正在尝试从以下文件中的另一个包导入结构:

// main.go
import "path/to/models/product"    
product = Product{Name: "Shoes"}

// models/product.go
type Product struct{
 Name string
}
Run Code Online (Sandbox Code Playgroud)

但是在main.go文件中结构Product是未定义的.如何导入结构?

import struct go

2
推荐指数
1
解决办法
4337
查看次数

如何在golang中清理输入数据?

我正在尝试在将提交的数据封送到指定的结构中之前不久清理输入.

这是我正在使用的模型.

type Post struct {
    Id               int       `json:"Id"`
    CreatedAt        time.Time `json:"CreatedAt"`
    UpdatedAt        time.Time `json:"UpdatedAt"`
    CreatorId        int       `json:"CreatorId"`
    Creator          *User
    Editors          []int  `json:"Editors"`
    Status           Status `json:"Status"`
    Title            string `json:"Title"`
    ShortDescription string `json:"ShortDescription"`
    Description      string `json:"Description"`
    Content          string `json:"Content"`
    Url              string `json:"Url"`
    Media            *Media
    Categories       []Category `json:"Categories"`
    Tags             []Tag      `json:"Tags"`
    MediaId          int        `json:"MediaId"`
    Keywords         string     `json:"Keywords"`
    Data             []string   `json:"Data"`
}
Run Code Online (Sandbox Code Playgroud)

以下是可能提交的JSON数据的示例

{"Id":1,"CreatedAt":"2016-10-11T21:29:46.134+02:00","UpdatedAt":"0001-01-01T00:00:00Z","CreatorId":1,"Editors":null,"Status":1,"Title":"This is the title of the first post, to be changed.<script>alert()</script>","ShortDescription":"this is the short description of this post","Description":"","Content":"Contrary to popular …
Run Code Online (Sandbox Code Playgroud)

forms postgresql json http go

2
推荐指数
1
解决办法
5636
查看次数

标签 统计

go ×4

json ×2

struct ×2

curl ×1

forms ×1

http ×1

import ×1

interface ×1

json-web-token ×1

parsing ×1

postgresql ×1

reflection ×1