golang的HTTP基本认证机制实例详解
程序员文章站
2022-08-29 08:30:20
本文实例讲述了golang的http基本认证机制。分享给大家供大家参考,具体如下:
看了<>第12章http基本认证机制(本站...
本文实例讲述了golang的http基本认证机制。分享给大家供大家参考,具体如下:
看了<<http权威指南>>第12章http基本认证机制(本站下载地址:),感觉讲的蛮详细的,写了一个小小例子测试.
请求响应过程:
复制代码 代码如下:
==>
get /hello http/1.1
host: 127.0.0.1:12345
<==
http/1.1 401 unauthorized
www-authenticate: basic realm="dotcoo user login"
==>
get /hello http/1.1
host: 127.0.0.1:12345
authorization: basic ywrtaw46ywrtaw5wd2q=
<==
http/1.1 200 ok
content-type: text/plain; charset=utf-8
get /hello http/1.1
host: 127.0.0.1:12345
<==
http/1.1 401 unauthorized
www-authenticate: basic realm="dotcoo user login"
==>
get /hello http/1.1
host: 127.0.0.1:12345
authorization: basic ywrtaw46ywrtaw5wd2q=
<==
http/1.1 200 ok
content-type: text/plain; charset=utf-8
golang http基本认证机制的实现
复制代码 代码如下:
package main
import (
"fmt"
"io"
"net/http"
"log"
"encoding/base64"
"strings"
)
// hello world, the web server
func helloserver(w http.responsewriter, req *http.request) {
auth := req.header.get("authorization")
if auth == "" {
w.header().set("www-authenticate", `basic realm="dotcoo user login"`)
w.writeheader(http.statusunauthorized)
return
}
fmt.println(auth)
auths := strings.splitn(auth, " ", 2)
if len(auths) != 2 {
fmt.println("error")
return
}
authmethod := auths[0]
authb64 := auths[1]
switch authmethod {
case "basic":
authstr, err := base64.stdencoding.decodestring(authb64)
if err != nil {
fmt.println(err)
io.writestring(w, "unauthorized!\n")
return
}
fmt.println(string(authstr))
userpwd := strings.splitn(string(authstr), ":", 2)
if len(userpwd) != 2 {
fmt.println("error")
return
}
username := userpwd[0]
password := userpwd[1]
fmt.println("username:", username)
fmt.println("password:", password)
fmt.println()
default:
fmt.println("error")
return
}
io.writestring(w, "hello, world!\n")
}
func main() {
http.handlefunc("/hello", helloserver)
err := http.listenandserve(":12345", nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
import (
"fmt"
"io"
"net/http"
"log"
"encoding/base64"
"strings"
)
// hello world, the web server
func helloserver(w http.responsewriter, req *http.request) {
auth := req.header.get("authorization")
if auth == "" {
w.header().set("www-authenticate", `basic realm="dotcoo user login"`)
w.writeheader(http.statusunauthorized)
return
}
fmt.println(auth)
auths := strings.splitn(auth, " ", 2)
if len(auths) != 2 {
fmt.println("error")
return
}
authmethod := auths[0]
authb64 := auths[1]
switch authmethod {
case "basic":
authstr, err := base64.stdencoding.decodestring(authb64)
if err != nil {
fmt.println(err)
io.writestring(w, "unauthorized!\n")
return
}
fmt.println(string(authstr))
userpwd := strings.splitn(string(authstr), ":", 2)
if len(userpwd) != 2 {
fmt.println("error")
return
}
username := userpwd[0]
password := userpwd[1]
fmt.println("username:", username)
fmt.println("password:", password)
fmt.println()
default:
fmt.println("error")
return
}
io.writestring(w, "hello, world!\n")
}
func main() {
http.handlefunc("/hello", helloserver)
err := http.listenandserve(":12345", nil)
if err != nil {
log.fatal("listenandserve: ", err)
}
}
希望本文所述对大家go语言程序设计有所帮助。
上一篇: golang解析xml的方法
下一篇: golang简单tls协议用法完整示例