三端登录
This commit is contained in:
parent
b4249d89ff
commit
b107ec9b8e
@ -1,15 +0,0 @@
|
||||
package com.aircraft.modules.security.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "b-login")
|
||||
public class BLoginProperties {
|
||||
|
||||
private boolean singleLogin = false;
|
||||
// 其他B端特有配置
|
||||
public static final String cacheKey = "buser_login_cache:";
|
||||
}
|
@ -36,4 +36,8 @@ public class LoginProperties {
|
||||
private boolean singleLogin = false;
|
||||
|
||||
public static final String cacheKey = "user_login_cache:";
|
||||
|
||||
public static final String bCacheKey = "b_user_login_cache:";
|
||||
|
||||
public static final String aCacheKey = "a_user_login_cache:";
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
package com.aircraft.modules.security.rest;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.aircraft.utils.enums.UserTypeEnum;
|
||||
import com.aircraft.modules.security.service.*;
|
||||
import com.wf.captcha.base.Captcha;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
@ -33,10 +33,8 @@ import com.aircraft.modules.security.config.enums.LoginCodeEnum;
|
||||
import com.aircraft.modules.security.config.LoginProperties;
|
||||
import com.aircraft.modules.security.config.SecurityProperties;
|
||||
import com.aircraft.modules.security.security.TokenProvider;
|
||||
import com.aircraft.modules.security.service.UserDetailsServiceImpl;
|
||||
import com.aircraft.modules.security.service.dto.AuthUserDto;
|
||||
import com.aircraft.modules.security.service.dto.JwtUserDto;
|
||||
import com.aircraft.modules.security.service.OnlineUserService;
|
||||
import com.aircraft.utils.RsaUtils;
|
||||
import com.aircraft.utils.RedisUtils;
|
||||
import com.aircraft.utils.SecurityUtils;
|
||||
@ -75,6 +73,8 @@ public class AuthController {
|
||||
private final LoginProperties loginProperties;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final UserDetailsServiceImpl userDetailsService;
|
||||
private final BUserDetailsService bUserDetailsService;
|
||||
private final AUserDetailsService aUserDetailsService;
|
||||
|
||||
@Log("用户登录")
|
||||
@ApiOperation("登录授权")
|
||||
@ -149,6 +149,85 @@ public class AuthController {
|
||||
return ResponseEntity.ok(authInfo);
|
||||
}
|
||||
|
||||
@Log("B端用户登录")
|
||||
@ApiOperation("B端登录授权")
|
||||
@AnonymousPostMapping(value = "/login/b")
|
||||
public ResponseEntity<Object> loginB(@Validated @RequestBody AuthUserDto authUser,
|
||||
HttpServletRequest request) throws Exception {
|
||||
|
||||
// 密码解密
|
||||
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
|
||||
|
||||
// 使用B端专属服务加载用户
|
||||
JwtUserDto jwtUser = bUserDetailsService.loadUserByUsername(authUser.getUsername());
|
||||
|
||||
// 密码验证
|
||||
if (!passwordEncoder.matches(password, jwtUser.getPassword())) {
|
||||
throw new BadRequestException("登录密码错误");
|
||||
}
|
||||
Map<String, String> details = new HashMap<>();
|
||||
details.put("userType", String.valueOf(jwtUser.getUserType().getValue()));
|
||||
// 设置认证信息
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||
jwtUser, null, jwtUser.getAuthorities()
|
||||
);
|
||||
authentication.setDetails(details);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
|
||||
// 生成令牌
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
|
||||
Map<String, Object> authInfo = new HashMap<>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
put("user", jwtUser); // 返回B端用户信息
|
||||
}};
|
||||
|
||||
// 单设备登录控制
|
||||
if (loginProperties.isSingleLogin()) { // B端专属配置
|
||||
onlineUserService.kickOutForUsername(authUser.getUsername());
|
||||
}
|
||||
|
||||
// 保存B端在线用户
|
||||
onlineUserService.save(jwtUser, token, request);
|
||||
|
||||
return ResponseEntity.ok(authInfo);
|
||||
}
|
||||
|
||||
@Log("飞行员端登录授权")
|
||||
@ApiOperation("飞行员端登录授权")
|
||||
@AnonymousPostMapping(value = "/login/a")
|
||||
public ResponseEntity<Object> alogin(@Validated @RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
|
||||
// 密码解密
|
||||
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
|
||||
|
||||
// 获取用户信息
|
||||
JwtUserDto jwtUser = aUserDetailsService.loadUserByUsername(authUser.getUsername());
|
||||
// 验证用户密码
|
||||
if (!passwordEncoder.matches(password, jwtUser.getPassword())) {
|
||||
throw new BadRequestException("登录密码错误");
|
||||
}
|
||||
Map<String, String> details = new HashMap<>();
|
||||
details.put("userType", String.valueOf(jwtUser.getUserType().getValue()));
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
authentication.setDetails(details);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 生成令牌
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
// 返回 token 与 用户信息
|
||||
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
put("user", jwtUser);
|
||||
}};
|
||||
if (loginProperties.isSingleLogin()) {
|
||||
// 踢掉之前已经登录的token
|
||||
onlineUserService.kickOutForUsername(authUser.getUsername());
|
||||
}
|
||||
// 保存在线信息
|
||||
onlineUserService.save(jwtUser, token, request);
|
||||
// 返回登录信息
|
||||
return ResponseEntity.ok(authInfo);
|
||||
}
|
||||
|
||||
@ApiOperation("获取用户信息")
|
||||
@GetMapping(value = "/info")
|
||||
public ResponseEntity<UserDetails> getUserInfo() {
|
||||
|
@ -88,30 +88,6 @@ public class TokenProvider implements InitializingBean {
|
||||
.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建Token 设置永不过期,
|
||||
* Token 的时间有效性转到Redis 维护
|
||||
* @param user /
|
||||
* @return /
|
||||
*/
|
||||
public String createToken(JwtCustomerDto user) {
|
||||
// 设置参数
|
||||
Map<String, Object> claims = new HashMap<>(6);
|
||||
// 设置用户ID
|
||||
claims.put(AUTHORITIES_UID_KEY, user.getUser().getId());
|
||||
// 设置UUID,确保每次Token不一样
|
||||
claims.put(AUTHORITIES_UUID_KEY, IdUtil.simpleUUID());
|
||||
// 直接调用 Jwts.builder() 创建新实例
|
||||
return Jwts.builder()
|
||||
// 设置自定义 Claims
|
||||
.setClaims(claims)
|
||||
// 设置主题
|
||||
.setSubject(user.getUsername())
|
||||
// 使用预生成的签名密钥和算法签名
|
||||
.signWith(signingKey, SignatureAlgorithm.HS512)
|
||||
.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 依据Token 获取鉴权信息
|
||||
*
|
||||
|
@ -0,0 +1,57 @@
|
||||
package com.aircraft.modules.security.service;
|
||||
|
||||
import com.aircraft.exception.BadRequestException;
|
||||
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.domain.EmEmployees;
|
||||
import com.aircraft.modules.system.service.CnCustomerService;
|
||||
import com.aircraft.modules.system.service.EmEmployeesService;
|
||||
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("aUserDetailsService")
|
||||
public class AUserDetailsService implements UserDetailsService {
|
||||
|
||||
private final EmEmployeesService emEmployeesService;
|
||||
private final UserCacheManager userCacheManager;
|
||||
|
||||
@Override
|
||||
public JwtUserDto loadUserByUsername(String username) {
|
||||
JwtUserDto employeeDto = userCacheManager.getEmployeeDtoCache(username);
|
||||
// 查询B端用户表(如b_user)
|
||||
if(employeeDto == null){
|
||||
EmEmployees emEmployees = emEmployeesService.findByUsername(username);
|
||||
if (emEmployees == null) {
|
||||
throw new BadRequestException("飞行员不存在");
|
||||
} else {
|
||||
if (emEmployees.getStatus().equals("0")) {
|
||||
throw new BadRequestException("账号未激活!");
|
||||
}
|
||||
// 使用构造函数传递必要的参数
|
||||
LoginUserDto userDto = new LoginUserDto();
|
||||
userDto.setId(emEmployees.getId());
|
||||
userDto.setUsername(emEmployees.getPhone());
|
||||
userDto.setNickName(emEmployees.getName());
|
||||
userDto.setDept(new Dept());
|
||||
userDto.setPassword(emEmployees.getPassword());
|
||||
userDto.setEnabled(true);
|
||||
userDto.setIsAdmin(false);
|
||||
// 创建一个 JwtCustomerDto 对象
|
||||
employeeDto = new JwtUserDto(userDto, UserTypeEnum.EMPLOYEES, new ArrayList<>());
|
||||
// 添加缓存数据
|
||||
userCacheManager.addEmployeeCache(username, employeeDto);
|
||||
}
|
||||
}
|
||||
return employeeDto;
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
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) {
|
||||
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) {
|
||||
String loginKey = properties.getOnlineKey() + username + "*";
|
||||
redisUtils.scanDel(loginKey);
|
||||
}
|
||||
}
|
@ -24,15 +24,15 @@ public class BUserDetailsService implements UserDetailsService {
|
||||
private final UserCacheManager userCacheManager;
|
||||
|
||||
@Override
|
||||
public JwtCustomerDto loadUserByUsername(String phone) {
|
||||
JwtCustomerDto customerDto = userCacheManager.getCustomerCache(phone);
|
||||
public JwtUserDto loadUserByUsername(String username) {
|
||||
JwtUserDto customerDto = userCacheManager.getCustomerCache(username);
|
||||
// 查询B端用户表(如b_user)
|
||||
if(customerDto == null){
|
||||
CnCustomer customer = cnCustomerService.findByphone(phone);
|
||||
CnCustomer customer = cnCustomerService.findByUserName(username);
|
||||
if (customer == null) {
|
||||
throw new BadRequestException("客户不存在");
|
||||
} else {
|
||||
if (!customer.getStatus().equals("0")) {
|
||||
if (customer.getStatus().equals("0")) {
|
||||
throw new BadRequestException("账号未激活!");
|
||||
}
|
||||
// 使用构造函数传递必要的参数
|
||||
@ -45,7 +45,9 @@ public class BUserDetailsService implements UserDetailsService {
|
||||
userDto.setEnabled(true);
|
||||
userDto.setIsAdmin(false);
|
||||
// 创建一个 JwtCustomerDto 对象
|
||||
customerDto = new JwtCustomerDto(userDto, UserTypeEnum.CUSTOMER, new ArrayList<>());
|
||||
customerDto = new JwtUserDto(userDto, UserTypeEnum.CUSTOMER, new ArrayList<>());
|
||||
// 添加缓存数据
|
||||
userCacheManager.addCustomerCache(username, customerDto);
|
||||
}
|
||||
}
|
||||
return customerDto;
|
||||
|
@ -17,7 +17,6 @@ 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;
|
||||
@ -56,15 +55,15 @@ public class UserCacheManager {
|
||||
|
||||
/**
|
||||
* 返回用户缓存
|
||||
* @param phone 电话
|
||||
* @param username 用户名
|
||||
* @return JwtUserDto
|
||||
*/
|
||||
public JwtCustomerDto getCustomerCache(String phone) {
|
||||
public JwtUserDto getCustomerCache(String username) {
|
||||
// 转小写
|
||||
phone = StringUtils.lowerCase(phone);
|
||||
if (StringUtils.isNotEmpty(phone)) {
|
||||
username = StringUtils.lowerCase(username);
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
// 获取数据
|
||||
return redisUtils.get(LoginProperties.cacheKey + phone, JwtCustomerDto.class);
|
||||
return redisUtils.get(LoginProperties.bCacheKey + username, JwtUserDto.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -98,4 +97,34 @@ public class UserCacheManager {
|
||||
redisUtils.del(LoginProperties.cacheKey + userName);
|
||||
}
|
||||
}
|
||||
|
||||
public void addCustomerCache(String username, JwtUserDto customerDto) {
|
||||
// 转小写
|
||||
username = StringUtils.lowerCase(username);
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
// 添加数据, 避免数据同时过期
|
||||
long time = idleTime + RandomUtil.randomInt(900, 1800);
|
||||
redisUtils.set(LoginProperties.bCacheKey + username, customerDto, time);
|
||||
}
|
||||
}
|
||||
|
||||
public JwtUserDto getEmployeeDtoCache(String username) {
|
||||
// 转小写
|
||||
username = StringUtils.lowerCase(username);
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
// 获取数据
|
||||
return redisUtils.get(LoginProperties.aCacheKey + username, JwtUserDto.class);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addEmployeeCache(String username, JwtUserDto employeeDto) {
|
||||
// 转小写
|
||||
username = StringUtils.lowerCase(username);
|
||||
if (StringUtils.isNotEmpty(username)) {
|
||||
// 添加数据, 避免数据同时过期
|
||||
long time = idleTime + RandomUtil.randomInt(900, 1800);
|
||||
redisUtils.set(LoginProperties.aCacheKey + username, employeeDto, time);
|
||||
}
|
||||
}
|
||||
}
|
@ -44,10 +44,7 @@ import java.util.List;
|
||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
private final UserService userService;
|
||||
private final RoleService roleService;
|
||||
private final DataService dataService;
|
||||
private final UserCacheManager userCacheManager;
|
||||
@Lazy
|
||||
private final EmEmployeesService emEmployeesService;
|
||||
|
||||
@Override
|
||||
public JwtUserDto loadUserByUsername(String username) {
|
||||
@ -62,12 +59,8 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
}
|
||||
// 获取用户的权限
|
||||
List<AuthorityDto> authorities = roleService.buildPermissions(user);
|
||||
EmEmployees emEmployees = null;
|
||||
// if (user.getUserType()==1) {
|
||||
// emEmployees = emEmployeesService.findByUserId(user.getId());
|
||||
// }
|
||||
// 初始化JwtUserDto
|
||||
jwtUserDto = new JwtUserDto(BeanUtil.copyProperties(user, LoginUserDto.class), UserTypeEnum.getByValue(user.getUserType()),null, authorities,emEmployees);
|
||||
jwtUserDto = new JwtUserDto(BeanUtil.copyProperties(user, LoginUserDto.class), UserTypeEnum.getByValue(user.getUserType()), authorities);
|
||||
// 添加缓存数据
|
||||
userCacheManager.addUserCache(username, jwtUserDto);
|
||||
}
|
||||
@ -75,25 +68,4 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
return jwtUserDto;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public JwtUserDto loadEmEmployeesByUsername(String username) {
|
||||
// JwtUserDto jwtUserDto = userCacheManager.getUserCache(username);
|
||||
// if(jwtUserDto == null){
|
||||
// User user = userService.getLoginData(username);
|
||||
// if (user == null) {
|
||||
// throw new BadRequestException("用户不存在");
|
||||
// } else {
|
||||
// if (!user.getEnabled()) {
|
||||
// throw new BadRequestException("账号未激活!");
|
||||
// }
|
||||
// // 获取用户的权限
|
||||
// List<AuthorityDto> authorities = roleService.buildPermissions(user);
|
||||
// // 初始化JwtUserDto
|
||||
// jwtUserDto = new JwtUserDto(BeanUtil.copyProperties(user, LoginUserDto.class), UserTypeEnum.getByValue(user.getUserType()),dataService.getDeptIds(user), authorities);
|
||||
// // 添加缓存数据
|
||||
// userCacheManager.addUserCache(username, jwtUserDto);
|
||||
// }
|
||||
// }
|
||||
// return jwtUserDto;
|
||||
// }
|
||||
}
|
||||
|
@ -44,28 +44,12 @@ public class JwtUserDto implements UserDetails {
|
||||
@ApiModelProperty("用户类型")
|
||||
private final UserTypeEnum userType;
|
||||
|
||||
@ApiModelProperty(value = "数据权限")
|
||||
private final List<Long> dataScopes;
|
||||
// @ApiModelProperty(value = "数据权限")
|
||||
// private final List<Long> dataScopes;
|
||||
|
||||
@ApiModelProperty(value = "角色")
|
||||
private final List<AuthorityDto> authorities;
|
||||
|
||||
@ApiModelProperty(value = "飞行员端信息")
|
||||
private final EmEmployees emEmployees;
|
||||
|
||||
public JwtUserDto createcnCustomerJwtUserDto(CnCustomer cnCustomer){
|
||||
LoginUserDto userDto = new LoginUserDto();
|
||||
userDto.setId(cnCustomer.getId());
|
||||
userDto.setUsername(cnCustomer.getPhone());
|
||||
userDto.setNickName(cnCustomer.getName());
|
||||
userDto.setDept(new Dept());
|
||||
userDto.setPassword(null);
|
||||
userDto.setEnabled(true);
|
||||
userDto.setIsAdmin(false);
|
||||
return new JwtUserDto(userDto, UserTypeEnum.CUSTOMER,new ArrayList<>(),new ArrayList<>(),null);
|
||||
}
|
||||
|
||||
|
||||
public Set<String> getRoles() {
|
||||
return authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());
|
||||
}
|
||||
|
@ -2,53 +2,26 @@ package com.aircraft.modules.system.controller;
|
||||
|
||||
|
||||
import com.aircraft.annotation.Log;
|
||||
import com.aircraft.annotation.rest.AnonymousPostMapping;
|
||||
import com.aircraft.config.properties.RsaProperties;
|
||||
import com.aircraft.exception.BadRequestException;
|
||||
import com.aircraft.modules.security.config.BLoginProperties;
|
||||
import com.aircraft.modules.security.config.LoginProperties;
|
||||
import com.aircraft.modules.security.config.SecurityProperties;
|
||||
import com.aircraft.modules.security.security.TokenProvider;
|
||||
import com.aircraft.modules.security.service.BOnlineUserService;
|
||||
import com.aircraft.modules.security.service.BUserDetailsService;
|
||||
import com.aircraft.modules.security.service.OnlineUserService;
|
||||
import com.aircraft.modules.security.service.UserDetailsServiceImpl;
|
||||
import com.aircraft.modules.security.service.dto.AuthUserDto;
|
||||
import com.aircraft.modules.security.service.dto.JwtCustomerDto;
|
||||
import com.aircraft.modules.system.domain.CnCustomer;
|
||||
import com.aircraft.modules.system.service.CnCustomerService;
|
||||
import com.aircraft.utils.PageResult;
|
||||
import com.aircraft.utils.RedisUtils;
|
||||
import com.aircraft.utils.RsaUtils;
|
||||
import com.aircraft.utils.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import kotlin.Result;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@ -67,16 +40,8 @@ public class CnCustomerController {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CnCustomerController.class);
|
||||
@Autowired
|
||||
private CnCustomerService entityService;
|
||||
private final SecurityProperties properties;
|
||||
private final RedisUtils redisUtils;
|
||||
private final OnlineUserService onlineUserService;
|
||||
private final TokenProvider tokenProvider;
|
||||
private final LoginProperties loginProperties;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final UserDetailsServiceImpl userDetailsService;
|
||||
private final BUserDetailsService bUserDetailsService;
|
||||
private final BLoginProperties bLoginProperties;
|
||||
private final BOnlineUserService bOnlineUserService;
|
||||
|
||||
|
||||
@Log("分页查询客户")
|
||||
@ApiOperation(value = "分页查询客户", notes = "分页查询客户")
|
||||
@ -107,6 +72,10 @@ public class CnCustomerController {
|
||||
@RequestMapping(method = {RequestMethod.POST})
|
||||
public ResponseEntity<Object> add(@Valid @RequestBody final CnCustomer entity) {
|
||||
try {
|
||||
CnCustomer cnCustomer = entityService.findByUserName(entity.getUsername());
|
||||
if (cnCustomer != null) {
|
||||
throw new RuntimeException("用户名已存在!");
|
||||
}
|
||||
entity.setStatus("1");
|
||||
entity.setPassword(passwordEncoder.encode("123456"));
|
||||
entityService.save(entity);
|
||||
@ -143,50 +112,5 @@ public class CnCustomerController {
|
||||
return new ResponseEntity<>(Collections.emptyList(),HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("B端用户登录")
|
||||
@ApiOperation("B端登录授权")
|
||||
@AnonymousPostMapping(value = "/b/login")
|
||||
public ResponseEntity<Object> loginB(@Validated @RequestBody AuthUserDto authUser,
|
||||
HttpServletRequest request) throws Exception {
|
||||
|
||||
// 1. 密码解密(与后台相同)
|
||||
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
|
||||
|
||||
// 3. 使用B端专属服务加载用户 ★核心修改★
|
||||
// 假设:BUserDetailsService 是专门为B端实现的UserDetailsService
|
||||
JwtCustomerDto jwtUser = bUserDetailsService.loadUserByUsername(authUser.getUsername());
|
||||
|
||||
// 4. 密码验证(保持相同逻辑)
|
||||
if (!passwordEncoder.matches(password, jwtUser.getPassword())) {
|
||||
throw new BadRequestException("登录密码错误");
|
||||
}
|
||||
Map<String, String> details = new HashMap<>();
|
||||
details.put("userType", String.valueOf(jwtUser.getUserType().getValue()));
|
||||
// 5. 设置认证信息
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||
jwtUser, null, jwtUser.getAuthorities()
|
||||
);
|
||||
authentication.setDetails(details);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
|
||||
// 6. 生成令牌(复用相同机制)
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
|
||||
// 7. 返回信息(可调整返回字段)
|
||||
Map<String, Object> authInfo = new HashMap<>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
put("user", jwtUser); // 返回B端用户信息
|
||||
}};
|
||||
|
||||
// 8. 单设备登录控制(可选)
|
||||
if (bLoginProperties.isSingleLogin()) { // B端专属配置
|
||||
bOnlineUserService.kickOutForUsername(authUser.getUsername());
|
||||
}
|
||||
|
||||
// 9. 保存B端在线用户 ★核心修改★
|
||||
bOnlineUserService.save(jwtUser, token, request);
|
||||
|
||||
return ResponseEntity.ok(authInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -158,47 +158,4 @@ public class EmEmployeesController {
|
||||
return new ResponseEntity<>(records,HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("用户登录")
|
||||
@ApiOperation("登录授权")
|
||||
@AnonymousPostMapping(value = "/login")
|
||||
public ResponseEntity<Object> login(@Validated @RequestBody AuthUserDto authUser, HttpServletRequest request) throws Exception {
|
||||
// 密码解密
|
||||
String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword());
|
||||
// 查询验证码
|
||||
// String code = redisUtils.get(authUser.getUuid(), String.class);
|
||||
// 清除验证码
|
||||
// redisUtils.del(authUser.getUuid());
|
||||
// if (StringUtils.isBlank(code)) {
|
||||
// throw new BadRequestException("验证码不存在或已过期");
|
||||
// }
|
||||
// if (StringUtils.isBlank(authUser.getCode()) || !authUser.getCode().equalsIgnoreCase(code)) {
|
||||
// throw new BadRequestException("验证码错误");
|
||||
// }
|
||||
// 获取用户信息
|
||||
JwtUserDto jwtUser = userDetailsService.loadUserByUsername(authUser.getUsername());
|
||||
// 验证用户密码
|
||||
// if (!passwordEncoder.matches(password, jwtUser.getPassword())) {
|
||||
// throw new BadRequestException("登录密码错误");
|
||||
// }
|
||||
Map<String, String> details = new HashMap<>();
|
||||
details.put("userType", String.valueOf(jwtUser.getUserType().getValue()));
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(jwtUser, null, jwtUser.getAuthorities());
|
||||
authentication.setDetails(details);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
// 生成令牌
|
||||
String token = tokenProvider.createToken(jwtUser);
|
||||
// 返回 token 与 用户信息
|
||||
Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
|
||||
put("token", properties.getTokenStartWith() + token);
|
||||
put("user", jwtUser);
|
||||
}};
|
||||
if (loginProperties.isSingleLogin()) {
|
||||
// 踢掉之前已经登录的token
|
||||
onlineUserService.kickOutForUsername(authUser.getUsername());
|
||||
}
|
||||
// 保存在线信息
|
||||
onlineUserService.save(jwtUser, token, request);
|
||||
// 返回登录信息
|
||||
return ResponseEntity.ok(authInfo);
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
@ -56,4 +57,6 @@ public class CnCustomer extends BaseEntity {
|
||||
@ApiModelProperty(value = "密码")
|
||||
private String password;
|
||||
|
||||
@ApiModelProperty(value = "用户名称")
|
||||
private String username;
|
||||
}
|
||||
|
@ -69,7 +69,6 @@ public class EmEmployees extends BaseEntity {
|
||||
@ApiModelProperty(value = "密码")
|
||||
private String password;
|
||||
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "用户名称")
|
||||
private String username;
|
||||
|
||||
|
@ -42,5 +42,5 @@ public interface EmEmployeesMapper extends BaseMapper<EmEmployees> {
|
||||
|
||||
List<EmEmployeesDetailVo> findByExample(@Param(Constants.WRAPPER) QueryWrapper<EmEmployeesDetailVo> queryWrapper, Page page);
|
||||
|
||||
void deleteBatchIds(@Param("ids") Set<Long> ids);
|
||||
void deleteBatchIds(@Param("ids") List<Long> ids);
|
||||
}
|
@ -37,5 +37,7 @@ public interface CnCustomerService extends IService<CnCustomer> {
|
||||
* @param phone
|
||||
* @return
|
||||
*/
|
||||
CnCustomer findByphone(String phone);
|
||||
CnCustomer findByPhone(String phone);
|
||||
|
||||
CnCustomer findByUserName(String username);
|
||||
}
|
||||
|
@ -98,4 +98,6 @@ public interface EmEmployeesService extends IService<EmEmployees> {
|
||||
* @return
|
||||
*/
|
||||
EmEmployeesDetailVo findById(Integer id);
|
||||
|
||||
EmEmployees findByUsername(String username);
|
||||
}
|
@ -36,7 +36,7 @@ public class CnCustomerServiceImpl extends ServiceImpl<CnCustomerMapper, CnCusto
|
||||
}
|
||||
|
||||
@Override
|
||||
public CnCustomer findByphone(String phone) {
|
||||
public CnCustomer findByPhone(String phone) {
|
||||
QueryWrapper<CnCustomer> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.lambda()
|
||||
.eq(CnCustomer::getPhone,phone)
|
||||
@ -44,6 +44,15 @@ public class CnCustomerServiceImpl extends ServiceImpl<CnCustomerMapper, CnCusto
|
||||
return this.baseMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CnCustomer findByUserName(String username) {
|
||||
QueryWrapper<CnCustomer> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.lambda()
|
||||
.eq(CnCustomer::getUsername,username)
|
||||
.eq(BaseEntity::getDelFlag,0);
|
||||
return this.baseMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询
|
||||
*
|
||||
|
@ -16,12 +16,9 @@
|
||||
package com.aircraft.modules.system.service.impl;
|
||||
|
||||
import com.aircraft.base.BaseEntity;
|
||||
import com.aircraft.modules.security.service.UserCacheManager;
|
||||
import com.aircraft.modules.system.domain.EmEmployees;
|
||||
import com.aircraft.modules.system.domain.vo.EmEmployeesDetailVo;
|
||||
import com.aircraft.modules.system.domain.vo.EmEmployeesVo;
|
||||
import com.aircraft.modules.system.domain.User;
|
||||
import com.aircraft.modules.system.mapper.UserMapper;
|
||||
import com.aircraft.modules.system.service.EmAreaService;
|
||||
import com.aircraft.modules.system.service.EmScenicService;
|
||||
import com.aircraft.utils.*;
|
||||
@ -40,6 +37,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* @description 服务实现
|
||||
@ -68,7 +66,11 @@ public class EmEmployeesServiceImpl extends ServiceImpl<EmEmployeesMapper, EmEmp
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void create(EmEmployeesVo resources) {
|
||||
EmEmployees emEmployees = new EmEmployees();
|
||||
EmEmployees emEmployees = this.findByUsername(resources.getUsername());
|
||||
if (emEmployees != null) {
|
||||
throw new RuntimeException("用户名已存在!");
|
||||
}
|
||||
emEmployees = new EmEmployees();
|
||||
BeanUtils.copyProperties(resources, emEmployees);
|
||||
emEmployees.setPassword(passwordEncoder.encode("123456"));
|
||||
emEmployees.setDelFlag(0);
|
||||
@ -76,6 +78,14 @@ public class EmEmployeesServiceImpl extends ServiceImpl<EmEmployeesMapper, EmEmp
|
||||
emEmployeesMapper.insert(resources);
|
||||
}
|
||||
|
||||
public EmEmployees findByUsername(@NotBlank String username) {
|
||||
QueryWrapper<EmEmployees> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.lambda()
|
||||
.eq(EmEmployees::getUsername,username)
|
||||
.eq(BaseEntity::getDelFlag,0);
|
||||
return emEmployeesMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(EmEmployees resources) {
|
||||
@ -87,7 +97,7 @@ public class EmEmployeesServiceImpl extends ServiceImpl<EmEmployeesMapper, EmEmp
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteAll(List<Long> ids) {
|
||||
this.removeByIds(ids);
|
||||
emEmployeesMapper.deleteBatchIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,11 +16,11 @@
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id, name, gender, phone, department_id, status, create_time, update_time, valid, area_id, scenic_id, qualification, qualification_attachment, userid
|
||||
id, name, phone, status, create_time, update_time, valid, area_id, scenic_id, qualification, qualification_attachment,username
|
||||
</sql>
|
||||
<delete id="deleteBatchIds">
|
||||
UPDATE em_employees
|
||||
SET del_flag = 0
|
||||
SET del_flag = 1
|
||||
where id in
|
||||
<foreach collection="ids" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
|
Loading…
Reference in New Issue
Block a user