简体   繁体   中英

How can I Authenticate any login for a http request done by client?

I have an HTTP server in Go in which when client is trying to login I have to authenticate credentials and in return i want to send success or failure. Later if any request come, I need to authenticate it using tokenID generated and on success i have to send a file.

I have tried the above using cookies. But cookies values are visible when opening cookies tab. So i need to send encrypt cookie. Please tell me a way to do so IF its possible.

sending username and password is a response , serving a file is a response too. You can't send two separate response at once.You can send an object as response containing username password and url of the file in the server.

You just send only one response, but we can combine multipart responses in one with some pattern.

Like this:

{
  "username": "xxxx",
  "password": "xxxx",
  "file": "file uri"
}

ERROR:= "http: superfluous response.WriteHeader call". This error is coming as you cannot send two response for one request.

The best way to achieve what you are trying to do is by using cookies. Send the data in the form of cookies and Bingo. Your work will be done without errors/warnings.

expiration := time.Now().Add(time.Second * time.Duration(1000))
cookie := http.Cookie{Name: "Token", Value: "username", Expires: expiration}
http.SetCookie(w, &cookie)
usercookie := http.Cookie{Name: "usercookie", Value: "username", Expires: expiration}
http.SetCookie(w, &usercookie)
http.ServeFile(w, r, r.URL.Path[1:])

This Code will create a cookie and later you can access it. This is the right way of achieving what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM