Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
1140af3199
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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 用户名
|
||||
|
@ -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<AuthorityDto> authorities;
|
||||
|
||||
|
@ -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<Object> 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<String, String> 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. 生成令牌(复用相同机制)
|
||||
|
@ -74,7 +74,6 @@ public class User extends BaseEntity implements Serializable {
|
||||
private String nickName;
|
||||
|
||||
@Email
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "邮箱")
|
||||
private String email;
|
||||
|
||||
|
@ -114,7 +114,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> 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<UserMapper, User> 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<UserMapper, User> 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<UserMapper, User> 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<UserMapper, User> 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<UserMapper, User> 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());
|
||||
@ -218,7 +218,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> 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<UserMapper, User> implements Us
|
||||
}
|
||||
userMapper.deleteBatchIds(ids);
|
||||
// 删除用户岗位
|
||||
userJobMapper.deleteByUserIds(ids);
|
||||
// userJobMapper.deleteByUserIds(ids);
|
||||
// 删除用户角色
|
||||
userRoleMapper.deleteByUserIds(ids);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user