vue前端密码加密,springboot后端密码解密
1.模块安装
1 | npm install crypto-js |
2.src–>util–>secret.js
1 | import CryptoJS from ‘crypto-js’ // 默认的 KEY 与 iv 如果没有给 } if (keyStr) { let base64 = CryptoJS.enc.Base64.parse(word); let decrypt = CryptoJS.AES.decrypt(src, key, { let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); |
3.login.vue引入加密方法
1 | import {Encrypt} from ‘../utils/Secret.js’ |
4.login.vue登录请求发起,对密码参数加密
1 | let param={ |
5.后端pom.xml
1 |
|
6.后端utils–>secretUtil.java
1 | package com.unccr.ms.utils; import org.apache.tomcat.util.codec.binary.Base64; import javax.crypto.Cipher; /** private static String IV = “1234567890123456”; /*** /*** /** */ byte[] dataBytes = data.getBytes(); byte[] plaintext = new byte[plaintextLength]; SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), “AES”); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); return new Base64().encodeToString(encrypted); } catch (Exception e) { /** Cipher cipher = Cipher.getInstance(“AES/CBC/NoPadding”); |
7.登录Controller对前端传来的密码解密(先导入加密工具类)
1 | // SecretUtil.desEncrypt: 前端传来的加密密码进行解密 |
8.结果测试
请求中密码已被加密。
后端控制台打印输出加密后的密码(与前端传来一致),解密后与原来的密码相同。