原文:
1、设备指纹技术方案
2、Fingerprintjs2 实现浏览器指纹采集器 获取用户唯一标识码
需求
最主要的需求就是判断本次请求的用户到底是新用户还是老用户还是机器人。通过request去分析ip、ua、mac地址都有各种缺点导致不准确。
Fingerprintjs2 是通过纯前端原生js实现的浏览器指纹采集器,通过获取浏览器中所有能获取到的信息(部分通过base64转成String),最后生成出md5,用于该用户在该设备上的唯一标识码,官方宣称准确度高达99.5%
测试代码
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><title>Fingerprint2 TEST</title><style>body {color: #555;}#info {font-size: 12px;}#control span {color: #333;margin-left: 10px;}</style></head><body><div id="info"><p>Fingerprint2 Github: <a href="https://github.com/Valve/fingerprintjs2" target="_blank">https://github.com/Valve/fingerprintjs2</a></p><p>纯前端实现的浏览器指纹采集器,通过获取浏览器中所有能获取到的信息(部分通过base64转成String),最后生成出md5,用于该用户在该设备上的唯一标识码,官方宣称准确度高达99.5%</p></div><div id="control"><button onclick="start()">开始</button><span>userAgent:</span><input type="checkbox" id="userAgent" checked="checked"><span>fonts:</span><input type="checkbox" id="fonts" checked="checked"><span>fontsFlash:</span><input type="checkbox" id="fontsFlash" checked="checked"><span>canvas:</span><input type="checkbox" id="canvas" checked="checked"><span>webgl:</span><input type="checkbox" id="webgl" checked="checked"><span>audio:</span><input type="checkbox" id="audio" checked="checked"><span>enumerateDevices:</span><input type="checkbox" id="enumerateDevices" checked="checked"></div><div id="view"></div><script src="https://cdn.staticfile.org/fingerprintjs2/2.1.0/fingerprint2.min.js"></script><script>function start() {const start = new Date().getTime();let view = document.querySelector('#view');view.innerHTML = '';let excludes = {};if (!document.querySelector('#userAgent').checked) {excludes.userAgent = true;}if (!document.querySelector('#audio').checked) {excludes.audio = true;}if (!document.querySelector('#enumerateDevices').checked) {excludes.enumerateDevices = true;}if (!document.querySelector('#fonts').checked) {excludes.fonts = true;}if (!document.querySelector('#fontsFlash').checked) {excludes.fontsFlash = true;}if (!document.querySelector('#webgl').checked) {excludes.webgl = true;}if (!document.querySelector('#canvas').checked) {excludes.canvas = true;}let options = {excludes: excludes}Fingerprint2.get(options, function (components) {// 参数const values = components.map(function (component) {return component.value});// 指纹const murmur = Fingerprint2.x64hash128(values.join(''), 31);view.innerHTML += '<p>指纹 : ' + murmur + '</p>';view.innerHTML += '<p>消耗 : ' + (new Date().getTime() - start) + ' 毫秒</p>';view.innerHTML += '<p>使用的参数 : </p>';for (const c of components) {view.innerHTML += '<p>' + c.key + ' : ' + c.value + '</p>';}});}</script></body></html>效果演示
测试地址:https://tczmh.gitee.io/fingerprint2demo/
最终效果:指纹 : 959c35da0972771684b62c210fee5438 消耗 : 351 毫秒 使用的参数 : userAgent : Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36 webdriver : not available language : zh-CN colorDepth : 24 deviceMemory : 4 pixelRatio : 1 hardwareConcurrency : 4 screenResolution : 900,1600 availableScreenResolution : 900,1600 timezoneOffset : -480 timezone : Asia/Shanghai sessionStorage : true localStorage : true indexedDb : true addBehavior : false openDatabase : true cpuClass : not available platform : Linux x86_64 doNotTrack : 1 plugins : Chrome PDF Plugin,Portable Document Format,application/x-google-chrome-pdf,pdf,Chrome PDF Viewer,,application/pdf,pdf,Native Client,,application/x-nacl,,application/x-pnacl (后面太长省略)…
END
演示代码:https://gitee.com/tczmh/Fingerprint2Demo
Fingerprintjs2 Github:https://github.com/Valve/fingerprintjs2
使用:main.js:
import Fingerprint2 from ‘fingerprintjs2’
if (window.requestIdleCallback) {
requestIdleCallback(function () {
Fingerprint2.get(function (components) {
const str = JSON.stringify(components)
console.log(md5(str), ‘缓存’)
})
})
} else {
setTimeout(function () {
Fingerprint2.get(function (components) {
const str = JSON.stringify(components)
console.log(md5(str), ‘首次’)
})
}, 500)
}
