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
4499d371
Commit
4499d371
authored
May 12, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
异常处理显示
parent
213a64bd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
185 additions
and
0 deletions
+185
-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
+122
-0
No files found.
yzg-util-db/src/main/java/com/yanzuoguang/token/TokenData.java
0 → 100644
View file @
4499d371
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 @
4499d371
package
com
.
yanzuoguang
.
token
;
import
com.yanzuoguang.util.base.ObjectHelper
;
import
com.yanzuoguang.util.cache.MemoryCache
;
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
)
{
String
id
=
getCurrentId
();
TokenData
tokenData
=
cache
.
get
(
id
);
if
(
tokenData
==
null
||
tokenData
.
getData
()
==
null
)
{
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
)
{
MapRow
row
=
get
(
MapRow
.
class
);
Object
value
=
null
;
if
(
row
.
containsKey
(
field
))
{
value
=
row
.
get
(
field
);
}
return
StringHelper
.
to
(
cls
,
value
);
}
/**
* 缓存数据
*
* @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
);
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
);
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