- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我已经设置了一个 Go rest api。在登录时我这样做:
session, _ := store.New(r, sessionId)
session.Options.MaxAge = 12 * 3600
err := session.Save(r, w)
//treat error
为了检查 session ,我有这样的东西:
session, err := store.Get(r, sessionId)
//treat error
if session.IsNew {
http.Error(w, "Unauthorized session.", http.StatusUnauthorized)
return
}
如果我执行来自 postman 的请求,它工作正常,但是当我从我的客户那里执行请求时,我得到 401。你们有没有遇到过这样的事情?该商店是一个 CookieStore。
我已经检查了 id,我用静态字符串替换了 sessionId 变量。 gorilla session 使用 gorilla 上下文来注册一个新请求,当我从 postman 那里发出请求时 context.data[r]
不是空的,但来自客户端的请求总是空的 ->总是一个新的 session 。
https://github.com/gorilla/context/blob/master/context.go - 第 33 行
它被称为
https://github.com/gorilla/sessions/blob/master/sessions.go - 第 122 行
在 CookieStore.Get 函数中用到了
https://github.com/gorilla/sessions/blob/master/store.go - 第 77 行
编辑 1:对于我使用聚合物的客户端,我也尝试了 xmlhttp。聚合物:
<iron-ajax
id="ajaxRequest"
auto
url="{{requestUrl}}"
headers="{{requestHeaders}}"
handle-as="json"
on-response="onResponse"
on-error="onError"
content-type="application/json"
>
</iron-ajax>
和处理程序
onResponse: function(response){
console.log(response.detail.response);
this.items = response.detail.response
},
onError: function(error){
console.log(error.detail)
},
ready: function(){
this.requestUrl = "http://localhost:8080/api/fingerprint/company/" + getCookie("companyId");
this.requestHeaders = {"Set-cookie": getCookie("api_token")}
}
cookie 成功到达后端。
和 xmlhttp:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if(xmlhttp.status == 200){
//do stuff
}else if(xmlhttp.status == 401){
page.redirect("/unauthorized")
}else{
page.redirect("/error")
}
}
}
xmlhttp.open("GET","http://localhost:8080/api/fingerprint/company/" + getCookie("companyId"),true);
xmlhttp.setRequestHeader("Set-cookie", getCookie("api_token"));
xmlhttp.send();
编辑 2:
所以我尝试使用 fiddler 进行调试(感谢您的建议),我发现 postman 的请求有一个粗体条目 Cookies/Login
而来自客户端的请求没有。知道如何获取/设置该值吗?它以某种方式在 Postman 中自动设置。在身份验证请求中,我得到一个 set-cookie header ,其中包含我需要的所有数据,但我无法在客户端上获取它。我得到 Refused to get unsafe header set-cookie
。
最佳答案
问题是,在客户端中,请求需要有 withCredentials = true
,然后浏览器会处理所有事情。它从 set-cookie
header 获取 cookie,并通过 cookie
header 发送 cookie。所以,毕竟这不是 gorilla session 问题。
关于session - Gorilla session 不适用于客户端的 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31448642/
在围棋中,我尝试了简单的数据库连接。我需要进口大猩猩/MUX,但我不能。。我正在使用VS代码。在cd进入我的项目目录之后,我创建了main.go并运行了go get-u githorb.com/Gor
我得到 session ,来 self 曾经使用的 PHP 在服务器端设置一个或多个键及其值,并能够检索或覆盖它,直到 session 过期。 与 gorilla / session 相同 var(
在 Go 中,我尝试了简单的数据库连接。我需要导入 gorilla/mux ,但我做不到。 我正在使用 VS 代码。之后 cd转到我的项目目录,我创建了 main.go确实跑了go get -u gi
尝试使用 github.com/gorilla/mux 配置 go 服务器路由以响应所有带有 index.html 的请求,但排除扩展名为 .jpg|.js|.png 的请求 由于扩展名而被排除的静态
我从表格中收到日期。该日期具有特定格式“dd/mm/yyyy”。 为了将其解析为我的结构,我使用 gorilla/schema 包,但该包无法将接收到的数据识别为日期。 我如何解析日期并将其放入正确的
我有一个客户端的websocket代码看起来像这样 c, _, err := websocket.DefaultDialer.Dial("", nil) if err != nil {
我正在学习 Go 并遇到了 gorilla/mux 路由器。我想在文本文件中有动态路由,例如: /user/1/post/1 为此,我编写了以下代码: func (s *Server) RegRout
因此,我知道 Gorilla Mux 不支持可选的查询参数,人们建议使用查询参数创建不同的路由,从而使其更加牢固。但是在我的情况下,无法按预期的方式解析路线。 如果我调用/service/{local
我正在使用来自 gorilla websockets 的聊天应用程序示例,但我有一个问题,有时,当后端需要向客户端发送两条不同的消息时,它们只在一个消息事件中发送,这对我来说很糟糕,因为JSON.pa
我想通过 gorilla /websocket发送图像数据的字节,我当前的代码是: var b bytes.Buffer empty := bufio.NewWriter(&b) png.Encode
我正在访问一些我以前写的简单Web服务器的旧代码,并且我的请求模式不再起作用。我有一个初始化我的路线的函数,如下所示: func (a *App) InitializeRoutes() { a
我想创建一个/user 子路由器,如下所示 user := app.Router.PathPrefix("/user").Subrouter() user.HandleFunc("/create",
我尝试关闭连接手册(使用 Dark WebSocket Terminal ),但客户端告诉我 1005(无状态 Rcvd) 服务器: ReadLoop: for { mt, message,
我有以下代码: r := mux.NewRouter() r.HandleFunc("/", homeHandler) r.HandleFunc("/login", loginHandler) adm
请看下面我的代码。如您所见,这是一个具有两个端点的简单服务: /test <-- 使用中间件保护的基本身份验证 /static <-- 提供 ./static 目录中的所有文件 现在我也在尝试将基本身
// SetReadLimit sets the maximum size for a message read from the peer. If a // message exceeds the
我有以下路线: router.Methods("POST").Path("/my_post_01").HandlerFunc(myHandler1) router.Methods("GET").Pat
因此,我正在使用 Go 和 Gorilla Mux 开发一个简单的 RESTful API。我的第二条路线不工作时遇到问题,它返回 404 错误。我不确定问题出在哪里,因为我是 Go 和 Gorill
我正在 Golang 中构建一个与 Gorilla websockets 通信的网关。 我在 Ubuntu 16.04 上运行它,目前正在使用 .NET 控制台应用程序对其进行测试。 在 Window
我无法让 gorilla mux 工作.. 当请求 http://www.localhost:9000 时,Web 服务器返回 404 page not found 但这有效 http://local
我是一名优秀的程序员,十分优秀!