From 721bbe2cbc5ecbd36f238d6cf7983c71e8111d61 Mon Sep 17 00:00:00 2001 From: dante <1062325367@qq.com> Date: Sun, 27 Jun 2021 00:14:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/UserDetailsServiceImpl.java | 72 ++++++++++--------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/service/UserDetailsServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/UserDetailsServiceImpl.java index c85dd658..034bd7d4 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/service/UserDetailsServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/service/UserDetailsServiceImpl.java @@ -57,44 +57,35 @@ public class UserDetailsServiceImpl implements UserDetailsService { final static Map> userDtoCache = new ConcurrentHashMap<>(); public static ExecutorService executor = newThreadPool(); + @Override public JwtUserDto loadUserByUsername(String username) { JwtUserDto jwtUserDto = null; - Future future=userDtoCache.get(username); - if(!loginProperties.isCacheEnable()){ - 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) { + Future future = userDtoCache.get(username); + if (!loginProperties.isCacheEnable()) { + UserDto user; try { - jwtUserDto=future.get(); - }catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e.getMessage()); + user = userService.findByName(username); + } catch (EntityNotFoundException e) { + // SpringSecurity会自动转换UsernameNotFoundException为BadCredentialsException + throw new UsernameNotFoundException("", e); } - // 检查dataScope是否修改 - List dataScopes = jwtUserDto.getDataScopes(); - dataScopes.clear(); - dataScopes.addAll(dataService.getDeptIds(jwtUserDto.getUser())); - }else{ + 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) { Callable call=()->getJwtBySearchDB(username); FutureTask ft=new FutureTask<>(call); future=userDtoCache.putIfAbsent(username,ft); @@ -109,10 +100,23 @@ public class UserDetailsServiceImpl implements UserDetailsService { }catch (InterruptedException | ExecutionException e) { throw new RuntimeException(e.getMessage()); } + }else{ + try { + jwtUserDto=future.get(); + }catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e.getMessage()); + } + // 检查dataScope是否修改 + List dataScopes = jwtUserDto.getDataScopes(); + dataScopes.clear(); + dataScopes.addAll(dataService.getDeptIds(jwtUserDto.getUser())); + } return jwtUserDto; + } + private JwtUserDto getJwtBySearchDB(String username) { UserDto user; try { @@ -153,7 +157,7 @@ public class UserDetailsServiceImpl implements UserDetailsService { return thread; } }; - return new ThreadPoolExecutor(8, 200, + return new ThreadPoolExecutor(10, 200, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024), namedThreadFactory,