From b40371a84f409897a7a42e88029e983468b72aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=85?= <1669139609@qq.com> Date: Mon, 24 Jan 2022 16:35:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E9=83=A8=E5=88=86=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20=E5=8A=A0=E5=AF=86=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jero/common/util/PasswordUtil.java | 14 ++++ .../system/controller/SysUserController.java | 78 +++++++++++++++---- 2 files changed, 77 insertions(+), 15 deletions(-) diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/PasswordUtil.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/PasswordUtil.java index eb1886b1..6ca4b702 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/PasswordUtil.java +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/util/PasswordUtil.java @@ -96,6 +96,20 @@ public class PasswordUtil { return bytesToHexString(encipheredData); } + /** + * 自定义加密明文字符串 + * + * @param plaintext + * 待加密的明文字符串 + * @return 加密后的密文字符串 + * @throws Exception + */ + public static String encrypt(String plaintext) { + + return encrypt(plaintext, ALGORITHM, Salt); + } + + /** * 解密密文字符串 * diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysUserController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysUserController.java index 8a6c035c..600a6a8c 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysUserController.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/SysUserController.java @@ -10,6 +10,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.jero.common.exception.JeroBootException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.shiro.SecurityUtils; @@ -137,20 +138,42 @@ public class SysUserController { @RequestMapping(value = "/add", method = RequestMethod.POST) public Result add(@RequestBody JSONObject jsonObject) { Result result = new Result(); - String selectedRoles = jsonObject.getString("selectedroles"); - String selectedDeparts = jsonObject.getString("selecteddeparts"); - try { - SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); - user.setCreateTime(new Date());//设置创建时间 - String salt = oConvertUtils.randomGen(8); - user.setSalt(salt); - String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt); - user.setPassword(passwordEncode); - user.setStatus(1); - user.setDelFlag(CommonConstant.DEL_FLAG_0); - sysUserService.addUserWithRole(user, selectedRoles); + try { + String selectedRoles = jsonObject.getString("selectedroles"); + String selectedDeparts = jsonObject.getString("selecteddeparts"); + String rsaPublicKey = jsonObject.getString("rsaPublicKey"); + String rsaPrivateKey = String.valueOf(redisUtil.get(rsaPublicKey)); + SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); + String email; + String phone; + try { + //1:先用私钥解密数据 + email = CommonUtils.decryptBtRsaPriKey(user.getEmail(), rsaPrivateKey); + phone = CommonUtils.decryptBtRsaPriKey(user.getPhone(), rsaPrivateKey); + } catch (Exception e) { + throw new JeroBootException("解密失败!", e); + } + //3:把数据加密在放回来 + email = PasswordUtil.encrypt(email); + phone = PasswordUtil.encrypt(phone); + user.setEmail(email); + user.setPhone(phone); + String password; + try { + password = CommonUtils.decryptBtRsaPriKey(user.getPassword(), rsaPrivateKey); + } catch (Exception e) { + throw new JeroBootException("解密失败!", e); + } + user.setCreateTime(new Date());//设置创建时间 + String salt = oConvertUtils.randomGen(8); + user.setSalt(salt); + String passwordEncode = PasswordUtil.encrypt(user.getUsername(), password, salt); + user.setPassword(passwordEncode); + user.setStatus(1); + user.setDelFlag(CommonConstant.DEL_FLAG_0); + sysUserService.addUserWithRole(user, selectedRoles); sysUserService.addUserWithDepart(user, selectedDeparts); - result.success("添加成功!"); + result.success("添加成功!"); } catch (Exception e) { log.error(e.getMessage(), e); result.error500("操作失败"); @@ -162,6 +185,10 @@ public class SysUserController { @RequiresPermissions("user:edit") @RequestMapping(value = "/edit", method = RequestMethod.PUT) public Result edit(@RequestBody JSONObject jsonObject) { + String rsaPublicKey = jsonObject.getString("rsaPublicKey"); + String roles = jsonObject.getString("selectedroles"); + String departs = jsonObject.getString("selecteddeparts"); + String rsaPrivateKey = String.valueOf(redisUtil.get(rsaPublicKey)); Result result = new Result(); try { SysUser sysUser = sysUserService.getById(jsonObject.getString("id")); @@ -170,11 +197,23 @@ public class SysUserController { result.error500("未找到对应实体"); }else { SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class); + String email; + String phone; + try { + //1:先用私钥解密数据 + email = CommonUtils.decryptBtRsaPriKey(user.getEmail(), rsaPrivateKey); + phone = CommonUtils.decryptBtRsaPriKey(user.getPhone(), rsaPrivateKey); + } catch (Exception e) { + throw new JeroBootException("解密失败!", e); + } + //3:把数据加密在放回来 + email = PasswordUtil.encrypt(email); + phone = PasswordUtil.encrypt(phone); + user.setEmail(email); + user.setPhone(phone); user.setUpdateTime(new Date()); //String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), sysUser.getSalt()); user.setPassword(sysUser.getPassword()); - String roles = jsonObject.getString("selectedroles"); - String departs = jsonObject.getString("selecteddeparts"); sysUserService.editUserWithRole(user, roles); sysUserService.editUserWithDepart(user, departs); sysUserService.updateNullPhoneEmail(); @@ -542,6 +581,15 @@ public class SysUserController { String oldpassword = json.getString("oldpassword"); String password = json.getString("password"); String confirmpassword = json.getString("confirmpassword"); + String RSAPublicKey = json.getString("rsaPublicKey"); + String RSAPrivateKey = String.valueOf(redisUtil.get(RSAPublicKey)); + try { + oldpassword = CommonUtils.decryptBtRsaPriKey(oldpassword, RSAPrivateKey); + password = CommonUtils.decryptBtRsaPriKey(password, RSAPrivateKey); + confirmpassword = CommonUtils.decryptBtRsaPriKey(confirmpassword, RSAPrivateKey); + }catch (Exception e){ + log.error(e.getMessage(),e); + } SysUser user = this.sysUserService.getOne(new LambdaQueryWrapper().eq(SysUser::getUsername, username)); if(user==null) { return Result.error("用户不存在!");