1.index.html引入 <script src="./static/js/jsencrypt.min.js"></script>css
或者 npm i jsencrypt -S html
第一種引入方式直接用前端
<template>
<div class="rsa_box">
<el-row v-if="!baseUrl">
<el-button @click="testRsa">測試加密按鈕</el-button>
</el-row>
<iframe :src="baseUrl" frameborder="0" width="100%" :height="100px"></iframe>
</div>
</template>
<script> import { rsaTest } from '@/http/api' export default { data() { return { baseUrl: '' } }, methods: { testRsa() { var encrypt = new JSEncrypt(); rsaTest().then(res => { encrypt.setPublicKey(res.data.publicKey); encrypt.setPrivateKey(res.data.privateKey); let userName = encrypt.encrypt(res.data.userName); let userInfo = encrypt.encrypt(res.data.userInfo) let code = res.data.code; let href = res.data.startUrl + "?userName=" + userName + "&userInfo=" + userInfo + "&code=" + code " href = href.replace(/\+/g, '%2B')//方法一:統一換成轉義字符 後臺在換回來 this.baseUrl = href }) } } } </script> <style lang="scss" scoped> </style>
若是是npm 的在main.jsnpm
import JsEncrypt from 'jsencrypt'
Vue.prototype.$jsEncrypt = JsEncrypt
注意(url和token的問題):就是得出來的 String 是帶 '+' 號的,可是這個加號在傳給後臺的時候實際是空格,前端生成的 token 是帶 + 號生成的,但後臺在生成 token 的時候 空格 ,致使兩邊的 token 對應不上api
解決方案:在前端把串裏的加換成空格 ,再去生成 token.replace(/\+/g,' '); 測試