以下パッケージに関する質問です
https://github.com/dgrijalva/jwt-go
- 渡ってきたトークンから
jwt-go
でClaims(jwt.MapClaims
)取得 http.Request.Context()
に入れるr.Context().Value()
からMapClaims
取得MapClaims
からvalueを得ようとする
上記手順でエラーになってしまいます。なぜか教えていただけると幸いです。
contextに入れる前では取得できてます。
invalid operation: claims["uid"] (type interface {} does not support indexing)
以下コードです
func jwtMiddleware(fn http.HandleFunc) http.HandleFunc { return func (w http.ResponseWriter, r *http.Request) { token, err := jwt.Parse(t, func(token *jwt.Token) (interface{}, error) { return []byte(secretKey), nil }) claims := token.Claims.(jwt.MapClaims) ctx := r.Context() ctx = context.WithValue(ctx, tokenKey, claims) fn(w, r.WithContext(ctx)) } } func hogeHandler (w http.ResponseWrite, r *http.Request) { claims := r.Context().Value(tokenKey) uid := claims["uid"].(string) //<- エラー invalid operation: claims["uid"] (type interface {} does not support indexing) }

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。