Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
ef0d3d80
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ef0d3d80
编写于
8月 13, 2016
作者:
M
maxin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
set session.managerconfig public
上级
ce6f1987
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
43 addition
and
33 deletion
+43
-33
hooks.go
hooks.go
+14
-16
session/sess_cookie_test.go
session/sess_cookie_test.go
+11
-2
session/sess_mem_test.go
session/sess_mem_test.go
+7
-1
session/sess_test.go
session/sess_test.go
+2
-2
session/session.go
session/session.go
+9
-12
未找到文件。
hooks.go
浏览文件 @
ef0d3d80
...
...
@@ -45,26 +45,24 @@ func registerSession() error {
if
BConfig
.
WebConfig
.
Session
.
SessionOn
{
var
err
error
sessionConfig
:=
AppConfig
.
String
(
"sessionConfig"
)
conf
:=
new
(
session
.
ManagerConfig
)
if
sessionConfig
==
""
{
conf
:=
map
[
string
]
interface
{}{
"cookieName"
:
BConfig
.
WebConfig
.
Session
.
SessionName
,
"gclifetime"
:
BConfig
.
WebConfig
.
Session
.
SessionGCMaxLifetime
,
"providerConfig"
:
filepath
.
ToSlash
(
BConfig
.
WebConfig
.
Session
.
SessionProviderConfig
),
"secure"
:
BConfig
.
Listen
.
EnableHTTPS
,
"enableSetCookie"
:
BConfig
.
WebConfig
.
Session
.
SessionAutoSetCookie
,
"domain"
:
BConfig
.
WebConfig
.
Session
.
SessionDomain
,
"cookieLifeTime"
:
BConfig
.
WebConfig
.
Session
.
SessionCookieLifeTime
,
"enableSidInHttpHeader"
:
BConfig
.
WebConfig
.
Session
.
EnableSidInHttpHeader
,
"sessionNameInHttpHeader"
:
BConfig
.
WebConfig
.
Session
.
SessionNameInHttpHeader
,
"enableSidInUrlQuery"
:
BConfig
.
WebConfig
.
Session
.
EnableSidInUrlQuery
,
}
confBytes
,
err
:=
json
.
Marshal
(
conf
)
if
err
!=
nil
{
conf
.
CookieName
=
BConfig
.
WebConfig
.
Session
.
SessionName
conf
.
EnableSetCookie
=
BConfig
.
WebConfig
.
Session
.
SessionAutoSetCookie
conf
.
Gclifetime
=
BConfig
.
WebConfig
.
Session
.
SessionGCMaxLifetime
conf
.
Secure
=
BConfig
.
Listen
.
EnableHTTPS
conf
.
CookieLifeTime
=
BConfig
.
WebConfig
.
Session
.
SessionCookieLifeTime
conf
.
ProviderConfig
=
filepath
.
ToSlash
(
BConfig
.
WebConfig
.
Session
.
SessionProviderConfig
)
conf
.
Domain
=
BConfig
.
WebConfig
.
Session
.
SessionDomain
conf
.
EnableSidInHttpHeader
=
BConfig
.
WebConfig
.
Session
.
EnableSidInHttpHeader
conf
.
SessionNameInHttpHeader
=
BConfig
.
WebConfig
.
Session
.
SessionNameInHttpHeader
conf
.
EnableSidInUrlQuery
=
BConfig
.
WebConfig
.
Session
.
EnableSidInUrlQuery
}
else
{
if
err
=
json
.
Unmarshal
([]
byte
(
sessionConfig
),
conf
);
err
!=
nil
{
return
err
}
sessionConfig
=
string
(
confBytes
)
}
if
GlobalSessions
,
err
=
session
.
NewManager
(
BConfig
.
WebConfig
.
Session
.
SessionProvider
,
sessionConfig
);
err
!=
nil
{
if
GlobalSessions
,
err
=
session
.
NewManager
(
BConfig
.
WebConfig
.
Session
.
SessionProvider
,
conf
);
err
!=
nil
{
return
err
}
go
GlobalSessions
.
GC
()
...
...
session/sess_cookie_test.go
浏览文件 @
ef0d3d80
...
...
@@ -15,6 +15,7 @@
package
session
import
(
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
...
...
@@ -23,7 +24,11 @@ import (
func
TestCookie
(
t
*
testing
.
T
)
{
config
:=
`{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
globalSessions
,
err
:=
NewManager
(
"cookie"
,
config
)
conf
:=
new
(
ManagerConfig
)
if
err
:=
json
.
Unmarshal
([]
byte
(
config
),
conf
);
err
!=
nil
{
t
.
Fatal
(
"json decode error"
,
err
)
}
globalSessions
,
err
:=
NewManager
(
"cookie"
,
conf
)
if
err
!=
nil
{
t
.
Fatal
(
"init cookie session err"
,
err
)
}
...
...
@@ -56,7 +61,11 @@ func TestCookie(t *testing.T) {
func
TestDestorySessionCookie
(
t
*
testing
.
T
)
{
config
:=
`{"cookieName":"gosessionid","enableSetCookie":true,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
globalSessions
,
err
:=
NewManager
(
"cookie"
,
config
)
conf
:=
new
(
ManagerConfig
)
if
err
:=
json
.
Unmarshal
([]
byte
(
config
),
conf
);
err
!=
nil
{
t
.
Fatal
(
"json decode error"
,
err
)
}
globalSessions
,
err
:=
NewManager
(
"cookie"
,
conf
)
if
err
!=
nil
{
t
.
Fatal
(
"init cookie session err"
,
err
)
}
...
...
session/sess_mem_test.go
浏览文件 @
ef0d3d80
...
...
@@ -15,6 +15,7 @@
package
session
import
(
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
...
...
@@ -22,7 +23,12 @@ import (
)
func
TestMem
(
t
*
testing
.
T
)
{
globalSessions
,
_
:=
NewManager
(
"memory"
,
`{"cookieName":"gosessionid","gclifetime":10}`
)
config
:=
`{"cookieName":"gosessionid","gclifetime":10, "enableSetCookie":true}`
conf
:=
new
(
ManagerConfig
)
if
err
:=
json
.
Unmarshal
([]
byte
(
config
),
conf
);
err
!=
nil
{
t
.
Fatal
(
"json decode error"
,
err
)
}
globalSessions
,
_
:=
NewManager
(
"memory"
,
conf
)
go
globalSessions
.
GC
()
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
w
:=
httptest
.
NewRecorder
()
...
...
session/sess_test.go
浏览文件 @
ef0d3d80
...
...
@@ -89,7 +89,7 @@ func TestCookieEncodeDecode(t *testing.T) {
func
TestParseConfig
(
t
*
testing
.
T
)
{
s
:=
`{"cookieName":"gosessionid","gclifetime":3600}`
cf
:=
new
(
m
anagerConfig
)
cf
:=
new
(
M
anagerConfig
)
cf
.
EnableSetCookie
=
true
err
:=
json
.
Unmarshal
([]
byte
(
s
),
cf
)
if
err
!=
nil
{
...
...
@@ -103,7 +103,7 @@ func TestParseConfig(t *testing.T) {
}
cc
:=
`{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
cf2
:=
new
(
m
anagerConfig
)
cf2
:=
new
(
M
anagerConfig
)
cf2
.
EnableSetCookie
=
true
err
=
json
.
Unmarshal
([]
byte
(
cc
),
cf2
)
if
err
!=
nil
{
...
...
session/session.go
浏览文件 @
ef0d3d80
...
...
@@ -30,7 +30,6 @@ package session
import
(
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
...
...
@@ -82,7 +81,7 @@ func Register(name string, provide Provider) {
provides
[
name
]
=
provide
}
type
m
anagerConfig
struct
{
type
M
anagerConfig
struct
{
CookieName
string
`json:"cookieName"`
EnableSetCookie
bool
`json:"enableSetCookie,omitempty"`
Gclifetime
int64
`json:"gclifetime"`
...
...
@@ -100,7 +99,7 @@ type managerConfig struct {
// Manager contains Provider and its configuration.
type
Manager
struct
{
provider
Provider
config
*
m
anagerConfig
config
*
M
anagerConfig
}
// NewManager Create new Manager with provider name and json config string.
...
...
@@ -115,17 +114,12 @@ type Manager struct {
// 2. hashfunc default sha1
// 3. hashkey default beegosessionkey
// 4. maxage default is none
func
NewManager
(
provideName
,
config
strin
g
)
(
*
Manager
,
error
)
{
func
NewManager
(
provideName
string
,
cf
*
ManagerConfi
g
)
(
*
Manager
,
error
)
{
provider
,
ok
:=
provides
[
provideName
]
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"session: unknown provide %q (forgotten import?)"
,
provideName
)
}
cf
:=
new
(
managerConfig
)
cf
.
EnableSetCookie
=
true
err
:=
json
.
Unmarshal
([]
byte
(
config
),
cf
)
if
err
!=
nil
{
return
nil
,
err
}
if
cf
.
Maxlifetime
==
0
{
cf
.
Maxlifetime
=
cf
.
Gclifetime
}
...
...
@@ -142,7 +136,7 @@ func NewManager(provideName, config string) (*Manager, error) {
}
}
err
=
provider
.
SessionInit
(
cf
.
Maxlifetime
,
cf
.
ProviderConfig
)
err
:
=
provider
.
SessionInit
(
cf
.
Maxlifetime
,
cf
.
ProviderConfig
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -166,7 +160,7 @@ func NewManager(provideName, config string) (*Manager, error) {
// otherwise return an valid session id.
func
(
manager
*
Manager
)
getSid
(
r
*
http
.
Request
)
(
string
,
error
)
{
cookie
,
errs
:=
r
.
Cookie
(
manager
.
config
.
CookieName
)
if
errs
!=
nil
||
cookie
.
Value
==
""
||
cookie
.
MaxAge
<
0
{
if
errs
!=
nil
||
cookie
.
Value
==
""
{
var
sid
string
if
manager
.
config
.
EnableSidInUrlQuery
{
errs
:=
r
.
ParseForm
()
...
...
@@ -211,6 +205,9 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
}
session
,
err
=
manager
.
provider
.
SessionRead
(
sid
)
if
err
!=
nil
{
return
nil
,
errs
}
cookie
:=
&
http
.
Cookie
{
Name
:
manager
.
config
.
CookieName
,
Value
:
url
.
QueryEscape
(
sid
),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录