b端登录
This commit is contained in:
parent
593c534cfc
commit
3c4781916c
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user