RSA前端加密(vue項目)

1,安裝插件

npm install jsencrypt --save

2,安裝成功之後,在main.js文件引入

import JSEncrypt from 'jsencrypt';//引入模塊
Vue.prototype.$jsEncrypt = JSEncrypt;//配置全局變量

3,在需要使用的組件(本文以登錄頁面對賬號和密碼進行了加密)

 getPublicKey({}).then((res)=>{//請求公鑰接口
                            const jse = new this.$jsEncrypt()
                            jse.setPublicKey(res)//配置公鑰
                            var aesuncrypt_Acount = jse.encrypt(this.ruleForm.username)//加密賬號
                            var aesuncrypt_pwd = jse.encrypt(this.ruleForm.password)//加密密碼
                            userlogin({//登錄接口
                                account:aesuncrypt_Acount,
                                pwd:aesuncrypt_pwd,
                            }).then((res)=>{
                                if(res.success){
                             //登錄成功之後的操作
                                    this.$router.push('/');
                                }else{
                                    //登錄失敗的操作
........
                                }
                            })
                        })

對步驟3的說明,獲取公鑰是需要後端開發人員提供api接口的

------------END---------

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章