|
|
@@ -13,27 +13,25 @@
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
-package me.zhengjie.modules.security.service;
|
|
|
+package me.zhengjie.application.admin.service.impl;
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
-import me.zhengjie.exception.BadRequestException;
|
|
|
-import me.zhengjie.exception.EntityNotFoundException;
|
|
|
-import me.zhengjie.modules.security.config.bean.LoginProperties;
|
|
|
-import me.zhengjie.modules.security.service.dto.JwtUserDto;
|
|
|
-import me.zhengjie.dao.mybatis.entity.BankEntity;
|
|
|
-import me.zhengjie.dao.mybatis.entity.NotaryOfficeEntity;
|
|
|
-import me.zhengjie.dao.mybatis.mapper.BankMapper;
|
|
|
-import me.zhengjie.dao.mybatis.mapper.NotaryOfficeMapper;
|
|
|
import me.zhengjie.application.admin.service.DataService;
|
|
|
import me.zhengjie.application.admin.service.RoleService;
|
|
|
import me.zhengjie.application.admin.service.UserService;
|
|
|
+import me.zhengjie.application.admin.service.dto.JwtUserDto;
|
|
|
import me.zhengjie.application.admin.service.dto.UserDto;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import me.zhengjie.dao.mybatis.entity.BankEntity;
|
|
|
+import me.zhengjie.dao.mybatis.entity.NotaryOfficeEntity;
|
|
|
+import me.zhengjie.dao.mybatis.mapper.BankMapper;
|
|
|
+import me.zhengjie.dao.mybatis.mapper.NotaryOfficeMapper;
|
|
|
+import me.zhengjie.exception.BadRequestException;
|
|
|
+import me.zhengjie.exception.EntityNotFoundException;
|
|
|
+import me.zhengjie.security.config.bean.LoginProperties;
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.List;
|
|
|
import java.util.concurrent.*;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
@@ -48,13 +46,8 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
private final RoleService roleService;
|
|
|
private final DataService dataService;
|
|
|
private final LoginProperties loginProperties;
|
|
|
-
|
|
|
- private final UserCacheManager USER_DTO_CACHE;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private NotaryOfficeMapper notaryOfficeMapper;
|
|
|
- @Autowired
|
|
|
- private BankMapper bankMapper;
|
|
|
+ private final NotaryOfficeMapper notaryOfficeMapper;
|
|
|
+ private final BankMapper bankMapper;
|
|
|
|
|
|
public void setEnableCache(boolean enableCache) {
|
|
|
this.loginProperties.setCacheEnable(enableCache);
|
|
|
@@ -65,8 +58,8 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
@Override
|
|
|
public JwtUserDto loadUserByUsername(String username) {
|
|
|
JwtUserDto jwtUserDto = null;
|
|
|
- Future<JwtUserDto> future = USER_DTO_CACHE.get(username);
|
|
|
- if (!loginProperties.isCacheEnable()) {
|
|
|
+// Future<JwtUserDto> future = USER_DTO_CACHE.get(username);
|
|
|
+// if (!loginProperties.isCacheEnable()) {
|
|
|
UserDto user;
|
|
|
try {
|
|
|
user = userService.findByName(username);
|
|
|
@@ -91,58 +84,63 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
user.setAutoGenerate(notaryOfficeEntity.getAutoGenerate());
|
|
|
}
|
|
|
}
|
|
|
- jwtUserDto = new JwtUserDto(
|
|
|
- user,
|
|
|
- dataService.getDeptIds(user),
|
|
|
- roleService.mapToGrantedAuthorities(user)
|
|
|
- );
|
|
|
+ jwtUserDto = new JwtUserDto();
|
|
|
+ jwtUserDto.setUser(user);
|
|
|
+ jwtUserDto.setUserId(user.getId());
|
|
|
+ jwtUserDto.setDataScopes(dataService.getDeptIds(user));
|
|
|
+ jwtUserDto.setAuthorities(roleService.mapToGrantedAuthorities(user));
|
|
|
+ jwtUserDto.setEnabled(user.getEnabled());
|
|
|
+ jwtUserDto.setUsername(user.getUsername());
|
|
|
+ jwtUserDto.setPassword(user.getPassword());
|
|
|
+ jwtUserDto.setNickName(user.getNickName());
|
|
|
+ jwtUserDto.setDept(user.getDept().getName());
|
|
|
}
|
|
|
return jwtUserDto;
|
|
|
- }
|
|
|
-
|
|
|
- if (future == null) {
|
|
|
- Callable<JwtUserDto> call = () -> getJwtBySearchDb(username);
|
|
|
- FutureTask<JwtUserDto> ft = new FutureTask<>(call);
|
|
|
- future = USER_DTO_CACHE.putIfAbsent(username, ft);
|
|
|
- if (future == null) {
|
|
|
- future = ft;
|
|
|
- executor.submit(ft);
|
|
|
- }
|
|
|
- try {
|
|
|
- return future.get();
|
|
|
- } catch (CancellationException e) {
|
|
|
- USER_DTO_CACHE.remove(username);
|
|
|
- System.out.println("error" + Thread.currentThread().getName());
|
|
|
- } catch (InterruptedException | ExecutionException e) {
|
|
|
- throw new RuntimeException(e.getMessage());
|
|
|
- }
|
|
|
- } else {
|
|
|
- try {
|
|
|
- jwtUserDto = future.get();
|
|
|
- } catch (UsernameNotFoundException e) {
|
|
|
- throw new UsernameNotFoundException(e.getMessage());
|
|
|
- }
|
|
|
- catch (InterruptedException | ExecutionException e) {
|
|
|
- throw new RuntimeException(e.getMessage());
|
|
|
- }
|
|
|
- // 检查dataScope是否修改
|
|
|
- List<Long> dataScopes = jwtUserDto.getDataScopes();
|
|
|
- dataScopes.clear();
|
|
|
- dataScopes.addAll(dataService.getDeptIds(jwtUserDto.getUser()));
|
|
|
-
|
|
|
- }
|
|
|
- if (!org.springframework.util.StringUtils.isEmpty(jwtUserDto.getUser().getOrgId())){
|
|
|
- String[] split = jwtUserDto.getUser().getOrgId().split("_");
|
|
|
- if ("bank".equals(split[0])){
|
|
|
- BankEntity bankEntity = bankMapper.selectById(split[1]);
|
|
|
- jwtUserDto.getUser().setOrgName(bankEntity.getBankName());
|
|
|
- } else {
|
|
|
- NotaryOfficeEntity notaryOfficeEntity = notaryOfficeMapper.selectById(split[1]);
|
|
|
- jwtUserDto.getUser().setOrgName(notaryOfficeEntity.getName());
|
|
|
- jwtUserDto.getUser().setAutoGenerate(notaryOfficeEntity.getAutoGenerate());
|
|
|
- }
|
|
|
- }
|
|
|
- return jwtUserDto;
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (future == null) {
|
|
|
+// Callable<JwtUserDto> call = () -> getJwtBySearchDb(username);
|
|
|
+// FutureTask<JwtUserDto> ft = new FutureTask<>(call);
|
|
|
+// future = USER_DTO_CACHE.putIfAbsent(username, ft);
|
|
|
+// if (future == null) {
|
|
|
+// future = ft;
|
|
|
+// executor.submit(ft);
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// return future.get();
|
|
|
+// } catch (CancellationException e) {
|
|
|
+// USER_DTO_CACHE.remove(username);
|
|
|
+// System.out.println("error" + Thread.currentThread().getName());
|
|
|
+// } catch (InterruptedException | ExecutionException e) {
|
|
|
+// throw new RuntimeException(e.getMessage());
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// try {
|
|
|
+// jwtUserDto = future.get();
|
|
|
+// } catch (UsernameNotFoundException e) {
|
|
|
+// throw new UsernameNotFoundException(e.getMessage());
|
|
|
+// }
|
|
|
+// catch (InterruptedException | ExecutionException e) {
|
|
|
+// throw new RuntimeException(e.getMessage());
|
|
|
+// }
|
|
|
+// // 检查dataScope是否修改
|
|
|
+// List<Long> dataScopes = jwtUserDto.getDataScopes();
|
|
|
+// dataScopes.clear();
|
|
|
+// dataScopes.addAll(dataService.getDeptIds(jwtUserDto.getUser()));
|
|
|
+//
|
|
|
+// }
|
|
|
+// if (!org.springframework.util.StringUtils.isEmpty(jwtUserDto.getUser().getOrgId())){
|
|
|
+// String[] split = jwtUserDto.getUser().getOrgId().split("_");
|
|
|
+// if ("bank".equals(split[0])){
|
|
|
+// BankEntity bankEntity = bankMapper.selectById(split[1]);
|
|
|
+// jwtUserDto.getUser().setOrgName(bankEntity.getBankName());
|
|
|
+// } else {
|
|
|
+// NotaryOfficeEntity notaryOfficeEntity = notaryOfficeMapper.selectById(split[1]);
|
|
|
+// jwtUserDto.getUser().setOrgName(notaryOfficeEntity.getName());
|
|
|
+// jwtUserDto.getUser().setAutoGenerate(notaryOfficeEntity.getAutoGenerate());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return jwtUserDto;
|
|
|
}
|
|
|
|
|
|
private JwtUserDto getJwtBySearchDb(String username) {
|
|
|
@@ -170,11 +168,9 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|
|
user.setAutoGenerate(notaryOfficeEntity.getAutoGenerate());
|
|
|
}
|
|
|
}
|
|
|
- return new JwtUserDto(
|
|
|
- user,
|
|
|
- dataService.getDeptIds(user),
|
|
|
- roleService.mapToGrantedAuthorities(user)
|
|
|
- );
|
|
|
+ JwtUserDto jwtUserDto = new JwtUserDto();
|
|
|
+ jwtUserDto.setUser(user);
|
|
|
+ return jwtUserDto;
|
|
|
}
|
|
|
|
|
|
}
|