代码拉取完成,页面将自动刷新
package main
import (
"github.com/emicklei/go-restful"
"io"
"net/http"
)
// This example shows how to create a (Route) Filter that performs Basic Authentication on the Http request.
//
// GET http://localhost:8080/secret
// and use admin,admin for the credentials
func main() {
ws := new(restful.WebService)
ws.Route(ws.GET("/secret").Filter(basicAuthenticate).To(secret))
restful.Add(ws)
http.ListenAndServe(":8080", nil)
}
func basicAuthenticate(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
encoded := req.Request.Header.Get("Authorization")
// usr/pwd = admin/admin
// real code does some decoding
if len(encoded) == 0 || "Basic YWRtaW46YWRtaW4=" != encoded {
resp.AddHeader("WWW-Authenticate", "Basic realm=Protected Area")
resp.WriteErrorString(401, "401: Not Authorized")
return
}
chain.ProcessFilter(req, resp)
}
func secret(req *restful.Request, resp *restful.Response) {
io.WriteString(resp, "42")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。