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
f21e0908
Commit
f21e0908
authored
May 12, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
异常处理显示
parent
4499d371
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
0 deletions
+158
-0
WebAspect.java
...ud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
+3
-0
CookiesHelper.java
...main/java/com/yanzuoguang/cloud/helper/CookiesHelper.java
+155
-0
No files found.
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
View file @
f21e0908
package
com
.
yanzuoguang
.
cloud
.
aop
;
import
com.yanzuoguang.cloud.CloudContans
;
import
com.yanzuoguang.cloud.helper.CookiesHelper
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
...
...
@@ -57,6 +58,7 @@ public class WebAspect extends BaseRequestAspect {
long
start
=
System
.
currentTimeMillis
();
String
name
=
joinPoint
.
getSignature
().
getName
();
try
{
CookiesHelper
.
tokenInit
();
logger
.
info
(
"[ {} ] request params is {}"
,
name
,
joinPoint
.
getArgs
());
Object
result
=
executeMethod
(
joinPoint
,
name
);
...
...
@@ -80,6 +82,7 @@ public class WebAspect extends BaseRequestAspect {
throw
e
;
}
}
finally
{
CookiesHelper
.
tokenFinish
();
Log
.
threadCommit
();
saveInterLogs
(
joinPoint
,
responseResult
);
}
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/helper/CookiesHelper.java
0 → 100644
View file @
f21e0908
package
com
.
yanzuoguang
.
cloud
.
helper
;
import
com.yanzuoguang.token.TokenHelper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.helper.UrlHelper
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.Cookie
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 获取Cookies对象
*
* @author 颜佐光
*/
public
class
CookiesHelper
{
/**
* 单个Cookies最大长度
*/
private
static
final
int
ITEM_MAX_SIZE
=
1000
;
/**
* 编码方式
*/
private
static
final
String
CHARSET
=
"UTF-8"
;
/**
* Cookies标记格式
*/
private
static
final
String
TAG_FORMAT
=
"%s_%d"
;
/**
* TOKEN缓存对象
*/
public
static
final
String
TOKEN_STRING_KEY
=
"T_S"
;
/**
* 读取缓存Cookies
*
* @param key 键值
* @return 缓存的键值
*/
public
static
String
get
(
String
key
)
{
HttpServletRequest
request
=
getRequest
();
Map
<
String
,
String
>
map
=
new
HashMap
<>();
int
keyLength
=
0
;
// 遍历cookies找到对应的Cookies
for
(
Cookie
item
:
request
.
getCookies
())
{
if
(
item
.
getName
().
equals
(
key
))
{
if
(
StringHelper
.
isNumber
(
item
.
getValue
()))
{
keyLength
=
StringHelper
.
toInt
(
item
.
getValue
());
continue
;
}
else
{
return
UrlHelper
.
decoding
(
item
.
getValue
(),
CHARSET
);
}
}
else
if
(
item
.
getName
().
startsWith
(
key
))
{
map
.
put
(
key
,
item
.
getValue
());
}
}
// 循环处理多个Cookeis
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
keyLength
;
i
++)
{
String
itemKey
=
String
.
format
(
TAG_FORMAT
,
key
,
i
);
if
(!
map
.
containsKey
(
itemKey
))
{
throw
new
CodeException
(
"Cookies缺少["
+
itemKey
+
"]"
);
}
sb
.
append
(
map
.
get
(
itemKey
));
}
return
UrlHelper
.
decoding
(
sb
.
toString
(),
CHARSET
);
}
/**
* 写入缓存Cookies
*
* @param key 键
* @param value 值
*/
public
static
void
set
(
String
key
,
String
value
)
{
HttpServletResponse
response
=
getResposne
();
String
toValue
=
UrlHelper
.
encoding
(
value
,
CHARSET
);
if
(
toValue
.
length
()
<
ITEM_MAX_SIZE
)
{
// 创建cookie对象
Cookie
cookie
=
new
Cookie
(
key
,
value
);
// 服务器把cookie响应给客户端,所有的cookie对象,都会在服务器端创建,通过http响应给客户端(浏览器)
response
.
addCookie
(
cookie
);
}
else
{
int
page
=
StringHelper
.
getPage
(
toValue
.
length
(),
ITEM_MAX_SIZE
);
{
// 总Cookies数量
Cookie
cookie
=
new
Cookie
(
key
,
String
.
valueOf
(
page
));
response
.
addCookie
(
cookie
);
}
for
(
int
i
=
0
;
i
<=
page
;
i
++)
{
String
itemKey
=
String
.
format
(
TAG_FORMAT
,
key
,
i
);
int
start
=
i
*
ITEM_MAX_SIZE
;
int
size
=
Math
.
min
(
page
-
start
,
ITEM_MAX_SIZE
);
int
end
=
start
+
size
-
1
;
String
itemValue
=
toValue
.
substring
(
start
,
end
);
Cookie
cookie
=
new
Cookie
(
itemKey
,
itemValue
);
response
.
addCookie
(
cookie
);
}
}
}
/**
* 获取请求对象
*
* @return Servlet请求对象
*/
public
static
HttpServletRequest
getRequest
()
{
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
())
.
getRequest
();
return
request
;
}
/**
* 获取请求对象
*
* @return Servlet请求对象
*/
public
static
HttpServletResponse
getResposne
()
{
HttpServletResponse
response
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
())
.
getResponse
();
return
response
;
}
/**
* 登录结束处理
*/
public
static
void
tokenFinish
()
{
String
tokenResponse
=
TokenHelper
.
getTokenString
();
if
(!
StringHelper
.
isEmpty
(
tokenResponse
))
{
set
(
TOKEN_STRING_KEY
,
tokenResponse
);
}
TokenHelper
.
remove
();
}
/**
* 登录初始化
*/
public
static
void
tokenInit
()
{
String
tokenRequest
=
get
(
CookiesHelper
.
TOKEN_STRING_KEY
);
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