same = true
|
i = 1,#str1
do
-- If the two strings' lengths are different,sub()
|
c1 = str1:sub(i,i)
|
c1 ~= c2 false
|
end |
same
same = true
|
i = 1,#str1
do
-- If the two strings' lengths are different,sub()
|
c1 = str1:sub(i,i)
|
c1 ~= c2 false
|
end |
same
写一个处理器,来弹出一个基本的验证窗体不是很难,但是Go没有完美的文档,所以我必须自己一点点寻猎。其实非常简单,最终,这里就是HTTP基本验证的Go代码:
func handler(w http.ResponseWriter,r *http.Request) {
""
{
w.Header().Set("WWW-Authenticate" Nowrap; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,`Basic realm= "The kingdom of Stavros" `)
|
w.Write([]byte("401 Unauthorized\n" ))
|
else
fmt.Printf(
"Authenticated user %v.\n"
Nowrap; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,username)
setTokenCookie(w,token) |
"<html><head><script>location.reload()</script></head></html>"
)
} |
一旦我们验证了一个用户之后,我们需要给他们的口令设置一个cookie。我门只需要做我们用Lua做过的同样的事情,如上,只是更加简单,因为Go在标准库里面就包括一个真加密包。这个代码一样很直接明了,即使没有完全文档化:
mac := hmac.New(sha1.New,[]byte(mac.Write([]byte(fmt.Sprintf( "%v" Nowrap; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,expiration)))
|
expectedMAC := fmt.Sprintf("%x" Nowrap; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,mac.Sum( nil fmt.Sprintf( "%v:%s" Nowrap; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,expiration,expectedMAC)
|
func setTokenCookie(w http.ResponseWriter,token string) { |
cookie := http.Cookie{"MyToken" Nowrap; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,
|
"/" ".example.com" expire,monospace!important; font-size:10pt!important; min-height:inherit!important">expire.Format(time.UnixDate),monospace!important; font-size:10pt!important; min-height:inherit!important">3600,153)!important">false true rawCookie,monospace!important; font-size:10pt!important; min-height:inherit!important">[]string{rawCookie}}
|
25 | } |
来完成我们这一大段美妙的组合,我们只需要一个函数,用来检查由用户提供的验证信息,而且我们做到了!这里是我从一些库里面汲取出来的代码,当前它只是检查一个特定的用户名/密码的组合,所以和第三方的服务的集成就做为留给读者的作业吧:
"Authorization"
),monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">" "
Nowrap; margin:0px!important; padding:0px!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:auto!important; line-height:1.1em!important; font-family:Consolas,2)
len(s) != 2 || s[0] != "Basic" {
|
b,err := base64.StdEncoding.DecodeString(s[1])
nil
pair := strings.SplitN(string(b),2)
pair[0] != "username" || pair[1] != "password" pair[0]
|
} |
我到目前对于Nginx的Lua模块还是有着相当的喜欢。它允许你在web服务器的请求/响应周期里面做一些简单的操作,而且对于某些操作,比如为代理服务器做验证的检查,是很有意义的。这些事情对于一个不可编程的web服务器,一直很难,因此我们极可能需要写自己的HTTP代理服务。
上面的代码相当的简短,而且优雅,所以我对于上面的所有都感到高兴。我不能确定,这对于响应添加了多少额外的时间,不过,做一个验证是有好处的,我想这将值得去做(而且应该足够快,所以不是一个问题)。
另一个好处就是,你可以仅使用一个在Nginxlocationblock里面的单独的directive来开启它,所以没有需要跟踪的配置项。我发现,总体而言,这是一个非常优雅的@R_502_6280@案,而且我很高兴的了解到Nginx可以让我去做这样的事情,可能是将来我需要去做的。
如果你有任何建言或者是反馈,请留下你的评语(特别是如果我把某些地方给弄错了)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。