Go语言在HTTP服务器、路由、中间件及恶意攻击场景中的应用
1. 无授权请求与HTML模板响应
在进行HTTP请求时,如果不携带凭证,中间件会返回401未授权错误;而携带有效凭证进行相同请求,则会得到仅认证用户可访问的超级机密问候消息。例如:
$ curl -i 'http://localhost:8000/hello?username=admin&password=password'
HTTP/1.1 200 OK
Date: Thu, 16 Jan 2020 20:41:05 GMT
Content-Length: 9
Content-Type: text/plain; charset=utf-8
Hi admin
接下来,我们可以使用Go的模板包以更动态的方式返回HTML。Go有两个模板包, text/template 和 html/template ,这里我们使用 html/template ,因为它提供了所需的上下文编码。
以下是一个简单的示例,展示如何创建和使用模板:
package main
import (
"html/template"
"os"
)
var x = `
<html>
<body>
Hello {
{.}}
</body>
</html>
`
func main() {
t, err
超级会员免费看
订阅专栏 解锁全文

1185

被折叠的 条评论
为什么被折叠?



