From fdb0e3fdd212b1b774a8bc73bc8468e96ad4219c Mon Sep 17 00:00:00 2001 From: xuyang <748613696@qq.com> Date: Tue, 27 Jun 2023 10:18:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/granter/SmsTokenGranter.java | 4 ++- .../java/com/njcn/common/utils/PubUtils.java | 21 +++++++++---- .../user/service/impl/AppRoleServiceImpl.java | 3 +- .../user/service/impl/AppUserServiceImpl.java | 4 +-- .../service/impl/ReferralCodeServiceImpl.java | 30 +++++-------------- .../user/service/impl/UserServiceImpl.java | 22 +++++++------- .../user/service/impl/UserSetServiceImpl.java | 4 ++- 7 files changed, 45 insertions(+), 43 deletions(-) diff --git a/pqs-auth/src/main/java/com/njcn/auth/security/granter/SmsTokenGranter.java b/pqs-auth/src/main/java/com/njcn/auth/security/granter/SmsTokenGranter.java index 65f096439..823ffea31 100644 --- a/pqs-auth/src/main/java/com/njcn/auth/security/granter/SmsTokenGranter.java +++ b/pqs-auth/src/main/java/com/njcn/auth/security/granter/SmsTokenGranter.java @@ -2,8 +2,10 @@ package com.njcn.auth.security.granter; import cn.hutool.core.util.StrUtil; import com.njcn.auth.security.token.SmsCodeAuthenticationToken; +import com.njcn.common.pojo.constant.PatternRegex; import com.njcn.common.pojo.constant.SecurityConstants; import com.njcn.common.pojo.exception.BusinessException; +import com.njcn.common.utils.PubUtils; import com.njcn.redis.pojo.enums.RedisKeyEnum; import com.njcn.redis.utils.RedisUtil; import com.njcn.user.enums.UserResponseEnum; @@ -45,7 +47,7 @@ public class SmsTokenGranter extends AbstractTokenGranter { Map parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters()); String phone = parameters.get(SecurityConstants.PHONE); String smsCode = parameters.get(SecurityConstants.SMS_CODE); - if (StrUtil.isBlank(phone)) { + if (StrUtil.isBlank(phone) || !PubUtils.match(PatternRegex.PHONE_REGEX, phone)) { throw new BusinessException(UserResponseEnum.REGISTER_PHONE_WRONG); } if (!judgeSmsCode(phone, smsCode)) { diff --git a/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java b/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java index 3b4c19ace..6621cbe14 100644 --- a/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java +++ b/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java @@ -23,10 +23,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Locale; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -323,8 +320,20 @@ public class PubUtils { return matcher.matches(); } - public static boolean patternPasswordPhone(String password) { - return match(PatternRegex.PASSWORD_PHONE_REGEX, password); + /** + * 生成随机推荐码 + */ + public static String getCode(Integer number){ + final String BASIC = "123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZZ"; + char[] basicArray = BASIC.toCharArray(); + Random random = new Random(); + char[] result = new char[number]; + for (int i = 0; i < result.length; i++) { + int index = random.nextInt(100) % (basicArray.length); + result[i] = basicArray[index]; + } + return new String(result); } + //***************************************************添加结束******************************************************** } diff --git a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppRoleServiceImpl.java b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppRoleServiceImpl.java index bcfb769bf..06fc92a72 100644 --- a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppRoleServiceImpl.java +++ b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppRoleServiceImpl.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.njcn.common.pojo.exception.BusinessException; +import com.njcn.common.utils.PubUtils; import com.njcn.redis.utils.RedisUtil; import com.njcn.user.enums.UserResponseEnum; import com.njcn.user.pojo.po.UserRole; @@ -53,7 +54,7 @@ public class AppRoleServiceImpl implements IAppRoleService { LinkedHashMap roleMap = new LinkedHashMap<>(); for (Map.Entry entry : map.entrySet()) { if (Objects.equals(entry.getKey(),referralCode)){ - roleMap.put(ReferralCodeServiceImpl.getCode(),entry.getValue()); + roleMap.put(PubUtils.getCode(6),entry.getValue()); } else { roleMap.put(entry.getKey(),entry.getValue()); } diff --git a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppUserServiceImpl.java b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppUserServiceImpl.java index 0966b28ee..a494c45c5 100644 --- a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppUserServiceImpl.java +++ b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/AppUserServiceImpl.java @@ -259,7 +259,7 @@ public class AppUserServiceImpl extends ServiceImpl impleme } private void judgeCode(String phone, String code, String devCode) { - String key = phone + devCode; + String key = RedisKeyEnum.SMS_LOGIN_KEY.getKey() + phone; String redisCode = redisUtil.getStringByKey(key); if (StringUtils.isEmpty(redisCode) || !code.equalsIgnoreCase(redisCode)) { throw new BusinessException(UserResponseEnum.LOGIN_WRONG_CODE); @@ -286,7 +286,7 @@ public class AppUserServiceImpl extends ServiceImpl impleme user.setState(UserState.ENABLE); user.setOrigin(UserState.NORMAL_ORIGIN); user.setCasualUser(UserType.OFFICIAL); - user.setPwdState(UserState.NEED); + user.setPwdState(UserState.NEEDLESS); user.setRegisterTime(LocalDateTime.now()); user.setLoginTime(LocalDateTime.now()); user.setPwdValidity(LocalDateTime.now()); diff --git a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/ReferralCodeServiceImpl.java b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/ReferralCodeServiceImpl.java index 2f3afddd5..126b9d3b4 100644 --- a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/ReferralCodeServiceImpl.java +++ b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/ReferralCodeServiceImpl.java @@ -1,5 +1,6 @@ package com.njcn.user.service.impl; +import com.njcn.common.utils.PubUtils; import com.njcn.redis.utils.RedisUtil; import com.njcn.user.pojo.vo.app.RoleReferralCodeVO; import com.njcn.user.service.IReferralCodeService; @@ -22,8 +23,6 @@ import java.util.*; @AllArgsConstructor public class ReferralCodeServiceImpl implements IReferralCodeService { - private static final String BASIC = "123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZZ"; - private final RedisUtil redisUtil; private final IRoleService roleService; @@ -34,9 +33,9 @@ public class ReferralCodeServiceImpl implements IReferralCodeService { Object object = redisUtil.getObjectByKey("roleReferralCode"); if (Objects.isNull(object)){ LinkedHashMap roleMap = new LinkedHashMap<>(); - String code1 = getCode(); - String code2 = getCode(); - String code3 = getCode(); + String code1 = PubUtils.getCode(6); + String code2 = PubUtils.getCode(6); + String code3 = PubUtils.getCode(6); roleMap.put(code1,"market_user"); roleMap.put(code2,"engineering_user"); roleMap.put(code3,"app_user"); @@ -66,9 +65,9 @@ public class ReferralCodeServiceImpl implements IReferralCodeService { @Override public List refreshReferralCode() { LinkedHashMap roleMap = new LinkedHashMap<>(); - String code1 = getCode(); - String code2 = getCode(); - String code3 = getCode(); + String code1 = PubUtils.getCode(6); + String code2 = PubUtils.getCode(6); + String code3 = PubUtils.getCode(6); roleMap.put(code1,"market_user"); roleMap.put(code2,"engineering_user"); roleMap.put(code3,"app_user"); @@ -82,21 +81,8 @@ public class ReferralCodeServiceImpl implements IReferralCodeService { RoleReferralCodeVO vo3 = new RoleReferralCodeVO(); vo3.setRoleName(roleService.getRoleByCode("app_user").getName()); vo3.setRoleReferralCode(code3); + return Arrays.asList(vo1,vo2,vo3); } - /** - * 生成随机推荐码 - */ - public static String getCode(){ - char[] basicArray = BASIC.toCharArray(); - Random random = new Random(); - char[] result = new char[6]; - for (int i = 0; i < result.length; i++) { - int index = random.nextInt(100) % (basicArray.length); - result[i] = basicArray[index]; - } - return new String(result); - } - } diff --git a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserServiceImpl.java b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserServiceImpl.java index 4cf06ddf1..672d8837a 100644 --- a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserServiceImpl.java +++ b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserServiceImpl.java @@ -251,19 +251,21 @@ public class UserServiceImpl extends ServiceImpl implements IU return null; } BeanUtil.copyProperties(user, userVO); - Dept dept = deptService.getDeptById(user.getDeptId()); - //非自定义部门 - if (Objects.equals(dept.getType(),0)){ - String areaId = deptService.getAreaIdByDeptId(user.getDeptId()); - userVO.setAreaId(areaId); - userVO.setAreaName(areaFeignClient.selectIdArea(areaId).getData().getName()); - } else { - userVO.setAreaName(deptService.getNameByDeptId(user.getDeptId())); + if (!Objects.isNull(user.getDeptId())){ + Dept dept = deptService.getDeptById(user.getDeptId()); + //非自定义部门 + if (Objects.equals(dept.getType(),0)){ + String areaId = deptService.getAreaIdByDeptId(user.getDeptId()); + userVO.setAreaId(areaId); + userVO.setAreaName(areaFeignClient.selectIdArea(areaId).getData().getName()); + } else { + userVO.setAreaName(deptService.getNameByDeptId(user.getDeptId())); + } + userVO.setDeptName(deptService.getNameByDeptId(user.getDeptId())); + userVO.setDeptLevel(dept.getPids().split(StrUtil.COMMA).length); } - userVO.setDeptName(deptService.getNameByDeptId(user.getDeptId())); userVO.setRoleList(roleService.getIdByUserId(id)); userVO.setRole(roleService.getNameByUserId(id)); - userVO.setDeptLevel(dept.getPids().split(StrUtil.COMMA).length); return userVO; } diff --git a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserSetServiceImpl.java b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserSetServiceImpl.java index 809d56b7a..6fd513b8e 100644 --- a/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserSetServiceImpl.java +++ b/pqs-user/user-boot/src/main/java/com/njcn/user/service/impl/UserSetServiceImpl.java @@ -53,8 +53,10 @@ public class UserSetServiceImpl extends ServiceImpl impl String secretKey = PubUtils.randomCode(16); userSet.setSecretKey(secretKey); Sm4Utils sm4 = new Sm4Utils(secretKey); + String password = PubUtils.getCode(8); + System.out.println("password==:" + password); //SM4加密初始默认密码 - String strSm4 = sm4.encryptData_ECB(UserDefaultPassword.DEFAULT_PASSWORD); + String strSm4 = sm4.encryptData_ECB(password); userSet.setStandBy(strSm4); this.save(userSet); return userSet;