From 3c4781916c7df04cc2a28d82263771c5018e4b7f Mon Sep 17 00:00:00 2001 From: lihongbiao <964708803@qq.com> Date: Fri, 18 Jul 2025 18:02:23 +0800 Subject: [PATCH 1/3] =?UTF-8?q?b=E7=AB=AF=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/service/BOnlineUserService.java | 39 +++++++++++++++++-- .../security/service/BUserDetailsService.java | 34 ++++++++++++++-- .../security/service/UserCacheManager.java | 16 ++++++++ .../security/service/dto/JwtCustomerDto.java | 4 ++ .../controller/CnCustomerController.java | 25 ++---------- .../aircraft/modules/system/domain/User.java | 1 - 6 files changed, 90 insertions(+), 29 deletions(-) diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/service/BOnlineUserService.java b/aircraft-system/src/main/java/com/aircraft/modules/security/service/BOnlineUserService.java index b08c462..72e7463 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/service/BOnlineUserService.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/service/BOnlineUserService.java @@ -1,19 +1,52 @@ package com.aircraft.modules.security.service; +import com.aircraft.modules.security.config.SecurityProperties; +import com.aircraft.modules.security.security.TokenProvider; import com.aircraft.modules.security.service.dto.JwtCustomerDto; import com.aircraft.modules.security.service.dto.JwtUserDto; +import com.aircraft.modules.security.service.dto.OnlineUserDto; +import com.aircraft.utils.EncryptUtils; +import com.aircraft.utils.RedisUtils; +import com.aircraft.utils.StringUtils; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; +import java.util.Date; +import java.util.concurrent.TimeUnit; +@Slf4j +@AllArgsConstructor @Service public class BOnlineUserService { + + private final RedisUtils redisUtils; + private final SecurityProperties properties; + private final TokenProvider tokenProvider; + public void save(JwtCustomerDto user, String token, HttpServletRequest request) { - // 实现B端在线用户存储逻辑 - // 可存入不同redis前缀的键值对 + String dept = user.getUser().getDept().getName(); + String ip = StringUtils.getIp(request); + String id = tokenProvider.getId(token); + String browser = StringUtils.getBrowser(request); + String address = StringUtils.getCityInfo(ip); + OnlineUserDto onlineUserDto = null; + try { + onlineUserDto = new OnlineUserDto(id, user.getUsername(), user.getUser().getNickName(), dept, browser , ip, address, EncryptUtils.desEncrypt(token), new Date()); + } catch (Exception e) { + log.error(e.getMessage(),e); + } + String loginKey = tokenProvider.loginKey(token); + redisUtils.set(loginKey, onlineUserDto, properties.getTokenValidityInSeconds(), TimeUnit.MILLISECONDS); } + /** + * 根据用户名强退用户 + * @param username / + */ public void kickOutForUsername(String username) { - // B端踢人逻辑 + String loginKey = properties.getOnlineKey() + username + "*"; + redisUtils.scanDel(loginKey); } } \ No newline at end of file diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java b/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java index 4be2bd0..ac64445 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/service/BUserDetailsService.java @@ -1,28 +1,54 @@ package com.aircraft.modules.security.service; +import com.aircraft.exception.BadRequestException; import com.aircraft.modules.security.service.dto.JwtCustomerDto; import com.aircraft.modules.security.service.dto.JwtUserDto; +import com.aircraft.modules.security.service.dto.LoginUserDto; import com.aircraft.modules.system.domain.CnCustomer; +import com.aircraft.modules.system.domain.Dept; import com.aircraft.modules.system.service.CnCustomerService; +import com.aircraft.utils.enums.UserTypeEnum; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Service; +import java.util.ArrayList; + @Slf4j @RequiredArgsConstructor @Service("bUserDetailsService") public class BUserDetailsService implements UserDetailsService { private final CnCustomerService cnCustomerService; + private final UserCacheManager userCacheManager; @Override public JwtCustomerDto loadUserByUsername(String phone) { + JwtCustomerDto customerDto = userCacheManager.getCustomerCache(phone); // 查询B端用户表(如b_user) - CnCustomer customer = cnCustomerService.findByphone(phone); - // 转换JwtUserDto(包含权限信息) -// return convertToJwtUser(customer); - return null; + if(customerDto == null){ + CnCustomer customer = cnCustomerService.findByphone(phone); + if (customer == null) { + throw new BadRequestException("客户不存在"); + } else { + if (!customer.getStatus().equals("0")) { + throw new BadRequestException("账号未激活!"); + } + // 使用构造函数传递必要的参数 + LoginUserDto userDto = new LoginUserDto(); + userDto.setId(customer.getId()); + userDto.setUsername(customer.getPhone()); + userDto.setNickName(customer.getName()); + userDto.setDept(new Dept()); + userDto.setPassword(customer.getPassword()); + userDto.setEnabled(true); + userDto.setIsAdmin(false); + // 创建一个 JwtCustomerDto 对象 + customerDto = new JwtCustomerDto(userDto, UserTypeEnum.CUSTOMER, new ArrayList<>()); + } + } + return customerDto; } } \ No newline at end of file diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/service/UserCacheManager.java b/aircraft-system/src/main/java/com/aircraft/modules/security/service/UserCacheManager.java index 89ab82e..361f8fb 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/service/UserCacheManager.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/service/UserCacheManager.java @@ -17,6 +17,7 @@ package com.aircraft.modules.security.service; import cn.hutool.core.util.RandomUtil; import com.aircraft.modules.security.config.LoginProperties; +import com.aircraft.modules.security.service.dto.JwtCustomerDto; import com.aircraft.modules.security.service.dto.JwtUserDto; import com.aircraft.utils.RedisUtils; import com.aircraft.utils.StringUtils; @@ -53,6 +54,21 @@ public class UserCacheManager { return null; } + /** + * 返回用户缓存 + * @param phone 电话 + * @return JwtUserDto + */ + public JwtCustomerDto getCustomerCache(String phone) { + // 转小写 + phone = StringUtils.lowerCase(phone); + if (StringUtils.isNotEmpty(phone)) { + // 获取数据 + return redisUtils.get(LoginProperties.cacheKey + phone, JwtCustomerDto.class); + } + return null; + } + /** * 添加缓存到Redis * @param userName 用户名 diff --git a/aircraft-system/src/main/java/com/aircraft/modules/security/service/dto/JwtCustomerDto.java b/aircraft-system/src/main/java/com/aircraft/modules/security/service/dto/JwtCustomerDto.java index b30a3e4..80a8079 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/security/service/dto/JwtCustomerDto.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/security/service/dto/JwtCustomerDto.java @@ -23,6 +23,7 @@ import com.alibaba.fastjson2.annotation.JSONField; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.NoArgsConstructor; import org.springframework.security.core.userdetails.UserDetails; import java.util.ArrayList; @@ -41,6 +42,9 @@ public class JwtCustomerDto implements UserDetails { @ApiModelProperty(value = "用户") private LoginUserDto user; + @ApiModelProperty("用户类型") + private final UserTypeEnum userType; + @ApiModelProperty(value = "角色") private final List authorities; diff --git a/aircraft-system/src/main/java/com/aircraft/modules/system/controller/CnCustomerController.java b/aircraft-system/src/main/java/com/aircraft/modules/system/controller/CnCustomerController.java index be3d86c..b2cc878 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/system/controller/CnCustomerController.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/system/controller/CnCustomerController.java @@ -88,25 +88,6 @@ public class CnCustomerController { return new ResponseEntity<>(records,HttpStatus.OK); } -// @Log("删除客户") -// @ApiOperation(value = "删除客户") -// @RequestMapping(value = "{id}", method = {RequestMethod.DELETE}) -// @ApiImplicitParam(name = "id", value = "客户ID", required = true, paramType = "path") -// public ResponseEntity delete(@PathVariable final Integer id) { -// try { -// CnCustomer entity = entityService.getById(id); -// entity.setDelFlag(1); -// entityService.updateById(entity); -// return new ResponseEntity<>("成功删除客户", HttpStatus.OK); -// } catch (DataIntegrityViolationException e) { -// LOG.error("删除客户失败", e); -// throw new RuntimeException( "删除客户失败,该客户不能删除,存在其他关联数据"); -// } catch (Exception e) { -// LOG.error("删除客户失败", e); -// throw new RuntimeException("删除客户失败", e); -// } -// } - @Log("查询单个客户") @ApiOperation(value = "查询单个客户") @RequestMapping(value = "{id}", method = {RequestMethod.GET}) @@ -179,11 +160,13 @@ public class CnCustomerController { if (!passwordEncoder.matches(password, jwtUser.getPassword())) { throw new BadRequestException("登录密码错误"); } - + Map details = new HashMap<>(); + details.put("userType", String.valueOf(jwtUser.getUserType().getValue())); // 5. 设置认证信息 - Authentication authentication = new UsernamePasswordAuthenticationToken( + UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( jwtUser, null, jwtUser.getAuthorities() ); + authentication.setDetails(details); SecurityContextHolder.getContext().setAuthentication(authentication); // 6. 生成令牌(复用相同机制) diff --git a/aircraft-system/src/main/java/com/aircraft/modules/system/domain/User.java b/aircraft-system/src/main/java/com/aircraft/modules/system/domain/User.java index a15e0ae..3eac1ec 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/system/domain/User.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/system/domain/User.java @@ -74,7 +74,6 @@ public class User extends BaseEntity implements Serializable { private String nickName; @Email - @NotBlank @ApiModelProperty(value = "邮箱") private String email; From f6c583f902dec47bddf791f5951adcd139bd6b8e Mon Sep 17 00:00:00 2001 From: lihongbiao <964708803@qq.com> Date: Fri, 18 Jul 2025 18:23:50 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/service/impl/UserServiceImpl.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java b/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java index 02001a1..1a6fbea 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java @@ -114,7 +114,7 @@ public class UserServiceImpl extends ServiceImpl implements Us @Override @Transactional(rollbackFor = Exception.class) public void create(User resources) { - resources.setDeptId(resources.getDept().getId()); +// resources.setDeptId(resources.getDept().getId()); if (userMapper.findByUsername(resources.getUsername()) != null) { throw new EntityExistException(User.class, "username", resources.getUsername()); } @@ -125,7 +125,7 @@ public class UserServiceImpl extends ServiceImpl implements Us throw new EntityExistException(User.class, "phone", resources.getPhone()); } save(resources); - if (UserTypeEnum.valueOf(String.valueOf(resources.getUserType())).equals(UserTypeEnum.EMPLOYEES)){ + if (Objects.equals(UserTypeEnum.getByValue(resources.getUserType()), UserTypeEnum.EMPLOYEES)) { EmEmployees emEmployees = new EmEmployees(); emEmployees.setDelFlag(0); emEmployees.setPhone(resources.getPhone()); @@ -133,7 +133,7 @@ public class UserServiceImpl extends ServiceImpl implements Us employeesService.save(emEmployees); } // 保存用户岗位 - userJobMapper.insertData(resources.getId(), resources.getJobs()); +// userJobMapper.insertData(resources.getId(), resources.getJobs()); // 保存用户角色 userRoleMapper.insertData(resources.getId(), resources.getRoles()); } @@ -143,14 +143,14 @@ public class UserServiceImpl extends ServiceImpl implements Us public void update(User resources) throws Exception { User user = getById(resources.getId()); User user1 = userMapper.findByUsername(resources.getUsername()); - User user2 = userMapper.findByEmail(resources.getEmail()); +// User user2 = userMapper.findByEmail(resources.getEmail()); User user3 = userMapper.findByPhone(resources.getPhone()); if (user1 != null && !user.getId().equals(user1.getId())) { throw new EntityExistException(User.class, "username", resources.getUsername()); } - if (user2 != null && !user.getId().equals(user2.getId())) { - throw new EntityExistException(User.class, "email", resources.getEmail()); - } +// if (user2 != null && !user.getId().equals(user2.getId())) { +// throw new EntityExistException(User.class, "email", resources.getEmail()); +// } if (user3 != null && !user.getId().equals(user3.getId())) { throw new EntityExistException(User.class, "phone", resources.getPhone()); } @@ -161,26 +161,26 @@ public class UserServiceImpl extends ServiceImpl implements Us redisUtils.del(CacheKey.ROLE_AUTH + resources.getId()); redisUtils.del(CacheKey.ROLE_USER + resources.getId()); } - // 修改部门会影响 数据权限 - if (!Objects.equals(resources.getDept(),user.getDept())) { - redisUtils.del(CacheKey.DATA_USER + resources.getId()); - } +// // 修改部门会影响 数据权限 +// if (!Objects.equals(resources.getDept(),user.getDept())) { +// redisUtils.del(CacheKey.DATA_USER + resources.getId()); +// } // 如果用户被禁用,则清除用户登录信息 if(!resources.getEnabled()){ onlineUserService.kickOutForUsername(resources.getUsername()); } - user.setDeptId(resources.getDept().getId()); +// user.setDeptId(resources.getDept().getId()); user.setUsername(resources.getUsername()); - user.setEmail(resources.getEmail()); +// user.setEmail(resources.getEmail()); user.setEnabled(resources.getEnabled()); user.setRoles(resources.getRoles()); - user.setDept(resources.getDept()); +// user.setDept(resources.getDept()); user.setJobs(resources.getJobs()); user.setPhone(resources.getPhone()); user.setNickName(resources.getNickName()); user.setGender(resources.getGender()); saveOrUpdate(user); - if (UserTypeEnum.valueOf(String.valueOf(resources.getUserType())).equals(UserTypeEnum.EMPLOYEES)){ + if (Objects.equals(UserTypeEnum.getByValue(resources.getUserType()), UserTypeEnum.EMPLOYEES)) { EmEmployees emEmployees = employeesService.findByUserId(resources.getId()); emEmployees.setPhone(resources.getPhone()); emEmployees.setName(resources.getNickName()); @@ -189,8 +189,8 @@ public class UserServiceImpl extends ServiceImpl implements Us // 清除缓存 delCaches(user.getId(), user.getUsername()); // 更新用户岗位 - userJobMapper.deleteByUserId(resources.getId()); - userJobMapper.insertData(resources.getId(), resources.getJobs()); +// userJobMapper.deleteByUserId(resources.getId()); +// userJobMapper.insertData(resources.getId(), resources.getJobs()); // 更新用户角色 userRoleMapper.deleteByUserId(resources.getId()); userRoleMapper.insertData(resources.getId(), resources.getRoles()); From 85af8dfa8e97f19956476ed9f02a641e99bca322 Mon Sep 17 00:00:00 2001 From: lihongbiao <964708803@qq.com> Date: Fri, 18 Jul 2025 18:24:33 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aircraft/modules/system/service/impl/UserServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java b/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java index 1a6fbea..51c775b 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/system/service/impl/UserServiceImpl.java @@ -218,7 +218,7 @@ public class UserServiceImpl extends ServiceImpl implements Us for (Long id : ids) { // 清理缓存 User user = getById(id); - if (user.getUserType()==1){ + if (Objects.equals(UserTypeEnum.getByValue(user.getUserType()), UserTypeEnum.EMPLOYEES)) { EmEmployees emEmployees = employeesService.findByUserId(id); employeesService.removeById(emEmployees.getId()); } @@ -226,7 +226,7 @@ public class UserServiceImpl extends ServiceImpl implements Us } userMapper.deleteBatchIds(ids); // 删除用户岗位 - userJobMapper.deleteByUserIds(ids); +// userJobMapper.deleteByUserIds(ids); // 删除用户角色 userRoleMapper.deleteByUserIds(ids); }