2025-06-08 11:18:31 +08:00
..
2025-06-08 10:05:58 +08:00
2025-05-31 12:03:22 +08:00
2025-05-31 16:14:34 +08:00
2025-05-31 16:14:34 +08:00
2025-05-31 16:14:34 +08:00
2025-05-31 16:14:34 +08:00
2025-05-31 16:14:34 +08:00
2025-05-31 16:14:34 +08:00
2025-05-31 16:14:34 +08:00
2025-06-08 11:18:31 +08:00
2025-06-08 10:05:58 +08:00
2025-05-31 10:50:37 +08:00

令牌管理

  • 颁发访问令牌: Login
  • 撤销访问令牌: 令牌失效了 Logout
  • 校验访问令牌:检查令牌的合法性, 是不是伪造的

详情设计

字段(业务需求)

令牌:

  • 过期时间
  • 颁发时间
  • 被颁发的人
  • ...

问题: 无刷新功能, 令牌到期了,自动退出了, 过期时间设置长一点, 长时间不过期 又有安全问题

  1. 业务功能: 令牌的刷新, 令牌过期了过后,允许用户进行刷新(需要使用刷新Token来刷新 刷新Token也是需要有过期时间 这个时间决定回话长度)有了刷新token用户不会出现 使用中被中断的情况, 并且长时间未使用,系统也户自动退出(刷新Token过期)

转化为接口定义

type Service interface {
	// 颁发访问令牌: Login
	IssueToken(context.Context, *IssueTokenRequest) (*Token, error)
	// 撤销访问令牌: 令牌失效了 Logout
	RevolkToken(context.Context, *RevolkTokenRequest) (*Token, error)

	// 校验访问令牌:检查令牌的合法性, 是不是伪造的
	ValiateToken(context.Context, *ValiateTokenRequest) (*Token, error)
}

接口的实现

func TestIssueToken(t *testing.T) {
	req := token.NewIssueTokenRequest()
	req.IssueByPassword("admin", "123456")
	req.Source = token.SOURCE_WEB
	set, err := svc.IssueToken(ctx, req)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(set)
}

func TestQueryToken(t *testing.T) {
	req := token.NewQueryTokenRequest()
	set, err := svc.QueryToken(ctx, req)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(set)
}