From 3410ac9ea868b95651415518632a2b3ff38eef27 Mon Sep 17 00:00:00 2001 From: Administrator <513693417@qq.com> Date: Fri, 18 Sep 2020 17:31:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/ajax/AjaxLoginController.java | 11 ++ .../controller/BizMemberController.java | 21 +++- .../com/ruoyi/business/utils/Encrypt.java | 110 ++++++++++++++++++ .../mapper/business/BizMemberMapper.xml | 2 +- .../templates/business/member/member.html | 42 ++++--- 5 files changed, 168 insertions(+), 18 deletions(-) create mode 100644 ruoyi-business/src/main/java/com/ruoyi/business/utils/Encrypt.java diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/ajax/AjaxLoginController.java b/ruoyi-business/src/main/java/com/ruoyi/business/ajax/AjaxLoginController.java index fac038871..f3a1211c0 100644 --- a/ruoyi-business/src/main/java/com/ruoyi/business/ajax/AjaxLoginController.java +++ b/ruoyi-business/src/main/java/com/ruoyi/business/ajax/AjaxLoginController.java @@ -3,12 +3,23 @@ package com.ruoyi.business.ajax; import com.ruoyi.business.service.IBizMemberService; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.utils.security.Md5Utils; import org.apache.ibatis.annotations.Param; +import org.apache.shiro.crypto.hash.Md5Hash; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import sun.security.provider.MD5; import javax.annotation.Resource; +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.IvParameterSpec; +import java.security.NoSuchAlgorithmException; +import java.util.Base64; /** * 前端用户登录 diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/controller/BizMemberController.java b/ruoyi-business/src/main/java/com/ruoyi/business/controller/BizMemberController.java index c6b7318de..11e7eafc2 100644 --- a/ruoyi-business/src/main/java/com/ruoyi/business/controller/BizMemberController.java +++ b/ruoyi-business/src/main/java/com/ruoyi/business/controller/BizMemberController.java @@ -1,7 +1,10 @@ package com.ruoyi.business.controller; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import com.ruoyi.business.utils.Encrypt; import com.ruoyi.common.utils.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; @@ -114,6 +117,21 @@ public class BizMemberController extends BaseController return toAjax(bizMemberService.updateBizMemberAndDou(bizMember)); } + /** + * 查看会员密码 + */ + @RequiresPermissions("business:member:edit") + @Log(title = "会员密码", businessType = BusinessType.UPDATE) + @PostMapping("/showPassword") + @ResponseBody + public AjaxResult showPassword(Long memberID) + { + BizMember bizMember = bizMemberService.selectBizMemberSimple(memberID); + Map resultMap = new HashMap(); + resultMap.put("pwd", Encrypt.decrypt(bizMember.getPassword())); + return AjaxResult.success(resultMap); + } + /** * 修改会员密码 */ @@ -125,7 +143,8 @@ public class BizMemberController extends BaseController { BizMember bizMember = bizMemberService.selectBizMemberSimple(memberID); if(bizMember == null || StringUtils.isEmpty(password)) return toAjax(0); - bizMember.setPassword(password); + //加密 + bizMember.setPassword(Encrypt.encrypt(password)); return toAjax(bizMemberService.updateBizMember(bizMember)); } diff --git a/ruoyi-business/src/main/java/com/ruoyi/business/utils/Encrypt.java b/ruoyi-business/src/main/java/com/ruoyi/business/utils/Encrypt.java new file mode 100644 index 000000000..29147d5ff --- /dev/null +++ b/ruoyi-business/src/main/java/com/ruoyi/business/utils/Encrypt.java @@ -0,0 +1,110 @@ +package com.ruoyi.business.utils; + +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.security.Md5Utils; + +import javax.crypto.*; +import javax.crypto.spec.DESKeySpec; +import javax.crypto.spec.IvParameterSpec; +import java.io.UnsupportedEncodingException; + +/* DES加密工具 */ +public class Encrypt { + //秘钥 + private static final String KEY = "ZF_desencrypt_2020"; + + //加密解密工具 + private static Cipher enCipher = null; + private static Cipher deCipher = null; + + //初始化加密工具 + static { + try { + int keyLen = 8; + String key8 = Md5Utils.hash(KEY).substring(0, keyLen).toUpperCase(); + //C# MD5 ResultString Fix + StringBuffer keys = new StringBuffer(); + for (int i = 0; i < keyLen; i += 2) { + keys.append(key8.substring(i, i + 2)).append("-"); + } + byte[] KeyStr = keys.substring(0, keyLen).getBytes("utf-8"); + DESKeySpec keySpec = new DESKeySpec(KeyStr);// 设置密钥参数 + IvParameterSpec iv = new IvParameterSpec(KeyStr);// 设置向量 + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂 + SecretKey key = keyFactory.generateSecret(keySpec); + enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量 + deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); + deCipher.init(Cipher.DECRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量 + } catch (Exception e) { + e.printStackTrace(); + } + } + + //加密 + public static String encrypt(String text) + { + if (StringUtils.isEmpty(text)) return null; + + byte[] pasByte = null; + try { + pasByte = enCipher.doFinal(text.getBytes("utf-8")); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + return new String(byte2hex(pasByte)); + } + + //解密 + public static String decrypt(String crypt) + { + if (StringUtils.isEmpty(crypt)) return null; + byte[] pasByte = null; + try { + pasByte = deCipher.doFinal(hex2byte(crypt.getBytes("utf-8"))); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + return new String(pasByte); + } + + public static void main(String args[]) { + //System.out.println(encrypt("x123456")); + System.out.println(decrypt("248E135E28C103B4")); + } + + private static final String HEX_CHAR = "0123456789ABCDEF"; + private static final byte[] HEX_STRING_BYTE = HEX_CHAR.getBytes(); + + //10进制转16进制 + private static byte[] byte2hex(byte[] b) { + int length = b.length; + byte[] b2 = new byte[length << 1]; + int pos; + for(int i=0; i> 4]; + b2[pos+1] = HEX_STRING_BYTE[b[i] & 0x0f]; + } + return b2; + } + + //16进制转10进制 + public static byte[] hex2byte(byte[] b) { + if(b.length%2 != 0) { + throw new IllegalArgumentException("byte array length is not even!"); + } + + int length = b.length >> 1; + byte[] b2 = new byte[length]; + int pos; + for(int i=0; i diff --git a/ruoyi-business/src/main/resources/templates/business/member/member.html b/ruoyi-business/src/main/resources/templates/business/member/member.html index b9867f224..991e71be3 100644 --- a/ruoyi-business/src/main/resources/templates/business/member/member.html +++ b/ruoyi-business/src/main/resources/templates/business/member/member.html @@ -131,7 +131,7 @@ align: 'center', formatter: function(value, row, index) { var actions = []; - actions.push('修改密码 '); + actions.push('修改密码 '); actions.push('编辑 '); actions.push('删除'); return actions.join(''); @@ -142,23 +142,33 @@ }); //修改密码 - function updatePwd(memberID, oldPwd){ - $.modal.confirm("
请输入新的密码
", function() { - let pwd = $("#newPwd").val(); - if(pwd.length == 0){ - $.modal.msgError("请输入新密码"); - return false; + function updatePwd(memberID){ + $.post(prefix + "/showPassword", { + memberID:memberID + }, function(response){ + if(response.code == 0){ + showPwd(response.data.pwd) } - $.operate.post(prefix + "/editPassword", { - memberID:memberID, - password:pwd - }, function(response){ - if(response.code == 0){ - $.modal.alertSuccess('操作成功!') - } - }); }); - $("#anch").parent().css("padding", "10px 20px").children(".layui-layer-ico").hide(); + + let showPwd = function(oldPwd){ + $.modal.confirm("
请输入新的密码
", function() { + let pwd = $("#newPwd").val(); + if(pwd.length == 0){ + $.modal.msgError("请输入新密码"); + return false; + } + $.operate.post(prefix + "/editPassword", { + memberID:memberID, + password:pwd + }, function(response){ + if(response.code == 0){ + $.modal.alertSuccess('操作成功!') + } + }); + }); + $("#anch").parent().css("padding", "10px 20px").children(".layui-layer-ico").hide(); + } }