• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

使用go搭建一个简单的web服务器(5)防止多次递交表单

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.前端登陆页面

<html>
<head>
<title>login</title>
</head>
<body>
<form action="http://127.0.0.1:9090/login" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="hidden" name="token" value="{{.}}">
<input type="submit" value="登陆">
</form>
</body>
</html>

2.后端处理逻辑

package main

import (
    "crypto/md5"
    "fmt"
    "html/template"
    "io"
    "log"
    "net/http"
    "strconv"
    "strings"
    "time"
)

func sayhelloname(w http.ResponseWriter, r *http.Request) {
    r.ParseForm() //解析参数,默认是不会解析的。
    fmt.Println(r.Form)
    fmt.Println("path:", r.URL.Path)
    fmt.Println("scheme:", r.URL.Scheme)
    fmt.Println(r.Form["url_long"])
    for k, v := range r.Form {
        fmt.Println("key:", k)
        fmt.Println("value:", strings.Join(v, ","))
    }
    fmt.Fprintf(w, "hello, welcome you!") //这个字符串写入到w中,用于返回给客户端。
}
func login(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method: ", r.Method)
    if r.Method == "GET" {
        //begin这里开始计算一个时间戳用于填充到模板中的隐藏标签中
        currenttime := time.Now().Unix()
        h := md5.New()
        io.WriteString(h, strconv.FormatInt(currenttime, 10))
        token := fmt.Sprintf("%x", h.Sum(nil))
        //end
        t, _ := template.ParseFiles("login.html")
        t.Execute(w, token)
    } else {
        r.ParseForm()
        //begin利用token防止用户多次递交相同的表单
        token := r.Form.Get("token")
        if token != "" {
            //验证token的合法性
        } else {
            //不存在token报错
        }
        fmt.Println("username length:", len(r.Form["username"][0]))
        fmt.Println("username:", template.HTMLEscapeString(r.Form.Get("username"))) //输出到服务器端
        fmt.Println("password:", template.HTMLEscapeString(r.Form.Get("password"))) //输出到服务器端
        template.HTMLEscape(w, []byte(r.Form.Get("username")))                      //输出到客户端
        //end
    }
}
func main() {
    http.HandleFunc("/", sayhelloname)       //设置访问的路由
    http.HandleFunc("/login", login)         //设置访问的路由
    err := http.ListenAndServe(":9090", nil) //设置监听的端口
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

 

strconv.FormatInt(i int64,base int)string
-> 返回的i的base进制的字符串表示,base必须在2-36之间,结果中会使用小写字母a到z表示大于10的数字

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go最新版本1.15安装配置及编辑器2020.2版本goland发布时间:2022-07-10
下一篇:
GoWeb开发之Revel-返回值发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap