mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-21 22:53:56 +08:00
update:SM2非对称加密的优化
This commit is contained in:
+6
-6
@@ -69,11 +69,6 @@ public class LoginController {
|
|||||||
@PostMapping("/smsVerification")
|
@PostMapping("/smsVerification")
|
||||||
@Operation(summary = "短信验证码")
|
@Operation(summary = "短信验证码")
|
||||||
public Result<Void> smsVerification(@RequestBody SmsVerificationDTO dto) {
|
public Result<Void> smsVerification(@RequestBody SmsVerificationDTO dto) {
|
||||||
// 验证图形验证码
|
|
||||||
boolean validate = captchaService.validate(dto.getCaptchaId(), dto.getCaptcha(), true);
|
|
||||||
if (!validate) {
|
|
||||||
throw new RenException(ErrorCode.SMS_CAPTCHA_ERROR);
|
|
||||||
}
|
|
||||||
Boolean isMobileRegister = sysParamsService
|
Boolean isMobileRegister = sysParamsService
|
||||||
.getValueObject(Constant.SysMSMParam.SERVER_ENABLE_MOBILE_REGISTER.getValue(), Boolean.class);
|
.getValueObject(Constant.SysMSMParam.SERVER_ENABLE_MOBILE_REGISTER.getValue(), Boolean.class);
|
||||||
if (!isMobileRegister) {
|
if (!isMobileRegister) {
|
||||||
@@ -182,13 +177,18 @@ public class LoginController {
|
|||||||
// 是否开启手机注册
|
// 是否开启手机注册
|
||||||
Boolean isMobileRegister = sysParamsService
|
Boolean isMobileRegister = sysParamsService
|
||||||
.getValueObject(Constant.SysMSMParam.SERVER_ENABLE_MOBILE_REGISTER.getValue(), Boolean.class);
|
.getValueObject(Constant.SysMSMParam.SERVER_ENABLE_MOBILE_REGISTER.getValue(), Boolean.class);
|
||||||
|
boolean validate;
|
||||||
if (isMobileRegister) {
|
if (isMobileRegister) {
|
||||||
// 验证用户是否是手机号码
|
// 验证用户是否是手机号码
|
||||||
boolean validPhone = ValidatorUtils.isValidPhone(login.getUsername());
|
boolean validPhone = ValidatorUtils.isValidPhone(login.getUsername());
|
||||||
if (!validPhone) {
|
if (!validPhone) {
|
||||||
throw new RenException(ErrorCode.USERNAME_NOT_PHONE);
|
throw new RenException(ErrorCode.USERNAME_NOT_PHONE);
|
||||||
}
|
}
|
||||||
|
// 验证短信验证码是否正常
|
||||||
|
validate = captchaService.validateSMSValidateCode(login.getUsername(), login.getMobileCaptcha(), false);
|
||||||
|
if (!validate) {
|
||||||
|
throw new RenException(ErrorCode.SMS_CODE_ERROR);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 按照用户名获取用户
|
// 按照用户名获取用户
|
||||||
|
|||||||
@@ -21,10 +21,6 @@ public class LoginDTO implements Serializable {
|
|||||||
@NotBlank(message = "{sysuser.password.require}")
|
@NotBlank(message = "{sysuser.password.require}")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
@Schema(description = "验证码")
|
|
||||||
@NotBlank(message = "{sysuser.captcha.require}")
|
|
||||||
private String captcha;
|
|
||||||
|
|
||||||
@Schema(description = "手机验证码")
|
@Schema(description = "手机验证码")
|
||||||
private String mobileCaptcha;
|
private String mobileCaptcha;
|
||||||
|
|
||||||
|
|||||||
@@ -256,19 +256,3 @@ export function sm2Decrypt(privateKey, cipherText) {
|
|||||||
return sm2.doDecrypt(dataWithoutPrefix, privateKey, 1);
|
return sm2.doDecrypt(dataWithoutPrefix, privateKey, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断字符串是否为Base64编码
|
|
||||||
* @param {string} str 待判断的字符串
|
|
||||||
* @returns {boolean} 是否为Base64编码
|
|
||||||
*/
|
|
||||||
export function isBase64(str) {
|
|
||||||
if (typeof str !== 'string' || str.trim() === '') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return btoa(atob(str)) === str;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -148,7 +148,7 @@
|
|||||||
import Api from "@/apis/api";
|
import Api from "@/apis/api";
|
||||||
import VersionFooter from "@/components/VersionFooter.vue";
|
import VersionFooter from "@/components/VersionFooter.vue";
|
||||||
import i18n, { changeLanguage } from "@/i18n";
|
import i18n, { changeLanguage } from "@/i18n";
|
||||||
import { getUUID, goToPage, showDanger, showSuccess, validateMobile, sm2Encrypt, isBase64 } from "@/utils";
|
import { getUUID, goToPage, showDanger, showSuccess, validateMobile, sm2Encrypt } from "@/utils";
|
||||||
import { mapState } from "vuex";
|
import { mapState } from "vuex";
|
||||||
import Constant from "@/utils/constant";
|
import Constant from "@/utils/constant";
|
||||||
|
|
||||||
@@ -212,32 +212,25 @@ export default {
|
|||||||
methods: {
|
methods: {
|
||||||
// 获取服务器公钥
|
// 获取服务器公钥
|
||||||
getServerPublicKey() {
|
getServerPublicKey() {
|
||||||
console.log('开始获取服务器公钥...');
|
|
||||||
// 先从本地存储获取
|
// 先从本地存储获取
|
||||||
const storedPublicKey = localStorage.getItem(Constant.STORAGE_KEY.PUBLIC_KEY);
|
const storedPublicKey = localStorage.getItem(Constant.STORAGE_KEY.PUBLIC_KEY);
|
||||||
if (storedPublicKey) {
|
if (storedPublicKey) {
|
||||||
console.log('从本地存储获取到公钥,长度:', storedPublicKey.length);
|
|
||||||
this.serverPublicKey = storedPublicKey;
|
this.serverPublicKey = storedPublicKey;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('本地存储无公钥,从服务器获取...');
|
|
||||||
// 从公共配置接口获取公钥
|
// 从公共配置接口获取公钥
|
||||||
Api.user.getPubConfig(
|
Api.user.getPubConfig(
|
||||||
(res) => {
|
(res) => {
|
||||||
if (res.data && res.data.data && res.data.data.sm2PublicKey) {
|
if (res.data && res.data.data && res.data.data.sm2PublicKey) {
|
||||||
console.log('获取到服务器公钥,长度:', res.data.data.sm2PublicKey.length);
|
|
||||||
this.serverPublicKey = res.data.data.sm2PublicKey;
|
this.serverPublicKey = res.data.data.sm2PublicKey;
|
||||||
// 存储到本地
|
// 存储到本地
|
||||||
localStorage.setItem(Constant.STORAGE_KEY.PUBLIC_KEY, this.serverPublicKey);
|
localStorage.setItem(Constant.STORAGE_KEY.PUBLIC_KEY, this.serverPublicKey);
|
||||||
console.log('公钥已存储到本地');
|
|
||||||
} else {
|
} else {
|
||||||
console.error('服务器返回数据格式异常:', res);
|
|
||||||
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(err) => {
|
(err) => {
|
||||||
console.error("获取公共配置失败:", err);
|
|
||||||
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -121,7 +121,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import Api from '@/apis/api';
|
import Api from '@/apis/api';
|
||||||
import VersionFooter from '@/components/VersionFooter.vue';
|
import VersionFooter from '@/components/VersionFooter.vue';
|
||||||
import { getUUID, goToPage, showDanger, showSuccess, validateMobile, sm2Encrypt, isBase64 } from '@/utils';
|
import { getUUID, goToPage, showDanger, showSuccess, validateMobile, sm2Encrypt } from '@/utils';
|
||||||
import { mapState } from 'vuex';
|
import { mapState } from 'vuex';
|
||||||
import Constant from '@/utils/constant';
|
import Constant from '@/utils/constant';
|
||||||
|
|
||||||
@@ -177,32 +177,25 @@ export default {
|
|||||||
methods: {
|
methods: {
|
||||||
// 获取服务器公钥
|
// 获取服务器公钥
|
||||||
getServerPublicKey() {
|
getServerPublicKey() {
|
||||||
console.log('开始获取服务器公钥...');
|
|
||||||
// 先从本地存储获取
|
// 先从本地存储获取
|
||||||
const storedPublicKey = localStorage.getItem(Constant.STORAGE_KEY.PUBLIC_KEY);
|
const storedPublicKey = localStorage.getItem(Constant.STORAGE_KEY.PUBLIC_KEY);
|
||||||
if (storedPublicKey) {
|
if (storedPublicKey) {
|
||||||
console.log('从本地存储获取到公钥,长度:', storedPublicKey.length);
|
|
||||||
this.serverPublicKey = storedPublicKey;
|
this.serverPublicKey = storedPublicKey;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('本地存储无公钥,从服务器获取...');
|
|
||||||
// 从公共配置接口获取公钥
|
// 从公共配置接口获取公钥
|
||||||
Api.user.getPubConfig(
|
Api.user.getPubConfig(
|
||||||
(res) => {
|
(res) => {
|
||||||
if (res.data && res.data.data && res.data.data.sm2PublicKey) {
|
if (res.data && res.data.data && res.data.data.sm2PublicKey) {
|
||||||
console.log('获取到服务器公钥,长度:', res.data.data.sm2PublicKey.length);
|
|
||||||
this.serverPublicKey = res.data.data.sm2PublicKey;
|
this.serverPublicKey = res.data.data.sm2PublicKey;
|
||||||
// 存储到本地
|
// 存储到本地
|
||||||
localStorage.setItem(Constant.STORAGE_KEY.PUBLIC_KEY, this.serverPublicKey);
|
localStorage.setItem(Constant.STORAGE_KEY.PUBLIC_KEY, this.serverPublicKey);
|
||||||
console.log('公钥已存储到本地');
|
|
||||||
} else {
|
} else {
|
||||||
console.error('服务器返回数据格式异常:', res);
|
|
||||||
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(err) => {
|
(err) => {
|
||||||
console.error("获取公共配置失败:", err);
|
|
||||||
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
showDanger(this.$t('sm2.failedToGetPublicKey'));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user