angela 发表于 2018-9-20 08:34:49

golang 请求带验证信息的坑

package middleware  

  

import (  

"encoding/base64"  
"github.com/astaxie/beego/context"
  
"github.com/astaxie/beego"
  
)
  
const(
  
HeaderAuthorization = "Authorization"
  
basic = "Basic"
  
)
  

  
func Author(ctx *context.Context) bool {
  
auth := ctx.Input.Header(HeaderAuthorization)
  
l := len(basic)
  

  
if len(auth) > l+1 && auth[:l] == basic {
  
b, err := base64.StdEncoding.DecodeString(auth)
  
if err != nil {
  
return false
  
}
  
cred := string(b)
  
for i := 0; i < len(cred); i++ {
  
if cred == ':' {
  
// Verify credentials
  
returnvalidator(cred[:i], cred)
  
}
  
}
  
}
  
return false
  
}
  
func validator(user string ,pass string) bool {
  
if user == beego.AppConfig.String("auth::appkey") && pass == beego.AppConfig.String("auth::appsecret"){
  
return true
  
}
  
return false
  
}


页: [1]
查看完整版本: golang 请求带验证信息的坑