• 使用go搭建一个简单的web服务器(4)预防跨站脚本


    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="submit" value="登陆">
    </form>
    </body>
    </html>

    2.后端处理逻辑

    package main
    
    import (
        "fmt"
        "html/template"
        "log"
        "net/http"
        "strings"
        text_template "text/template"
    )
    
    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" {
            t, _ := template.ParseFiles("login.html")
            t.Execute(w, nil)
        } else {
            r.ParseForm()
            //预防跨站脚本攻击
            way := 3
            if way == 1 {
                //将html标签进行转义
                fmt.Println("方法一")
                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"))) //将转义后的用户名字段发送给客户端
            } else if way == 2 {
                //将html标签进行转义
                fmt.Println("方法二")
                t, err := template.New("foo").Parse(`{{define "T"}}Hello,{{.}}!{{end}}`)
                err = t.ExecuteTemplate(w, "T", "<script>alert('you have been login')</script>") //将一段含有脚本的内容发送给客户端
                if err != nil {
                    fmt.Println(err)
                }
            } else if way == 3 {
                //在编辑文章时,有时候就想在文章中加入带有html标签的代码来作为示例,那么就不能使用上面的方法进行转义。
                fmt.Println("方法三")
                t, err := template.New("foo").Parse(`{{define "T"}}Hello,{{.}}!{{end}}`)
                err = t.ExecuteTemplate(w, "T", template.HTML("<script>alert('you have been login')</script>")) //将一段含有脚本的内容发送给客户端
                if err != nil {
                    fmt.Println(err)
                }
            } else {
                //在编辑文章时,有时候就想在文章中加入带有html标签的代码来作为示例,那么就不能使用上面的方法进行转义。
                fmt.Println("方法四")
                t, err := text_template.New("foo").Parse(`{{define "T"}}Hello,{{.}}!{{end}}`)
                err = t.ExecuteTemplate(w, "T", "<script>alert('you have been login')</script>") //将一段含有脚本的内容发送给客户端
                if err != nil {
                    fmt.Println(err)
                }
            }
    
        }
    }
    func main() {
        http.HandleFunc("/", sayhelloname)       //设置访问的路由
        http.HandleFunc("/login", login)         //设置访问的路由
        err := http.ListenAndServe(":9090", nil) //设置监听的端口
        if err != nil {
            log.Fatal("ListenAndServe: ", err)
        }
    }
    人生短,迷茫路一程又一程。 脚步重,雨雪天踟蹰也踟蹰。 滴水聚,久无成效伊人不见。 该如何,敲击敲击昼夜不停。
  • 相关阅读:
    Winform—C#读写config配置文件
    C# 中Web.config文件的读取与写入
    Redis配置文件详解
    三层架构之泛型抽象
    Linq To Sql语法及实例大全
    junit单元测试(keeps the bar green to keeps the code clean)
    观 GT Java语言管理系统的感悟
    java考核完的心得
    15个C++项目列表
    C++文件操作(fstream)
  • 原文地址:https://www.cnblogs.com/DesignerA/p/11558692.html
Copyright © 2020-2023  润新知