我正在构建golang简单的Web身份验证。
我找到了一个包含Cookie的简单示例,但是它不起作用。
它输出md5并且即使我在文本字段中写了东西也保持不变
开始:
package main
import (
"net/http"
"html/template"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
url := "path/to/login.html" //login page
if len(r.Header["Cookie"]) != 0 && r.Header["Cookie"][0] == "auth=your_MD5_cookies" {
url = "path/to/index.html" //page after login
}
t, _ := template.ParseFiles(url); t.Execute(w, "")
})
http.Handle("/js/", http.FileServer(http.Dir("path/to")))
http.ListenAndServe(":8000", nil)
login.html
<html>
<head>
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/js.cookie.min.js"></script>
<script src="js/jquery.md5.min.js"></script>
</head>
<body>
<p>Login: <input id="login" type="text" /></p>
<p>Password: <input id="pwd" type="password" /></p>
<button id="button">Войти</button>
<script>
$('#button').on('click', function () {
var auth = $.MD5($("#login").val()+$("#pwd").val());
Cookies.set("auth", auth);
location.reload();
});
</script>
</body>
</html>