用户部分信息 加密处理

This commit is contained in:
2022-01-24 16:35:11 +08:00
parent d43b77b3c8
commit b40371a84f
2 changed files with 77 additions and 15 deletions
@@ -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);
}
/**
* 解密密文字符串
*
@@ -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<SysUser> add(@RequestBody JSONObject jsonObject) {
Result<SysUser> result = new Result<SysUser>();
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<SysUser> 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<SysUser> result = new Result<SysUser>();
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<SysUser>().eq(SysUser::getUsername, username));
if(user==null) {
return Result.error("用户不存在!");