Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
b2672587
Commit
b2672587
authored
Nov 23, 2021
by
Zheng Jie
Browse files
代码优化
parent
931ecb3b
Changes
2
Hide whitespace changes
Inline
Side-by-side
eladmin-system/src/main/java/me/zhengjie/modules/security/service/UserCacheClean.java
View file @
b2672587
...
@@ -35,7 +35,7 @@ public class UserCacheClean {
...
@@ -35,7 +35,7 @@ public class UserCacheClean {
*/
*/
public
void
cleanUserCache
(
String
userName
)
{
public
void
cleanUserCache
(
String
userName
)
{
if
(
StringUtils
.
isNotEmpty
(
userName
))
{
if
(
StringUtils
.
isNotEmpty
(
userName
))
{
UserDetailsServiceImpl
.
userDtoCache
.
remove
(
userName
);
UserDetailsServiceImpl
.
USER_DTO_CACHE
.
remove
(
userName
);
}
}
}
}
...
@@ -44,6 +44,6 @@ public class UserCacheClean {
...
@@ -44,6 +44,6 @@ public class UserCacheClean {
* ,如发生角色授权信息变化,可以简便的全部失效缓存
* ,如发生角色授权信息变化,可以简便的全部失效缓存
*/
*/
public
void
cleanAll
()
{
public
void
cleanAll
()
{
UserDetailsServiceImpl
.
userDtoCache
.
clear
();
UserDetailsServiceImpl
.
USER_DTO_CACHE
.
clear
();
}
}
}
}
eladmin-system/src/main/java/me/zhengjie/modules/security/service/UserDetailsServiceImpl.java
View file @
b2672587
...
@@ -55,13 +55,13 @@ public class UserDetailsServiceImpl implements UserDetailsService {
...
@@ -55,13 +55,13 @@ public class UserDetailsServiceImpl implements UserDetailsService {
* @see {@link UserCacheClean}
* @see {@link UserCacheClean}
*/
*/
final
static
Map
<
String
,
Future
<
JwtUserDto
>>
userDtoCache
=
new
ConcurrentHashMap
<>();
final
static
Map
<
String
,
Future
<
JwtUserDto
>>
USER_DTO_CACHE
=
new
ConcurrentHashMap
<>();
public
static
ExecutorService
executor
=
newThreadPool
();
public
static
ExecutorService
executor
=
newThreadPool
();
@Override
@Override
public
JwtUserDto
loadUserByUsername
(
String
username
)
{
public
JwtUserDto
loadUserByUsername
(
String
username
)
{
JwtUserDto
jwtUserDto
=
null
;
JwtUserDto
jwtUserDto
=
null
;
Future
<
JwtUserDto
>
future
=
userDtoCache
.
get
(
username
);
Future
<
JwtUserDto
>
future
=
USER_DTO_CACHE
.
get
(
username
);
if
(!
loginProperties
.
isCacheEnable
())
{
if
(!
loginProperties
.
isCacheEnable
())
{
UserDto
user
;
UserDto
user
;
try
{
try
{
...
@@ -86,9 +86,9 @@ public class UserDetailsServiceImpl implements UserDetailsService {
...
@@ -86,9 +86,9 @@ public class UserDetailsServiceImpl implements UserDetailsService {
}
}
if
(
future
==
null
)
{
if
(
future
==
null
)
{
Callable
<
JwtUserDto
>
call
=()->
getJwtBySearchD
B
(
username
);
Callable
<
JwtUserDto
>
call
=()->
getJwtBySearchD
b
(
username
);
FutureTask
<
JwtUserDto
>
ft
=
new
FutureTask
<>(
call
);
FutureTask
<
JwtUserDto
>
ft
=
new
FutureTask
<>(
call
);
future
=
userDtoCache
.
putIfAbsent
(
username
,
ft
);
future
=
USER_DTO_CACHE
.
putIfAbsent
(
username
,
ft
);
if
(
future
==
null
){
if
(
future
==
null
){
future
=
ft
;
future
=
ft
;
executor
.
submit
(
ft
);
executor
.
submit
(
ft
);
...
@@ -96,7 +96,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
...
@@ -96,7 +96,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
try
{
try
{
return
future
.
get
();
return
future
.
get
();
}
catch
(
CancellationException
e
){
}
catch
(
CancellationException
e
){
userDtoCache
.
remove
(
username
);
USER_DTO_CACHE
.
remove
(
username
);
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
throw
new
RuntimeException
(
e
.
getMessage
());
throw
new
RuntimeException
(
e
.
getMessage
());
}
}
...
@@ -116,8 +116,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
...
@@ -116,8 +116,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
}
}
private
JwtUserDto
getJwtBySearchDb
(
String
username
)
{
private
JwtUserDto
getJwtBySearchDB
(
String
username
)
{
UserDto
user
;
UserDto
user
;
try
{
try
{
user
=
userService
.
findByName
(
username
);
user
=
userService
.
findByName
(
username
);
...
@@ -131,12 +130,11 @@ public class UserDetailsServiceImpl implements UserDetailsService {
...
@@ -131,12 +130,11 @@ public class UserDetailsServiceImpl implements UserDetailsService {
if
(!
user
.
getEnabled
())
{
if
(!
user
.
getEnabled
())
{
throw
new
BadRequestException
(
"账号未激活!"
);
throw
new
BadRequestException
(
"账号未激活!"
);
}
}
JwtUserDto
jwtUserDto
=
new
JwtUserDto
(
return
new
JwtUserDto
(
user
,
user
,
dataService
.
getDeptIds
(
user
),
dataService
.
getDeptIds
(
user
),
roleService
.
mapToGrantedAuthorities
(
user
)
roleService
.
mapToGrantedAuthorities
(
user
)
);
);
return
jwtUserDto
;
}
}
}
}
...
...
Write
Preview
Supports
Markdown
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