增加了令牌验证的ä¸两个方法

master
徐俊杰 3 years ago
parent bcc1694c2c
commit 8af59d845b

@ -0,0 +1,41 @@
/*
* @FileName: auth.go
* @Author: JuneXu
* @CreateTime: 2022/3/24 12:09
* @Description:
*/
package simpleRequest
import (
"encoding/base64"
"fmt"
)
type Authorization struct {
simpleReq *SimpleRequest
}
//
// Basic
// @Description: 身份验证使用bearer 令牌bearer 令牌
// @receiver s
// @param username
// @param password
//
func (s *Authorization) Bearer(token string) {
s.simpleReq.headers.Set("Authorization", fmt.Sprintf("bearer %v", token))
}
//
// Basic
// @Description: 身份验证的基本验证方案
// @receiver s
// @param username
// @param password
//
func (s *Authorization) Basic(username, password string) {
authStr := fmt.Sprintf("%v:%v", username, password)
data := base64.StdEncoding.EncodeToString([]byte(authStr))
s.simpleReq.headers.Set("Authorization", fmt.Sprintf("basic %v", data))
}

@ -75,18 +75,29 @@ func (s *SimpleRequest) NewRequest() *SimpleRequest {
//------------------------------------------------------
//
// 数据准备
//
//Authorization 添加令牌的方法集合
func (s *SimpleRequest) Authorization() *Authorization {
return &Authorization{
simpleReq: s,
}
}
//Headers 添加请求头
func (s *SimpleRequest) Headers() *HeadersConf {
return &HeadersConf{
simpleReq: s,
}
}
//Body 添加请求体
func (s *SimpleRequest) Body() *BodyConf {
return &BodyConf{
simpleReq: s,
}
}
//QueryParams 添加url后面的参数
func (s *SimpleRequest) QueryParams() *QueryParams {
return &QueryParams{
simpleReq: s,

@ -0,0 +1,21 @@
/*
* @FileName: base64_test.go
* @Author: JuneXu
* @CreateTime: 2022/3/24 12:03
* @Description:
*/
package test
import (
"encoding/base64"
"fmt"
"testing"
)
func TestBs(t *testing.T) {
authStr := fmt.Sprintf("%v:%v", "aaa", "bbb")
data := base64.StdEncoding.EncodeToString([]byte(authStr))
t.Log(data)
} //YWFhOmJiYg==
Loading…
Cancel
Save