调整格式

pull/652/head
dante 2021-06-27 00:14:38 +08:00
parent b1a3064cc8
commit 721bbe2cbc
1 changed files with 38 additions and 34 deletions

View File

@ -57,44 +57,35 @@ public class UserDetailsServiceImpl implements UserDetailsService {
final static Map<String, Future<JwtUserDto>> userDtoCache = new ConcurrentHashMap<>(); final static Map<String, Future<JwtUserDto>> userDtoCache = 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 = userDtoCache.get(username);
if(!loginProperties.isCacheEnable()){ if (!loginProperties.isCacheEnable()) {
UserDto user; UserDto user;
try {
user = userService.findByName(username);
} catch (EntityNotFoundException e) {
// SpringSecurity会自动转换UsernameNotFoundException为BadCredentialsException
throw new UsernameNotFoundException("", e);
}
if (user == null) {
throw new UsernameNotFoundException("");
} else {
if (!user.getEnabled()) {
throw new BadRequestException("账号未激活!");
}
jwtUserDto = new JwtUserDto(
user,
dataService.getDeptIds(user),
roleService.mapToGrantedAuthorities(user)
);
}
return jwtUserDto;
}
if (future!=null) {
try { try {
jwtUserDto=future.get(); user = userService.findByName(username);
}catch (InterruptedException | ExecutionException e) { } catch (EntityNotFoundException e) {
throw new RuntimeException(e.getMessage()); // SpringSecurity会自动转换UsernameNotFoundException为BadCredentialsException
throw new UsernameNotFoundException("", e);
} }
// 检查dataScope是否修改 if (user == null) {
List<Long> dataScopes = jwtUserDto.getDataScopes(); throw new UsernameNotFoundException("");
dataScopes.clear(); } else {
dataScopes.addAll(dataService.getDeptIds(jwtUserDto.getUser())); if (!user.getEnabled()) {
}else{ throw new BadRequestException("账号未激活!");
}
jwtUserDto = new JwtUserDto(
user,
dataService.getDeptIds(user),
roleService.mapToGrantedAuthorities(user)
);
}
return jwtUserDto;
}
if (future==null) {
Callable<JwtUserDto> call=()->getJwtBySearchDB(username); Callable<JwtUserDto> call=()->getJwtBySearchDB(username);
FutureTask<JwtUserDto> ft=new FutureTask<>(call); FutureTask<JwtUserDto> ft=new FutureTask<>(call);
future=userDtoCache.putIfAbsent(username,ft); future=userDtoCache.putIfAbsent(username,ft);
@ -109,10 +100,23 @@ public class UserDetailsServiceImpl implements UserDetailsService {
}catch (InterruptedException | ExecutionException e) { }catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e.getMessage()); throw new RuntimeException(e.getMessage());
} }
}else{
try {
jwtUserDto=future.get();
}catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e.getMessage());
}
// 检查dataScope是否修改
List<Long> dataScopes = jwtUserDto.getDataScopes();
dataScopes.clear();
dataScopes.addAll(dataService.getDeptIds(jwtUserDto.getUser()));
} }
return jwtUserDto; return jwtUserDto;
} }
private JwtUserDto getJwtBySearchDB(String username) { private JwtUserDto getJwtBySearchDB(String username) {
UserDto user; UserDto user;
try { try {
@ -153,7 +157,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
return thread; return thread;
} }
}; };
return new ThreadPoolExecutor(8, 200, return new ThreadPoolExecutor(10, 200,
0L, TimeUnit.MILLISECONDS, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), new LinkedBlockingQueue<>(1024),
namedThreadFactory, namedThreadFactory,