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
066e7d74
Commit
066e7d74
authored
May 13, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
0ef7b11a
f90d0a11
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
448 additions
and
0 deletions
+448
-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
+204
-0
TokenData.java
...til-db/src/main/java/com/yanzuoguang/token/TokenData.java
+63
-0
TokenHelper.java
...l-db/src/main/java/com/yanzuoguang/token/TokenHelper.java
+178
-0
No files found.
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
View file @
066e7d74
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
;
...
...
@@ -52,6 +53,7 @@ public class WebAspect extends BaseRequestAspect {
@Around
(
value
=
"webAspect()"
)
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Log
.
threadBegin
();
CookiesHelper
.
tokenInit
();
// 用户数据库记录
ResponseResult
responseResult
=
null
;
long
start
=
System
.
currentTimeMillis
();
...
...
@@ -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 @
066e7d74
package
com
.
yanzuoguang
.
cloud
.
helper
;
import
com.yanzuoguang.token.TokenHelper
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.StringHelper
;
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.io.UnsupportedEncodingException
;
import
java.net.URLDecoder
;
import
java.net.URLEncoder
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 获取Cookies对象
*
* @author 颜佐光
*/
public
class
CookiesHelper
{
/**
* 单个Cookies最大长度
*/
public
static
final
int
ITEM_MAX_SIZE
=
1000
;
/**
* 默认最大有效期
*/
public
static
final
int
DEFAULT_MAX_AGE
=
3
*
24
*
60
*
60
;
/**
* 编码方式
*/
public
static
final
String
CHARSET
=
"UTF-8"
;
/**
* Cookies标记格式
*/
public
static
final
String
TAG_FORMAT
=
"%s_%d"
;
/**
* TOKEN缓存对象
*/
public
static
final
String
TOKEN_STRING_KEY
=
"T_S"
;
/**
* 读取缓存Cookies
*
* @param key 键值
* @param remove 读取后是否删除
* @return 缓存的键值
*/
public
static
String
get
(
String
key
,
boolean
remove
)
throws
UnsupportedEncodingException
{
HttpServletRequest
request
=
getRequest
();
HttpServletResponse
response
=
getResposne
();
if
(
request
.
getCookies
()
==
null
)
{
return
StringHelper
.
EMPTY
;
}
Map
<
String
,
String
>
map
=
new
HashMap
<>();
int
keyLength
=
0
;
// 遍历cookies找到对应的Cookies
for
(
Cookie
item
:
request
.
getCookies
())
{
String
itemKey
=
item
.
getName
();
String
itemValue
=
item
.
getValue
();
if
(
itemKey
.
equals
(
key
))
{
removeCookie
(
response
,
itemKey
);
if
(
StringHelper
.
isEmpty
(
itemValue
))
{
return
StringHelper
.
EMPTY
;
}
else
if
(
StringHelper
.
isNumber
(
itemValue
))
{
keyLength
=
StringHelper
.
toInt
(
itemValue
);
continue
;
}
else
{
return
URLDecoder
.
decode
(
itemValue
,
CHARSET
);
}
}
else
if
(
itemKey
.
startsWith
(
key
))
{
removeCookie
(
response
,
itemKey
);
map
.
put
(
itemKey
,
itemValue
);
}
}
// 循环处理多个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
URLDecoder
.
decode
(
sb
.
toString
(),
CHARSET
);
}
/**
* 写入缓存Cookies
*
* @param key 键
* @param value 值
*/
public
static
void
set
(
String
key
,
String
value
)
throws
UnsupportedEncodingException
{
HttpServletResponse
response
=
getResposne
();
String
toValue
=
URLEncoder
.
encode
(
value
,
CHARSET
);
if
(
toValue
.
length
()
<
ITEM_MAX_SIZE
)
{
addCookie
(
response
,
key
,
toValue
);
}
else
{
int
page
=
StringHelper
.
getPage
(
toValue
.
length
(),
ITEM_MAX_SIZE
);
// 总Cookies数量
addCookie
(
response
,
key
,
String
.
valueOf
(
page
));
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
(
toValue
.
length
()
-
start
,
ITEM_MAX_SIZE
);
int
end
=
start
+
size
;
String
itemValue
=
toValue
.
substring
(
start
,
end
);
addCookie
(
response
,
itemKey
,
itemValue
);
}
}
}
/**
* 删除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
);
}
/**
* 获取请求对象
*
* @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
()
throws
UnsupportedEncodingException
{
String
tokenResponse
=
TokenHelper
.
getTokenString
();
if
(!
StringHelper
.
isEmpty
(
tokenResponse
))
{
set
(
TOKEN_STRING_KEY
,
tokenResponse
);
}
TokenHelper
.
remove
();
}
/**
* 登录初始化
*/
public
static
void
tokenInit
()
throws
UnsupportedEncodingException
{
String
tokenRequest
=
get
(
CookiesHelper
.
TOKEN_STRING_KEY
,
true
);
if
(!
StringHelper
.
isEmpty
(
tokenRequest
))
{
TokenHelper
.
setTokenString
(
tokenRequest
);
}
}
}
yzg-util-db/src/main/java/com/yanzuoguang/token/TokenData.java
0 → 100644
View file @
066e7d74
package
com
.
yanzuoguang
.
token
;
import
com.yanzuoguang.util.vo.MapRow
;
/**
* Token创建数据
*
* @author 颜佐光
*/
public
class
TokenData
{
/**
* token标记
*/
private
String
token
;
/**
* 数据密钥
*/
private
String
dataPwd
;
/**
* 有效时间
*/
private
long
expire
=
Long
.
MAX_VALUE
;
/**
* 缓存的数据
*/
private
MapRow
data
=
new
MapRow
();
public
String
getToken
()
{
return
token
;
}
public
void
setToken
(
String
token
)
{
this
.
token
=
token
;
}
public
String
getDataPwd
()
{
return
dataPwd
;
}
public
void
setDataPwd
(
String
dataPwd
)
{
this
.
dataPwd
=
dataPwd
;
}
public
long
getExpire
()
{
return
expire
;
}
public
void
setExpire
(
long
expire
)
{
this
.
expire
=
expire
;
}
public
MapRow
getData
()
{
return
data
;
}
public
void
setData
(
MapRow
data
)
{
this
.
data
=
data
;
}
}
yzg-util-db/src/main/java/com/yanzuoguang/token/TokenHelper.java
0 → 100644
View file @
066e7d74
package
com
.
yanzuoguang
.
token
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.cache.MemoryCache
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.vo.MapRow
;
/**
* 登录请求
*
* @author 颜佐光
*/
public
class
TokenHelper
{
/**
* 内存缓存
*/
protected
static
MemoryCache
<
TokenData
>
cache
=
new
MemoryCache
<>();
/**
* 获取当前线程编号
*
* @return 线程编号
*/
private
static
String
getCurrentId
()
{
return
String
.
valueOf
(
Thread
.
currentThread
().
getId
());
}
/**
* 获取当前的登录信息
*
* @param cls 需要获取的数据的类型
* @param <T> 数据类型
* @return 缓存的数据
*/
public
static
<
T
extends
Object
>
T
get
(
Class
<
T
>
cls
)
{
return
get
(
true
,
cls
);
}
/**
* 获取当前的登录信息
*
* @param checkFlag 是否抛出异常
* @param cls 需要获取的数据的类型
* @param <T> 数据类型
* @return 缓存的数据
*/
public
static
<
T
extends
Object
>
T
get
(
boolean
checkFlag
,
Class
<
T
>
cls
)
{
String
id
=
getCurrentId
();
TokenData
tokenData
=
cache
.
get
(
id
);
if
(
tokenData
==
null
||
tokenData
.
getData
()
==
null
)
{
if
(
checkFlag
)
{
throw
new
CodeException
(
"请先登录"
);
}
return
null
;
}
if
(
ObjectHelper
.
isSub
(
cls
,
tokenData
.
getData
().
getClass
()))
{
return
(
T
)
tokenData
.
getData
();
}
else
{
String
json
=
JsonHelper
.
serialize
(
tokenData
.
getData
());
return
JsonHelper
.
deserialize
(
json
,
cls
);
}
}
/**
* 获取字段值
*
* @param cls 字段值类型
* @param field 字段值
* @param <T> 类型
* @return 对象
*/
public
static
<
T
extends
Object
>
T
getField
(
Class
<
T
>
cls
,
String
field
)
{
return
getField
(
true
,
cls
,
field
);
}
/**
* 获取字段值
*
* @param checkFlag 是否抛出异常
* @param cls 字段值类型
* @param field 字段值
* @param <T> 类型
* @return 对象
*/
public
static
<
T
extends
Object
>
T
getField
(
boolean
checkFlag
,
Class
<
T
>
cls
,
String
field
)
{
MapRow
row
=
get
(
checkFlag
,
MapRow
.
class
);
Object
value
=
null
;
if
(
row
.
containsKey
(
field
))
{
value
=
row
.
get
(
field
);
}
return
StringHelper
.
to
(
cls
,
value
);
}
/**
* 缓存数据
*
* @param token 标记
* @param data 数据时间
*/
public
static
TokenData
write
(
String
token
,
Object
data
)
{
return
write
(
token
,
StringHelper
.
EMPTY
,
0
,
data
);
}
/**
* 缓存数据
*
* @param token 标记
* @param expire 有效期
* @param data 数据时间
*/
public
static
TokenData
write
(
String
token
,
long
expire
,
Object
data
)
{
return
write
(
token
,
StringHelper
.
EMPTY
,
expire
,
data
);
}
/**
* 缓存数据
*
* @param token 标记
* @param expire 有效期
* @param dataPwd 数据密码
* @param data 数据时间
*/
public
static
TokenData
write
(
String
token
,
String
dataPwd
,
long
expire
,
Object
data
)
{
String
id
=
getCurrentId
();
TokenData
tokenData
=
cache
.
get
(
id
);
if
(
tokenData
==
null
)
{
tokenData
=
new
TokenData
();
cache
.
put
(
id
,
tokenData
);
}
tokenData
.
setToken
(
token
);
tokenData
.
setDataPwd
(
dataPwd
);
if
(
expire
>
0
)
{
tokenData
.
setExpire
(
expire
);
}
if
(
tokenData
.
getData
()
==
null
)
{
tokenData
.
setData
(
new
MapRow
());
}
ObjectHelper
.
writeWithFrom
(
tokenData
.
getData
(),
data
);
return
tokenData
;
}
/**
* 设置登录标记字符串
*
* @param tokenString 登录标记字符串
*/
public
static
TokenData
setTokenString
(
String
tokenString
)
{
String
id
=
getCurrentId
();
TokenData
tokenData
=
JsonHelper
.
deserialize
(
tokenString
,
TokenData
.
class
);
cache
.
put
(
id
,
tokenData
);
return
tokenData
;
}
/**
* 获取登录标记字符串,自动要删除数据
*
* @return 登录标记字符粗
*/
public
static
String
getTokenString
()
{
String
id
=
getCurrentId
();
TokenData
tokenData
=
cache
.
get
(
id
);
if
(
tokenData
==
null
)
{
return
StringHelper
.
EMPTY
;
}
return
JsonHelper
.
serialize
(
tokenData
);
}
/**
* 删除缓存信息
*/
public
static
void
remove
()
{
String
id
=
getCurrentId
();
cache
.
remove
(
id
);
}
}
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