加解密添加

This commit is contained in:
haozq 2025-09-11 16:45:39 +08:00
parent e4effd6b3c
commit 08e61e3318
5 changed files with 26 additions and 12 deletions

View File

@ -17,11 +17,11 @@
<dependencies>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 表示依赖不会传递 -->
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-devtools</artifactId>-->
<!-- <optional>true</optional> &lt;!&ndash; 表示依赖不会传递 &ndash;&gt;-->
<!-- </dependency>-->
<!-- swagger3-->
<dependency>

View File

@ -43,10 +43,9 @@ public class SysLoginController
@PostMapping("/sendPhone")
public AjaxResult sendPhone(@RequestBody String phone) {
public AjaxResult sendPhone(@RequestBody LoginBody loginBody) {
// 下发短信
AjaxResult ajax= loginService.sendPhone(phone);
return ajax;
return loginService.sendPhone(loginBody.getUsername());
}
@ -62,8 +61,6 @@ public class SysLoginController
String token;
AjaxResult ajax = AjaxResult.success();
if("2".equals(loginBody.getLoginType())){
token = loginService.login(loginBody.getUsername(), loginBody.getCode());
}else{
token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),

View File

@ -43,4 +43,6 @@ public class CacheConstants
public static final String PWD_ERR_CNT_KEY = "pwd_err_cnt:";
public static final String PHONE_CODE = "phone_code:";
public static final String PHONE_NUM = "phone_num:";
}

View File

@ -4,10 +4,12 @@ import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.SM4;
import lombok.extern.slf4j.Slf4j;
/**
* @author bonus
*/
@Slf4j
public class Sm4Utils {
/**
* 必须是16字节
@ -29,6 +31,7 @@ public class Sm4Utils {
// 返回带盐的加密结果Hex编码
return HexUtil.encodeHexStr(encryptedData);
} catch (Exception e) {
log.error(e.toString(),e);
return plainText; // 发生异常时返回传入字符串
}
}
@ -47,13 +50,14 @@ public class Sm4Utils {
byte[] decryptedData = sm4.decrypt(cipherText);
return new String(decryptedData);
} catch (Exception e) {
return cipherText; // 发生异常时返回传入字符串
log.error(e.toString(),e);
return null; // 发生异常时返回传入字符串
}
}
// 测试方法演示加密和解密过程
public static void main(String[] args) {
String plainText = "15398187429";
String plainText = "18226653236";
System.out.println("原文: " + plainText);
// 加密明文

View File

@ -123,6 +123,7 @@ public class SysLoginService
* @return
*/
public String login(String phone, String code) {
phone= Sm4Utils.decrypt(phone);
if(StringUtils.isEmpty(phone)){
throw new ServiceException("请输入正确的手机号!");
}
@ -237,6 +238,15 @@ public class SysLoginService
ajax.put("msg", "手机号不存在或手机号不正确");
return ajax;
}
Integer num=redisCache.getCacheObject(CacheConstants.PHONE_NUM+phone);
if(num==null){
num=1;
}else{
num++;
}
if(num>10){
return AjaxResult.error("请勿频繁发送验证码!");
}
StringBuilder sb=new StringBuilder();
String code=getSixBitCode();
sb.append("【博诺思】验证码:").append(code).append(",验证码有效期").append(TIMES).append("分钟,切勿将验证码泄漏于他人。");
@ -246,6 +256,7 @@ public class SysLoginService
if ("200".equals(map.get("code"))){
AjaxResult ajax = AjaxResult.success();
redisCache.setCacheObject(CacheConstants.PHONE_CODE+phone,code,TIMES, TimeUnit.MINUTES);
redisCache.setCacheObject(CacheConstants.PHONE_NUM+phone,num,5, TimeUnit.MINUTES);
ajax.put("times", TIMES);
ajax.put("msg", "发送成功");
return ajax;