Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
bb594c92
Commit
bb594c92
authored
May 12, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
异常处理显示
parent
08a1a844
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
5 deletions
+37
-5
CookiesHelper.java
...main/java/com/yanzuoguang/cloud/helper/CookiesHelper.java
+37
-5
No files found.
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/helper/CookiesHelper.java
View file @
bb594c92
...
...
@@ -25,6 +25,10 @@ public class CookiesHelper {
* 单个Cookies最大长度
*/
public
static
final
int
ITEM_MAX_SIZE
=
1000
;
/**
* 默认最大有效期
*/
public
static
final
int
DEFAULT_MAX_AGE
=
3
*
24
*
60
*
60
;
/**
* 编码方式
*/
...
...
@@ -44,10 +48,12 @@ public class CookiesHelper {
* 读取缓存Cookies
*
* @param key 键值
* @param remove 读取后是否删除
* @return 缓存的键值
*/
public
static
String
get
(
String
key
)
throws
UnsupportedEncodingException
{
public
static
String
get
(
String
key
,
boolean
remove
)
throws
UnsupportedEncodingException
{
HttpServletRequest
request
=
getRequest
();
HttpServletResponse
response
=
getResposne
();
if
(
request
.
getCookies
()
==
null
)
{
return
StringHelper
.
EMPTY
;
}
...
...
@@ -57,6 +63,7 @@ public class CookiesHelper {
// 遍历cookies找到对应的Cookies
for
(
Cookie
item
:
request
.
getCookies
())
{
if
(
item
.
getName
().
equals
(
key
))
{
removeCookie
(
response
,
item
.
getName
());
if
(
StringHelper
.
isNumber
(
item
.
getValue
()))
{
keyLength
=
StringHelper
.
toInt
(
item
.
getValue
());
continue
;
...
...
@@ -64,6 +71,7 @@ public class CookiesHelper {
return
URLDecoder
.
decode
(
item
.
getValue
(),
CHARSET
);
}
}
else
if
(
item
.
getName
().
startsWith
(
key
))
{
removeCookie
(
response
,
item
.
getName
());
map
.
put
(
key
,
item
.
getValue
());
}
}
...
...
@@ -106,17 +114,41 @@ public class CookiesHelper {
}
}
/**
* 删除Cookie对象
*
* @param response 输出流
* @param key 键值
*/
private
static
void
removeCookie
(
HttpServletResponse
response
,
String
key
)
{
addCookie
(
response
,
key
,
StringHelper
.
EMPTY
,
0
);
}
/**
* 添加Cookies缓存
*
* @param response 输出对象
* @param key 键
* @param value 值
*/
private
static
void
addCookie
(
HttpServletResponse
response
,
String
key
,
String
value
)
{
addCookie
(
response
,
key
,
value
,
DEFAULT_MAX_AGE
);
}
/**
* 添加Cookies缓存
*
* @param response 输出对象
* @param key 键
* @param value 值
* @param maxAge 最大有效期
*/
private
static
void
addCookie
(
HttpServletResponse
response
,
String
key
,
String
value
,
int
maxAge
)
{
// 创建cookie对象
Cookie
cookie
=
new
Cookie
(
key
,
value
);
// 设置根目录生效
cookie
.
setPath
(
"/"
);
cookie
.
setMaxAge
(
maxAge
);
// 服务器把cookie响应给客户端,所有的cookie对象,都会在服务器端创建,通过http响应给客户端(浏览器)
response
.
addCookie
(
cookie
);
}
...
...
@@ -160,7 +192,7 @@ public class CookiesHelper {
* 登录初始化
*/
public
static
void
tokenInit
()
throws
UnsupportedEncodingException
{
String
tokenRequest
=
get
(
CookiesHelper
.
TOKEN_STRING_KEY
);
String
tokenRequest
=
get
(
CookiesHelper
.
TOKEN_STRING_KEY
,
true
);
if
(!
StringHelper
.
isEmpty
(
tokenRequest
))
{
TokenHelper
.
setTokenString
(
tokenRequest
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment