Exemplo n.º 1
0
Arquivo: md5.go Projeto: fyx912/token
func main() {
	hash := md5.New()
	b := []byte("test")
	hash.Write(b)
	fmt.Printf("%x %x\n", hash.Sum(nil), md5.Sum(b))
	hash.Write(nil)
	fmt.Printf("%x %x\n", hash.Sum(b), hash.Sum(nil))

	fmt.Println(uuid())
	fmt.Println(name())

	username, passsword := conf.GetUsers()
	fmt.Println("username="******"passsword=", passsword)
}
Exemplo n.º 2
0
func Login(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.RequestURI)
	getSessions, sessionId := utils.GetSessionAndSessionId(w, r)
	defer getSessions.SessionRelease(w) //释放session

	fmt.Println(" session ID=", sessionId)
	w.Header().Add("Content-Type", "text/html;charset=UTF-8")
	method := r.Method
	if method == "GET" {
		t, _ := template.ParseFiles("views/login.html")
		t.Execute(w, nil)
	} else {
		contentType := r.Header.Get("Content-Type")
		if strings.Contains(contentType, "application/json") {
			jsonData, err := ioutil.ReadAll(r.Body)
			utils.CheckError(err)
			defer r.Body.Close()
			if string(jsonData) != "" {
				user := make(map[string]string)
				err = json.Unmarshal(jsonData, &user)
				utils.CheckError(err)
				fmt.Println("body=", r.Body)
				fmt.Println("body..jsonData=", string(jsonData))

				//去除string的前后空白,通过map获取json数据
				requseUsername := strings.TrimSpace(user["username"])
				requestPassword := strings.TrimSpace(user["password"])

				//处理用户重复登录
				userHandle(r, requseUsername)
				// getSessions = USER_SESSION[requseUsername]
				//保存username到sessionId
				//SESSIONID_USER = new map[string]session.SessionStore
				utils.SESSIONID_USER[sessionId] = requseUsername
				//保存session到uesrname
				getSessions.Set("sessionId", sessionId)
				utils.USER_SESSION[requseUsername] = getSessions

				fmt.Println("body=", requseUsername, user["password"])

				username, password := conf.GetUsers()
				if requseUsername == username {
					if requestPassword == password {
						// t, _ := template.ParseFiles("views/login.gtpl")
						// t.Execute(w, nil)
						getSessions.Set("username", requseUsername)
						data := utils.OutputJsonData(0, "登陆成功!")
						fmt.Fprintln(w, data)
						fmt.Println("登陆成功!")
					} else {
						data := utils.OutputJsonData(1, "输入的密码有误!")
						fmt.Fprintln(w, data)
					}
				} else {
					data := utils.OutputJsonData(1, "账号不存在!")
					fmt.Fprintln(w, data)
				}
			} else {
				data := utils.OutputJsonData(1, "请求的内容不能为空!")
				fmt.Fprintln(w, data)
			}
		} else {
			data := utils.OutputJsonData(1, "请求的格式必须为json!")
			fmt.Fprintln(w, data)
		}
	}
}