middleware

package
v0.0.0-...-4aec9fb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2019 License: MIT Imports: 7 Imported by: 0

README

JWT的使用

修改service/pkg/http/handler_gen.goNewHTTPHandler函数,加入中间键处理

// authentication/pkg/http/handler_gen.go

import MyJwt "github.com/money-hub/MoneyDodo.service/middleware"

// NewHTTPHandler returns a handler that makes a set of endpoints available on
// predefined paths.
func NewHTTPHandler(endpoints endpoint.Endpoints, options map[string][]http.ServerOption) http1.Handler {
	m := mux.NewRouter()
	m.Use(MyJwt.GetTokenInfo)   // 添加中间键处理
	makeGetOpenidHandler(m, endpoints, options["GetOpenid"])
	makeAdminLoginHandler(m, endpoints, options["AdminLogin"])
	makeEnterpriseLoginHandler(m, endpoints, options["EnterpriseLogin"])
	makeLogoutHandler(m, endpoints, options["Logout"])
	return m
}

// 使用
eg:
ctx.Value("id").(string)
ctx.Value("role").(int)

Documentation

Index

Constants

View Source
const Issuer = "Money-Hub"
View Source
const SecretKey = "MoneyDodo"
View Source
const TokenName = "token"

Variables

This section is empty.

Functions

func CreateToken

func CreateToken(SecretKey []byte, issuer string, id string, role int) (tokenString string, err error)

*

  • 生成 token
  • SecretKey 是一个 const 常量

func GetTokenInfo

func GetTokenInfo(next http.Handler) http.Handler

* * 提取 request内的token信息

func ParseToken

func ParseToken(tokenSrt string, SecretKey []byte) (claims jwt.MapClaims, err error)

*

  • 解析 token

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL