Bladeren bron

添加RSA非对称加密

bihuisong 1 jaar geleden
bovenliggende
commit
e2c201c97f
3 gewijzigde bestanden met toevoegingen van 36 en 8 verwijderingen
  1. 11 0
      sooka-ui/src/api/login.js
  2. 7 0
      sooka-ui/src/api/tool/encrypt.js
  3. 18 8
      sooka-ui/src/views/login.vue

+ 11 - 0
sooka-ui/src/api/login.js

@@ -60,3 +60,14 @@ export function getCodeImg() {
     timeout: 20000
   })
 }
+
+// 获取密码加密传输公钥
+export function getSecretKey() {
+  return request({
+    url: '/auth/getSecretKey',
+    headers: {
+      isToken: false
+    },
+    method: 'post'
+  })
+}

+ 7 - 0
sooka-ui/src/api/tool/encrypt.js

@@ -0,0 +1,7 @@
+import { JSEncrypt } from 'jsencrypt'
+
+export function encryptedData(publicKey,password){
+  const encryptor = new JSEncrypt()
+  encryptor.setPublicKey(publicKey)
+  return encryptor.encrypt(password+'')
+}

+ 18 - 8
sooka-ui/src/views/login.vue

@@ -58,9 +58,10 @@
 </template>
 
 <script>
-import {getCodeImg} from "@/api/login";
+import {getCodeImg, getSecretKey} from "@/api/login";
 import Cookies from "js-cookie";
 import {decrypt, encrypt} from '@/utils/jsencrypt'
+import {encryptedData} from "@/api/tool/encrypt";
 
 export default {
   name: "Login",
@@ -136,14 +137,23 @@ export default {
             Cookies.remove("password");
             Cookies.remove('rememberMe');
           }
-          this.$store.dispatch("Login", this.loginForm).then(() => {
-            this.$router.push({path: this.redirect || "/"}).catch(() => {
+          //登录前先获取密码加密传输的公钥,对密码进行加密
+          getSecretKey().then((res) => {
+            const encryptedPassword = encryptedData(
+              res.data,
+              this.loginForm.password
+            );
+            this.loginForm.password = encryptedPassword;
+
+            this.$store.dispatch("Login", this.loginForm).then(() => {
+              this.$router.push({path: "/"}).catch(() => {
+              });
+            }).catch(() => {
+              this.loading = false;
+              if (this.captchaEnabled) {
+                this.getCode();
+              }
             });
-          }).catch(() => {
-            this.loading = false;
-            if (this.captchaEnabled) {
-              this.getCode();
-            }
           });
         }
       });