From 0e12e4ab45db6768a0f45d8952f78b0ae9190723 Mon Sep 17 00:00:00 2001 From: ZQN <364596817@qq.com> Date: 星期一, 19 五月 2025 16:09:01 +0800 Subject: [PATCH] 手机号登录,去掉短信验证。脱敏 --- project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java | 110 ++++++++++++++++++++++++++++++++++++++++--------------- 1 files changed, 80 insertions(+), 30 deletions(-) diff --git a/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java b/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java index 44c3077..e622e67 100644 --- a/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java +++ b/project-framework/src/main/java/com/project/framework/web/service/SysLoginService.java @@ -1,7 +1,5 @@ package com.project.framework.web.service; -import javax.annotation.Resource; - import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.aliyuncs.CommonRequest; @@ -13,23 +11,13 @@ import com.aliyuncs.profile.DefaultProfile; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.project.common.constant.AliyunSmsConstants; -import com.project.common.exception.base.BaseException; -import com.project.system.domain.bo.editBo.UserPhoneLoginBo; -import com.project.system.mapper.SysUserMapper; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.BadCredentialsException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.stereotype.Component; import com.project.common.constant.CacheConstants; import com.project.common.constant.Constants; import com.project.common.core.domain.entity.SysUser; import com.project.common.core.domain.model.LoginUser; import com.project.common.core.redis.RedisCache; import com.project.common.exception.ServiceException; +import com.project.common.exception.base.BaseException; import com.project.common.exception.user.CaptchaException; import com.project.common.exception.user.CaptchaExpireException; import com.project.common.exception.user.UserPasswordNotMatchException; @@ -41,8 +29,18 @@ import com.project.framework.manager.AsyncManager; import com.project.framework.manager.factory.AsyncFactory; import com.project.framework.security.context.AuthenticationContextHolder; +import com.project.system.domain.bo.editBo.UserPhoneLoginBo; +import com.project.system.mapper.SysUserMapper; import com.project.system.service.ISysConfigService; import com.project.system.service.ISysUserService; +import com.project.system.sms.YPSmsApi; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Component; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -63,6 +61,7 @@ private final ISysUserService userService; private final SysUserMapper userMapper; private final ISysConfigService configService; + private final YPSmsApi smsApi; /** @@ -116,6 +115,26 @@ return tokenService.createToken(loginUser); } + /** + * 鏂版敞鍐岃幏鍙栭獙璇佺爜 + * @param phone 鎵嬫満 + * @return 楠岃瘉鐮� + */ + public Boolean getVerifyCodeNew(String phone) { + // 鐢熸垚4浣嶉殢鏈烘暟 + String code = ""; + Random ran = new Random(); + int randomNum = ran.nextInt(10000); + code = String.format("%04d", randomNum); + log.info("鎵嬫満鍙凤細"+phone+"->楠岃瘉鐮侊細"+code); + boolean send = sendYp(phone, code); + if (send){ + redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); + return true; + } + redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); + return false; + } /** * 鑾峰彇楠岃瘉鐮� @@ -128,14 +147,33 @@ if (user==null){ throw new BaseException("鎮ㄦ墜鏈哄彿灏氭湭娉ㄥ唽锛�"); } + return getVerifyCodeNew(phone); + } - // 鐢熸垚4浣嶉殢鏈烘暟 - String code = ""; - Random ran = new Random(); - int randomNum = ran.nextInt(10000); - code = String.format("%04d", randomNum); - log.info("鎵嬫満鍙凤細"+phone+"->楠岃瘉鐮侊細"+code); - Boolean flag = true; + /** + * 浜戠墖楠岃瘉鐮� + * @param phone 鎵嬫満 + * @param code 楠岃瘉鐮� + * @return 缁撴灉 + */ + private boolean sendYp(String phone, String code) + { + String result = smsApi.sendSms(phone, StringUtils.format(YPSmsApi.CODE_TMP, code, Constants.PHONE_EXPIRATION)); + if (result.contains("\"code\":0,\"msg\":\"OK\"")){ + log.info("鍙戦�佹垚鍔� ->楠岃瘉鐮侊細"+code); + return true; + } + return false; + } + + /** + * 闃块噷楠岃瘉鐮� + * @param phone 鎵嬫満 + * @param code 楠岃瘉鐮� + * @return 缁撴灉 + */ + private boolean sendAl(String phone, String code ) + { DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", AliyunSmsConstants.SMS_APPID, AliyunSmsConstants.SMS_SECRET); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); @@ -152,15 +190,12 @@ JSONObject jsonObject = JSON.parseObject(response.getData()); if ("OK".equals(jsonObject.get("Code"))) { log.info("鍙戦�佹垚鍔� ->楠岃瘉鐮侊細"+code); - redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); return true; } - } catch (ClientException e) { e.printStackTrace(); } - redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); - return flag; + return false; } /** @@ -171,13 +206,26 @@ public String phoneLogin(UserPhoneLoginBo bo) { String phone = bo.getPhone(); - Boolean verified = verifyPhone(phone, bo.getCode()); - if (!verified){ - throw new BaseException("鎵嬫満鍙烽獙璇佺爜鏍¢獙澶辫触锛�"); +// Boolean verified = verifyPhone(phone, bo.getCode()); +// if (!verified){ +// throw new BaseException("鎵嬫満鍙烽獙璇佺爜鏍¢獙澶辫触锛�"); +// } + SysUser user = null; + if ("01".equals(bo.getUserType())){ + user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() + .eq(SysUser::getPhonenumber,bo.getPhone()) + .and(wrapper->wrapper.eq(SysUser::getUserType,"00").or().eq(SysUser::getUserType, "01"))); + } else { + user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() + .eq(SysUser::getPhonenumber,bo.getPhone()) + .eq(SysUser::getUserType,bo.getUserType()) + ); } - SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber,bo.getPhone())); if (user==null){ - throw new BaseException("鎮ㄦ墜鏈哄彿灏氭湭娉ㄥ唽锛�"); + throw new BaseException("鎮ㄦ墜鏈哄彿灏氭湭娉ㄥ唽鎴栨偍閫夋嫨鐧诲綍绫诲瀷鏈夎锛�"); + } + if (!"0".equals(user.getStatus())){ + throw new BaseException("鎮ㄨ处鍙峰凡鍋滅敤鎴栧緟瀹℃壒锛岃鑱旂郴钀ュ晢鍔炵鐞嗕汉鍛橈紒"); } return this.login(user.getUserName(), user.getRecommendUser(), null, null); } @@ -237,7 +285,7 @@ * @param code 楠岃瘉鐮� * @return 鏍¢獙缁撴灉 */ - private Boolean verifyPhone(String phone, String code) + public Boolean verifyPhone(String phone, String code) { String key = getCacheKey(phone); if (!redisCache.hasKey(key)) { @@ -252,4 +300,6 @@ return verify; } + + } -- Gitblit v1.9.1