前端數(shù)據(jù)安全解決方案

目標(biāo)

1:禁止網(wǎng)站分析。
2:保證Cookies安全。
3:http請(qǐng)求數(shù)據(jù)安全。
4:http請(qǐng)求防重。

方案

1:禁止網(wǎng)站分析

  • 禁止右鍵審查元素和開(kāi)啟F12
使用disable-devtool庫(kù):https://github.com/theajack/disable-devtool
  • 如果開(kāi)啟了F12,那么出發(fā)debug干擾審查元素
 // 循環(huán)監(jiān)測(cè),當(dāng)檢測(cè)到開(kāi)啟了Dev那么開(kāi)啟deg模式
    setInterval(function () {
        check();
    }, 4000);
    var check = function () {
        function doCheck(a) {
            if (("" + a / a)["length"] !== 1 || a % 20 === 0) {
                (function () { }["constructor"]("debugger")());
            } else {
                (function () { }["constructor"]("debugger")());
            }
            doCheck(++a);
        }
        try {
            doCheck(0);
        } catch (err) { }
    };
    check();

2:保證Cookies安全

  • 使用AES將保存的內(nèi)容加密:基于crypto-js
import CryptoJS from "crypto-js";

const keyCode = "223434ae2f1123a234cb"

export default {
    en(word, keyStr = keyCode) {
        let enc = CryptoJS.AES.encrypt(word, CryptoJS.enc.Hex.parse(keyStr), {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return enc.ciphertext.toString()
    },

    de(word, keyStr = keyCode) {
        let dec = CryptoJS.AES.decrypt(CryptoJS.format.Hex.parse(word), CryptoJS.enc.Hex.parse(keyStr), {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return CryptoJS.enc.Utf8.stringify(dec)
    }
};

3:http請(qǐng)求數(shù)據(jù)安全。

  • 將請(qǐng)求體加密,密鑰要和服務(wù)器端匹配
import CryptoJS from "crypto-js";

const keyCode = "223434ae2f1123a234cb"

export default {
    en(word, keyStr = keyCode) {
        let enc = CryptoJS.AES.encrypt(word, CryptoJS.enc.Hex.parse(keyStr), {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return enc.ciphertext.toString()
    },

    de(word, keyStr = keyCode) {
        let dec = CryptoJS.AES.decrypt(CryptoJS.format.Hex.parse(word), CryptoJS.enc.Hex.parse(keyStr), {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        })
        return CryptoJS.enc.Utf8.stringify(dec)
    }
};
  • 在攔截器中統(tǒng)一加密
// request interceptor
service.interceptors.request.use(
  config => {
    config.data = encry.en(JSON.stringify(config.data))
    return config
  },
  error => {
    Promise.reject(error)
  }
)
  • java后臺(tái)對(duì)應(yīng)加密
package com.cloudcc.microservice.devconsolesvc.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@Slf4j
public class AESUtil {


    /**
     * 加密算法:AES
     */
    private static String Algorithm = "AES";
    /**
     * 算法/模式/補(bǔ)碼方式
     */
    private static String AlgorithmProvider = "AES/ECB/PKCS5Padding";

    /**
     * 默認(rèn)密鑰:
     */
    private static String defaultKey = "devconsole-svc12";

    /**
     * @param src 明文
     * @param key 密鑰
     * @return
     * @version 1.0
     * @description 加密
     * @date 2021/7/26 19:52
     */
    public static String encrypt(String src, String key) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        if (StringUtils.isEmpty(key)) {
            key = defaultKey;
        }
        SecretKey secretKey = new SecretKeySpec(key.getBytes("utf-8"), Algorithm);
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherBytes = cipher.doFinal(src.getBytes(Charset.forName("utf-8")));
        return byteToHexString(cipherBytes);
    }

    /**
     * @param src 密文
     * @param key 密鑰
     * @return
     * @version 1.0
     * @description 解密
     * @date 2021/7/26 19:57
     */
    public static String decrypt(String src, String key) throws Exception {
        if (StringUtils.isEmpty(key)) {
            key = defaultKey;
        }
        SecretKey secretKey = new SecretKeySpec(key.getBytes("utf-8"), Algorithm);
        Cipher cipher = Cipher.getInstance(AlgorithmProvider);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] hexBytes = hexStringToBytes(src);
        byte[] plainBytes = cipher.doFinal(hexBytes);
        return new String(plainBytes, "utf-8");
    }

    /**
     * 將byte轉(zhuǎn)換為16進(jìn)制字符串
     *
     * @param src
     * @return
     */
    public static String byteToHexString(byte[] src) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xff;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                sb.append("0");
            }
            sb.append(hv);
        }
        return sb.toString();
    }

    /**
     * 將16進(jìn)制字符串裝換為byte數(shù)組
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] b = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            b[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return b;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static void main(String[] args) {
        try {
            // java中密鑰必須是16的倍數(shù)
            String key = "comconsbck-moc21";
            String src = "{\n" +
                    "    \"head\":{},\n" +
                    "    \"body\":{\n" +
                    "  \n" +
                    "        \"pageApi\":\"test-a\"\n" +
                    "    }\n" +
                    "}";
            System.out.println("vue中的密鑰:" + byteToHexString(key.getBytes()));
            System.out.println("原字符串:" + src);

            String enc = encrypt(src, key);
            System.out.println("加密:" + enc);

            System.out.println("解密:" + decrypt("de1132750d63a572333933c9b5ed545f", key));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4:http請(qǐng)求防重。

  • 請(qǐng)求體中添加時(shí)間戳,服務(wù)器端獲取和服務(wù)器事件對(duì)比,時(shí)間差小于5分鐘,為有效請(qǐng)求。
  • 請(qǐng)求體中添加隨機(jī)字符串,服務(wù)器將隨機(jī)字符串放入Map中,如果請(qǐng)求的隨機(jī)串存在Map中,那么視為無(wú)效請(qǐng)求。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 行業(yè)背景分析 行業(yè)從事和業(yè)務(wù)范圍xx公司和機(jī)關(guān)單位從事xx行業(yè),主要業(yè)務(wù)是制造或提供提供xx產(chǎn)品和服務(wù),曾獲得xx...
    JJJoeee閱讀 1,256評(píng)論 0 1
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂(lè)有人憂愁,有人驚喜有人失落,有的覺(jué)得收獲滿滿有...
    陌忘宇閱讀 8,860評(píng)論 28 54
  • 步驟:發(fā)微博01-導(dǎo)航欄內(nèi)容 -> 發(fā)微博02-自定義TextView -> 發(fā)微博03-完善TextView和...
    dibadalu閱讀 3,423評(píng)論 1 3
  • 人工智能是什么?什么是人工智能?人工智能是未來(lái)發(fā)展的必然趨勢(shì)嗎?以后人工智能技術(shù)真的能達(dá)到電影里機(jī)器人的智能水平嗎...
    ZLLZ閱讀 4,104評(píng)論 0 5
  • 上周六在壓力下實(shí)在需要釋放,去西塘躲了兩天,真是好地方,很優(yōu)雅的江南古鎮(zhèn),而且與周莊比開(kāi)發(fā)不算過(guò)度。 我們是周五半...
    聚塔閱讀 888評(píng)論 2 2

友情鏈接更多精彩內(nèi)容