3

我无法设置 jwt 验证来上传文件。

我用大猩猩多路复用器。我收到了令牌,但是如何为 r.PathPrefix("/files/").Handler(http.StripPrefix("/files/", controllers.DTusHandlers3())) 设置 middalware 令牌,我尽我所能来设置有效性,我在谷歌中找不到任何设置有效性的方法

r.PathPrefix("/files/").Handler(http.StripPrefix("/files/", 
controllers.DTusHandlers3()))

路线是:

func ApiRouter() {

    r := mux.NewRouter()
    r.HandleFunc("/signin", controllers.SignIn).Methods("POST")

    r.PathPrefix("/files/").Handler(http.StripPrefix("/files/", 
    controllers.DTusHandlers3()))

    http.Handle("/", r)

    log.Fatal(http.ListenAndServe("localhost:8080", r))
}

jwt 验证是:

func IsAuthorized(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

    if r.Header["Token"] == nil {
        var err models.Error
        err = models.SetError(err, "No Token Found")
        json.NewEncoder(w).Encode(err)
        return
    }

    var mySigningKey = []byte(secretkey)

    token, err := jwt.Parse(r.Header["Token"][0], func(token *jwt.Token) (interface{}, error) {
        if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
            return nil, fmt.Errorf("There was an error in parsing token.")
        }
        return mySigningKey, nil
    })

    if err != nil {
        var err models.Error
        err = models.SetError(err, "Your Token has been expired.")
        json.NewEncoder(w).Encode(err)
        return
    }

    if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
        if claims["role"] == "admin" {
            r.Header.Set("Role", "admin")
            handler.ServeHTTP(w, r)
            return

        } else if claims["role"] == "user" {
            r.Header.Set("Role", "user")
            handler.ServeHTTP(w, r)
            return

        }
    }
    var reserr models.Error
    reserr = models.SetError(reserr, "Not Authorized.")
    json.NewEncoder(w).Encode(err)
}}

https://github.com/tus/tusd

4

0 回答 0