Format and remove 09.1.md spaces

This commit is contained in:
vCaesar
2017-06-10 12:17:13 +08:00
parent be43d55cc1
commit 63e1f988a7

View File

@@ -51,8 +51,8 @@ CSRF的防御可以从服务端和客户端两方面着手防御效果是从
```Go ```Go
mux.Get("/user/:uid", getuser) mux.Get("/user/:uid", getuser)
mux.Post("/user/:uid", modifyuser) mux.Post("/user/:uid", modifyuser)
``` ```
这样处理后因为我们限定了修改只能使用POST当GET方式请求时就拒绝响应所以上面图示中GET方式的CSRF攻击就可以防止了但这样就能全部解决问题了吗当然不是因为POST也是可以模拟的。 这样处理后因为我们限定了修改只能使用POST当GET方式请求时就拒绝响应所以上面图示中GET方式的CSRF攻击就可以防止了但这样就能全部解决问题了吗当然不是因为POST也是可以模拟的。
@@ -67,32 +67,32 @@ CSRF的防御可以从服务端和客户端两方面着手防御效果是从
```Go ```Go
h := md5.New() h := md5.New()
io.WriteString(h, strconv.FormatInt(crutime, 10)) io.WriteString(h, strconv.FormatInt(crutime, 10))
io.WriteString(h, "ganraomaxxxxxxxxx") io.WriteString(h, "ganraomaxxxxxxxxx")
token := fmt.Sprintf("%x", h.Sum(nil)) token := fmt.Sprintf("%x", h.Sum(nil))
t, _ := template.ParseFiles("login.gtpl") t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, token) t.Execute(w, token)
``` ```
输出token 输出token
```html ```html
<input type="hidden" name="token" value="{{.}}"> <input type="hidden" name="token" value="{{.}}">
``` ```
验证token 验证token
```Go ```Go
r.ParseForm() r.ParseForm()
token := r.Form.Get("token") token := r.Form.Get("token")
if token != "" { if token != "" {
//验证token的合法性 //验证token的合法性
} else { } else {
//不存在token报错 //不存在token报错
} }
``` ```
这样基本就实现了安全的POST但是也许你会说如果破解了token的算法呢按照理论上是但是实际上破解是基本不可能的因为有人曾计算过暴力破解该串大概需要2的11次方时间。 这样基本就实现了安全的POST但是也许你会说如果破解了token的算法呢按照理论上是但是实际上破解是基本不可能的因为有人曾计算过暴力破解该串大概需要2的11次方时间。