简体   繁体   English

如何验证客户端完成的 http 请求的任何登录?

[英]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.我在 Go 中有一个 HTTP 服务器,当客户端尝试登录时,我必须验证凭据,作为回报,我想发送成功或失败。 Later if any request come, I need to authenticate it using tokenID generated and on success i have to send a file.稍后如果有任何请求,我需要使用生成的 tokenID 对其进行身份验证,成功后我必须发送一个文件。

I have tried the above using cookies.我已经尝试使用 cookie 进行上述操作。 But cookies values are visible when opening cookies tab.但是打开 cookie 选项卡时可以看到 cookie 值。 So i need to send encrypt cookie.所以我需要发送加密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.您不能一次发送两个单独的响应。您可以发送一个对象作为响应,其中包含服务器中文件的用户名密码和 url。

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".错误:=“http:多余的响应。WriteHeader 调用”。 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.实现您的目标的最佳方式是使用 cookie。 Send the data in the form of cookies and Bingo.以 cookie 和 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.此代码将创建一个 cookie,稍后您可以访问它。 This is the right way of achieving what you want.这是实现您想要的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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