|
|
@@ -5,23 +5,29 @@ import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
import cn.hutool.http.useragent.UserAgent;
|
|
|
import cn.hutool.http.useragent.UserAgentUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.nb.common.Constants;
|
|
|
import com.nb.common.MenuConstants;
|
|
|
+import com.nb.common.apply.ApplyManager;
|
|
|
import com.nb.common.bo.LoginUser;
|
|
|
+import com.nb.common.bo.SysApplyBO;
|
|
|
import com.nb.common.bo.SysRoleBO;
|
|
|
import com.nb.common.bo.SysUserBO;
|
|
|
import com.nb.common.dto.LoginDTO;
|
|
|
import com.nb.common.enums.*;
|
|
|
import com.nb.common.exception.CustomException;
|
|
|
+import com.nb.common.exception.apply.ApplyTokenParamException;
|
|
|
import com.nb.common.util.AddressUtil;
|
|
|
import com.nb.common.util.IpUtil;
|
|
|
import com.nb.common.util.SecurityUtil;
|
|
|
import com.nb.framework.web.service.IUserService;
|
|
|
import com.nb.system.common.vo.*;
|
|
|
+import com.nb.system.entity.SysApply;
|
|
|
import com.nb.system.entity.SysMenu;
|
|
|
import com.nb.system.entity.SysRole;
|
|
|
import com.nb.system.entity.SysUser;
|
|
|
@@ -32,7 +38,9 @@ import com.nb.system.service.ISysUserService;
|
|
|
import com.nb.system.utils.CaptchaTool;
|
|
|
import com.google.common.collect.Sets;
|
|
|
import com.nb.system.utils.TenantUtil;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -48,23 +56,21 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
+@AllArgsConstructor
|
|
|
@EnableConfigurationProperties(CaptchaProperties.class)
|
|
|
public class UserServiceImpl implements IUserService {
|
|
|
|
|
|
- @Resource
|
|
|
- private ISysMenuService sysMenuService;
|
|
|
+ private final ISysMenuService sysMenuService;
|
|
|
|
|
|
- @Resource
|
|
|
- private CaptchaTool captchaTool;
|
|
|
+ private final CaptchaTool captchaTool;
|
|
|
|
|
|
- @Resource
|
|
|
- private CaptchaProperties captchaProperties;
|
|
|
+ private final CaptchaProperties captchaProperties;
|
|
|
|
|
|
- @Resource
|
|
|
- private ISysUserService sysUserService;
|
|
|
+ private final ISysUserService sysUserService;
|
|
|
|
|
|
- @Resource
|
|
|
- private ISysRoleService sysRoleService;
|
|
|
+ private final ISysRoleService sysRoleService;
|
|
|
+
|
|
|
+ private final ApplyManager applyManager;
|
|
|
|
|
|
@Override
|
|
|
public String login(LoginDTO req) {
|
|
|
@@ -74,103 +80,10 @@ public class UserServiceImpl implements IUserService {
|
|
|
if (!GrantTypeEnum.contains(req.getGrantType())) {
|
|
|
throw new CustomException("授权类型暂不支持");
|
|
|
}
|
|
|
- SysUser sysUser = null;
|
|
|
- if (req.getGrantType().equals(GrantTypeEnum.USERNAME_PASSWORD.getCode())) {
|
|
|
- if (StrUtil.isBlank(req.getUsername())) {
|
|
|
- throw new CustomException("用户名不能为空");
|
|
|
- }
|
|
|
- if (StrUtil.isBlank(req.getPassword())) {
|
|
|
- throw new CustomException("密码不能为空");
|
|
|
- }
|
|
|
- if(captchaProperties.isEnable()){
|
|
|
- HttpServletRequest request = SpringMVCUtil.getRequest();
|
|
|
- String requestFrom = request.getHeader("RequestFrom");
|
|
|
- //来自app的请求不需要验证码
|
|
|
-// if(!"TuoRenApp".equals(requestFrom)){
|
|
|
-// captchaTool.ver(req.getCodeKey(),req.getCode());
|
|
|
-// }
|
|
|
- }
|
|
|
- sysUser = sysUserService.getOne(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getAccount, req.getUsername()));
|
|
|
- if (Objects.isNull(sysUser)) {
|
|
|
- log.info("登录用户:{}不存在", req.getUsername());
|
|
|
- throw new CustomException("登录用户不存在");
|
|
|
- }
|
|
|
- if (!SecurityUtil.matchesPassword(req.getPassword(), sysUser.getPassword())) {
|
|
|
- throw new CustomException("账号或密码不正确");
|
|
|
- }
|
|
|
- if (sysUser.getDelFlag().equals(DelFlagEnum.YES.getCode())) {
|
|
|
- log.info("登录用户:{}已被删除", req.getUsername());
|
|
|
- throw new CustomException("对不起,您的账号已被删除");
|
|
|
- }
|
|
|
- if (sysUser.getStatus().equals(StatusEnum.NO.getCode())) {
|
|
|
- log.info("登录用户:{}已被停用", req.getUsername());
|
|
|
- throw new CustomException("对不起,您的账号已被停用");
|
|
|
- }
|
|
|
- }
|
|
|
- if (req.getGrantType().equals(GrantTypeEnum.MOBILE_CODE.getCode())) {
|
|
|
- if (StrUtil.isBlank(req.getMobile())) {
|
|
|
- throw new CustomException("手机号不能为空");
|
|
|
- }
|
|
|
- if (StrUtil.isBlank(req.getCode())) {
|
|
|
- throw new CustomException("验证码不能为空");
|
|
|
- }
|
|
|
- // TODO,短信验证码校验,自行实现
|
|
|
- if (!Objects.equals(req.getCode(), Constants.DEFAULT_SMS_CODE)) {
|
|
|
- throw new CustomException("账号或密码不正确");
|
|
|
- }
|
|
|
- sysUser = sysUserService.getOne(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getPhone, req.getMobile()));
|
|
|
- if (Objects.isNull(sysUser)) {
|
|
|
- log.info("登录用户:{}不存在", req.getUsername());
|
|
|
- throw new CustomException("登录用户不存在");
|
|
|
- }
|
|
|
- if (sysUser.getDelFlag().equals(DelFlagEnum.YES.getCode())) {
|
|
|
- log.info("登录用户:{}已被删除", req.getUsername());
|
|
|
- throw new CustomException("对不起,您的账号已被删除");
|
|
|
- }
|
|
|
- if (sysUser.getStatus().equals(StatusEnum.NO.getCode())) {
|
|
|
- log.info("登录用户:{}已被停用", req.getUsername());
|
|
|
- throw new CustomException("对不起,您的账号已被停用");
|
|
|
- }
|
|
|
- }
|
|
|
- log.info("登录用户:{}", req.getUsername());
|
|
|
- SysUserBO sysUserBO = BeanUtil.copyProperties(sysUser, SysUserBO.class);
|
|
|
- // 查询角色列表
|
|
|
- List<SysRole> sysRoleList = sysRoleService.listSysRoleByUserId(sysUser.getId());
|
|
|
- // 设置角色列表
|
|
|
- sysUserBO.setRoles(sysRoleList.stream().map(item -> BeanUtil.copyProperties(item, SysRoleBO.class)).collect(Collectors.toList()));
|
|
|
- // 查询权限标识
|
|
|
- Set<String> permissions = Sets.newHashSet();
|
|
|
- if (CollUtil.isNotEmpty(sysRoleList)&&
|
|
|
- sysRoleList.stream().anyMatch(sysRole -> "admin".equalsIgnoreCase(sysRole.getRoleCode()))) {
|
|
|
- permissions.add(Constants.ALL_PERMISSION);
|
|
|
- } else {
|
|
|
- permissions = sysMenuService.getPermissionsByUserId(sysUser.getId());
|
|
|
- }
|
|
|
- // 登录
|
|
|
- StpUtil.login(sysUser.getId());
|
|
|
- LoginUser loginUser = new LoginUser();
|
|
|
- loginUser.setToken(StpUtil.getTokenValue());
|
|
|
- loginUser.setUserPlatform(UserPlatformEnum.WEB.getCode());
|
|
|
- loginUser.setGrantType(req.getGrantType());
|
|
|
- loginUser.setSysUser(sysUserBO);
|
|
|
- loginUser.setIsSys(sysUser.getIsSys());
|
|
|
- if (req.getGrantType().equals(GrantTypeEnum.USERNAME_PASSWORD.getCode())) {
|
|
|
- loginUser.setUsername(req.getUsername());
|
|
|
- }
|
|
|
- if (req.getGrantType().equals(GrantTypeEnum.MOBILE_CODE.getCode())) {
|
|
|
- loginUser.setUsername(req.getMobile());
|
|
|
- }
|
|
|
- loginUser.setLoginTime(new Date());
|
|
|
- HttpServletRequest request = SpringMVCUtil.getRequest();
|
|
|
- UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
|
|
- String ipAddress = IpUtil.getClientIp(request);
|
|
|
- loginUser.setIpAddress(ipAddress);
|
|
|
- loginUser.setLoginLocation(AddressUtil.getRealAddressByIp(ipAddress));
|
|
|
- loginUser.setBrowser(userAgent.getBrowser().getName());
|
|
|
- loginUser.setOs(userAgent.getOs().getName());
|
|
|
- loginUser.setPermissions(permissions);
|
|
|
- // 设置用户信息
|
|
|
- StpUtil.getTokenSession().set(Constants.LOGIN_USER_KEY, loginUser);
|
|
|
+ //用户登录
|
|
|
+ userLogin(req);
|
|
|
+ //第三方应用登录
|
|
|
+ applyLogin(req);
|
|
|
return StpUtil.getTokenValue();
|
|
|
}
|
|
|
|
|
|
@@ -291,4 +204,159 @@ public class UserServiceImpl implements IUserService {
|
|
|
return node;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ private void userLogin(LoginDTO req){
|
|
|
+ SysUser sysUser = null;
|
|
|
+ if (req.getGrantType().equals(GrantTypeEnum.USERNAME_PASSWORD.getCode())) {
|
|
|
+ if (StrUtil.isBlank(req.getUsername())) {
|
|
|
+ throw new CustomException("用户名不能为空");
|
|
|
+ }
|
|
|
+ if (StrUtil.isBlank(req.getPassword())) {
|
|
|
+ throw new CustomException("密码不能为空");
|
|
|
+ }
|
|
|
+ if(captchaProperties.isEnable()){
|
|
|
+ HttpServletRequest request = SpringMVCUtil.getRequest();
|
|
|
+ String requestFrom = request.getHeader("RequestFrom");
|
|
|
+ //来自app的请求不需要验证码
|
|
|
+ if(!"TuoRenApp".equals(requestFrom)){
|
|
|
+ captchaTool.ver(req.getCodeKey(),req.getCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sysUser = sysUserService.getOne(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getAccount, req.getUsername()));
|
|
|
+ if (Objects.isNull(sysUser)) {
|
|
|
+ log.info("登录用户:{}不存在", req.getUsername());
|
|
|
+ throw new CustomException("登录用户不存在");
|
|
|
+ }
|
|
|
+ if (!SecurityUtil.matchesPassword(req.getPassword(), sysUser.getPassword())) {
|
|
|
+ throw new CustomException("账号或密码不正确");
|
|
|
+ }
|
|
|
+ if (sysUser.getDelFlag().equals(DelFlagEnum.YES.getCode())) {
|
|
|
+ log.info("登录用户:{}已被删除", req.getUsername());
|
|
|
+ throw new CustomException("对不起,您的账号已被删除");
|
|
|
+ }
|
|
|
+ if (sysUser.getStatus().equals(StatusEnum.NO.getCode())) {
|
|
|
+ log.info("登录用户:{}已被停用", req.getUsername());
|
|
|
+ throw new CustomException("对不起,您的账号已被停用");
|
|
|
+ }
|
|
|
+ }else if (req.getGrantType().equals(GrantTypeEnum.MOBILE_CODE.getCode())) {
|
|
|
+ if (StrUtil.isBlank(req.getMobile())) {
|
|
|
+ throw new CustomException("手机号不能为空");
|
|
|
+ }
|
|
|
+ if (StrUtil.isBlank(req.getCode())) {
|
|
|
+ throw new CustomException("验证码不能为空");
|
|
|
+ }
|
|
|
+ // TODO,短信验证码校验,自行实现
|
|
|
+ if (!Objects.equals(req.getCode(), Constants.DEFAULT_SMS_CODE)) {
|
|
|
+ throw new CustomException("账号或密码不正确");
|
|
|
+ }
|
|
|
+ sysUser = sysUserService.getOne(Wrappers.lambdaQuery(SysUser.class).eq(SysUser::getPhone, req.getMobile()));
|
|
|
+ if (Objects.isNull(sysUser)) {
|
|
|
+ log.info("登录用户:{}不存在", req.getUsername());
|
|
|
+ throw new CustomException("登录用户不存在");
|
|
|
+ }
|
|
|
+ if (sysUser.getDelFlag().equals(DelFlagEnum.YES.getCode())) {
|
|
|
+ log.info("登录用户:{}已被删除", req.getUsername());
|
|
|
+ throw new CustomException("对不起,您的账号已被删除");
|
|
|
+ }
|
|
|
+ if (sysUser.getStatus().equals(StatusEnum.NO.getCode())) {
|
|
|
+ log.info("登录用户:{}已被停用", req.getUsername());
|
|
|
+ throw new CustomException("对不起,您的账号已被停用");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("登录用户:{}", req.getUsername());
|
|
|
+ SysUserBO sysUserBO = BeanUtil.copyProperties(sysUser, SysUserBO.class);
|
|
|
+ // 查询角色列表
|
|
|
+ List<SysRole> sysRoleList = sysRoleService.listSysRoleByUserId(sysUser.getId());
|
|
|
+ // 设置角色列表
|
|
|
+ sysUserBO.setRoles(sysRoleList.stream().map(item -> BeanUtil.copyProperties(item, SysRoleBO.class)).collect(Collectors.toList()));
|
|
|
+ // 查询权限标识
|
|
|
+ Set<String> permissions = Sets.newHashSet();
|
|
|
+ if (CollUtil.isNotEmpty(sysRoleList)&&
|
|
|
+ sysRoleList.stream().anyMatch(sysRole -> "admin".equalsIgnoreCase(sysRole.getRoleCode()))) {
|
|
|
+ permissions.add(Constants.ALL_PERMISSION);
|
|
|
+ } else {
|
|
|
+ permissions = sysMenuService.getPermissionsByUserId(sysUser.getId());
|
|
|
+ }
|
|
|
+ // 登录
|
|
|
+ StpUtil.login(sysUser.getId());
|
|
|
+ LoginUser loginUser = new LoginUser();
|
|
|
+ loginUser.setToken(StpUtil.getTokenValue());
|
|
|
+ loginUser.setUserPlatform(UserPlatformEnum.WEB.getCode());
|
|
|
+ loginUser.setGrantType(req.getGrantType());
|
|
|
+ loginUser.setSysUser(sysUserBO);
|
|
|
+ loginUser.setIsSys(sysUser.getIsSys());
|
|
|
+ if (req.getGrantType().equals(GrantTypeEnum.USERNAME_PASSWORD.getCode())) {
|
|
|
+ loginUser.setUsername(req.getUsername());
|
|
|
+ }
|
|
|
+ if (req.getGrantType().equals(GrantTypeEnum.MOBILE_CODE.getCode())) {
|
|
|
+ loginUser.setUsername(req.getMobile());
|
|
|
+ }
|
|
|
+ loginUser.setLoginTime(new Date());
|
|
|
+ HttpServletRequest request = SpringMVCUtil.getRequest();
|
|
|
+ UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
|
|
+ String ipAddress = IpUtil.getClientIp(request);
|
|
|
+ loginUser.setIpAddress(ipAddress);
|
|
|
+ loginUser.setLoginLocation(AddressUtil.getRealAddressByIp(ipAddress));
|
|
|
+ loginUser.setBrowser(userAgent.getBrowser().getName());
|
|
|
+ loginUser.setOs(userAgent.getOs().getName());
|
|
|
+ loginUser.setPermissions(permissions);
|
|
|
+ // 设置用户信息
|
|
|
+ StpUtil.getTokenSession().set(Constants.LOGIN_USER_KEY, loginUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void applyLogin(LoginDTO req){
|
|
|
+ if (req.getGrantType().equals(GrantTypeEnum.APPKEY_APPSECRET.getCode())){
|
|
|
+ if (StrUtil.isBlank(req.getAppKey())) {
|
|
|
+ throw new ApplyTokenParamException("appKey不能为空");
|
|
|
+ }
|
|
|
+ if (StrUtil.isBlank(req.getTimestamp())) {
|
|
|
+ throw new ApplyTokenParamException("timestamp不能为空");
|
|
|
+ }
|
|
|
+ if (StrUtil.isBlank(req.getSign())) {
|
|
|
+ throw new ApplyTokenParamException("sign不能为空");
|
|
|
+ }
|
|
|
+ String appKey = req.getAppKey();
|
|
|
+ String appSecret = applyManager.getAppSecret(appKey);
|
|
|
+ //验证签名
|
|
|
+ if (signCorrect(req.getSign(), req.getTimestamp(), req.getAppKey(), appSecret)) {
|
|
|
+ LoginUser loginUser = new LoginUser();
|
|
|
+ SysApplyBO sysApply = new SysApplyBO();
|
|
|
+ sysApply.setAppKey(appKey);
|
|
|
+ sysApply.setTenantId(applyManager.getTenantId(appKey));
|
|
|
+ loginUser.setToken(StpUtil.getTokenValue());
|
|
|
+ loginUser.setUserPlatform(UserPlatformEnum.WEB.getCode());
|
|
|
+ loginUser.setGrantType(req.getGrantType());
|
|
|
+ loginUser.setSysApply(sysApply);
|
|
|
+ loginUser.setIsSys(false);
|
|
|
+ loginUser.setUsername(req.getAppKey());
|
|
|
+ loginUser.setLoginTime(new Date());
|
|
|
+ HttpServletRequest request = SpringMVCUtil.getRequest();
|
|
|
+ UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
|
|
+ String ipAddress = IpUtil.getClientIp(request);
|
|
|
+ loginUser.setIpAddress(ipAddress);
|
|
|
+ loginUser.setLoginLocation(AddressUtil.getRealAddressByIp(ipAddress));
|
|
|
+ loginUser.setBrowser(userAgent.getBrowser().getName());
|
|
|
+ loginUser.setOs(userAgent.getOs().getName());
|
|
|
+ loginUser.setPermissions(new HashSet<>(applyManager.getPermission(appKey)));
|
|
|
+ // 设置用户信息
|
|
|
+ StpUtil.getTokenSession().set(Constants.LOGIN_USER_KEY, loginUser);
|
|
|
+ }else {
|
|
|
+ throw new ApplyTokenParamException("签名错误");
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean signCorrect(String sign,String timestamp,String appKey,String appSecret){
|
|
|
+ JSONObject jsonObject = new JSONObject(true);
|
|
|
+ jsonObject.putOpt("appKey",appKey)
|
|
|
+ .putOpt("appSecret",appSecret)
|
|
|
+ .putOpt("timestamp",timestamp);
|
|
|
+ return DigestUtil.md5Hex(jsonObject.toString()).equalsIgnoreCase(sign);
|
|
|
+ }
|
|
|
+
|
|
|
}
|