@ -9,14 +9,14 @@
### 1. 客户端获取JWT Token
### 1. 客户端获取JWT Token
我们定义一个协议供客户端调用获取JWT token, 我们找一个目录执行goctl api new jwt , 将生成的jwt.api改成如下:
我们定义一个协议供客户端调用获取JWT token, 我们新建一个目录jwt然后在目录中执行 `goctl api -o jwt.api` , 将生成的jwt.api改成如下:
````go
````go
type JwtTokenRequest struct {
type JwtTokenRequest struct {
}
}
type JwtTokenResponse struct {
type JwtTokenResponse struct {
AccessToken string `json:"access_token"`
AccessToken string `json:"access_token"`
AccessExpire int64 `json:"access_expire"`
AccessExpire int64 `json:"access_expire"`
RefreshAfter int64 `json:"refresh_after"` // 建议客户端刷新token的绝对时间
RefreshAfter int64 `json:"refresh_after"` // 建议客户端刷新token的绝对时间
}
}
@ -25,18 +25,18 @@ service jwt-api {
@server (
@server (
handler: JwtHandler
handler: JwtHandler
)
)
post /user/token(JwtTokenRequest) returns (JwtTokenResponse);
post /user/token(JwtTokenRequest) returns (JwtTokenResponse)
}
}
````
````
再次在生产服务目录中执行: api go -api jwt.api -dir .
再次在生成服务目录中执行:`goctl api go -api jwt.api -dir .`
打开jwtlogic.go文件, 修改 `func (l *JwtLogic) Jwt(req types. Request) (*types.Response, error) {` 方法如下:
打开jwtlogic.go文件, 修改 `func (l *JwtLogic) Jwt(req types. JwtToken Request) (*types.JwtToken Response, error) {` 方法如下:
```go
```go
const AccessSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
const AccessSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
func (l *JwtLogic) Jwt(req types.Request) (*types.Response, error) {
func (l *JwtLogic) Jwt(req types.JwtToken Request) (*types.JwtToken Response, error) {
var accessExpire int64 = 60 * 60 * 24 * 7
var accessExpire int64 = 60 * 60 * 24 * 7
now := time.Now().Unix()
now := time.Now().Unix()
@ -45,7 +45,7 @@ func (l *JwtLogic) Jwt(req types.Request) (*types.Response, error) {
return nil, err
return nil, err
}
}
return & types.Response{AccessToken: accessToken, AccessExpire: now + accessExpire, RefreshAfter: now + accessExpire/2}, nil
return & types.JwtToken Response{AccessToken: accessToken, AccessExpire: now + accessExpire, RefreshAfter: now + accessExpire/2}, nil
}
}
func (l *JwtLogic) GenToken(iat int64, secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
func (l *JwtLogic) GenToken(iat int64, secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
@ -81,9 +81,9 @@ type JwtTokenRequest struct {
}
}
type JwtTokenResponse struct {
type JwtTokenResponse struct {
AccessToken string `json:"access_token"`
AccessToken string `json:"access_token"`
AccessExpire int64 `json:"access_expire"`
AccessExpire int64 `json:"access_expire"`
RefreshAfter int64 `json:"refresh_after"` // 建议客户端刷新token的绝对时间
RefreshAfter int64 `json:"refresh_after"` // 建议客户端刷新token的绝对时间
}
}
type GetUserRequest struct {
type GetUserRequest struct {