Hyperf方案 设备指纹识别

张开发
2026/4/18 0:06:02 15 分钟阅读

分享文章

Hyperf方案 设备指纹识别
设备指纹有两种方案取决于你的场景 b ┌───────────────────────────────────┬───────┬────────────────┐ │ 方案 │ 精度 │ 场景 │ ├───────────────────────────────────┼───────┼────────────────┤ ────────────────────────────── │ FingerprintJS Pro(客户端服务端)│99.5% │ 反欺诈、风控 │ ├───────────────────────────────────┼───────┼────────────────┤ │ DeviceDetector(纯服务端 UA 解析)│ 中等 │ 设备分类、统计 │ └───────────────────────────────────┴───────┴────────────────┘ --- 方案一FingerprintJS Pro推荐高精度风控 客户端采集 → 服务端验证精度最高。composerrequire fingerprintjs/fingerprint-pro-server-api-php-sdk 前端一行引入scriptsrchttps://fpjscdn.net/v3/YOUR_PUBLIC_KEY/iife.min.js/scriptscriptFingerprintJS.load().then(fpfp.get()).then(r{// 将 r.visitorId 和 r.requestId 发给后端 fetch(/api/verify,{method:POST, body: JSON.stringify(r)});});/scriptHyperf 服务端验证?php namespace App\Controller;use Fingerprint\ServerAPI\Api\FingerprintApi;use Fingerprint\ServerAPI\Configuration;use GuzzleHttp\Client;use Hyperf\HttpServer\Annotation\Controller;use Hyperf\HttpServer\Annotation\PostMapping;#[Controller(prefix: /api)]class FingerprintController{private FingerprintApi$client;publicfunction__construct(){$configConfiguration::getDefaultConfiguration(env(FINGERPRINT_SECRET_KEY));$this-clientnew FingerprintApi(new Client(),$config);}#[PostMapping(path: verify)]publicfunctionverify(): array{$requestId$this-request-input(requestId);[$event,$error]$this-client-getEvent($requestId);if($error){return[risktrue];}$identification$event-getProducts()-getIdentification()-getData();return[visitorId$identification-getVisitorId(),confidence$identification-getConfidence()-getScore(),incognito$identification-getIncognito(),ip$identification-getIp(),];}}--- 方案二DeviceDetector纯服务端免费 适合设备分类、UA 解析无需客户端 JS。composerrequire matomo/device-detector 封装为 Hyperf Service?php namespace App\Service;use DeviceDetector\DeviceDetector;use DeviceDetector\Cache\PSR6Bridge;use Psr\SimpleCache\CacheInterface;class DeviceFingerprintService{publicfunction__construct(private CacheInterface$cache){}publicfunctiondetect(string$userAgent): array{$ddnew DeviceDetector($userAgent);$dd-setCache(new PSR6Bridge($this-cache));$dd-parse();return[fingerprintmd5($userAgent.request()-ip()),device_type$dd-getDeviceName(), // desktop/tablet/smartphonebrand$dd-getBrandName(),model$dd-getModel(),os$dd-getOs(name),browser$dd-getClient(name),is_bot$dd-isBot(),];}}控制器调用#[GetMapping(path: device)]publicfunctiondevice(): array{return$this-deviceService-detect($this-request-header(User-Agent));}--- 自建轻量指纹无第三方依赖 如果只需要简单的请求指纹IP UA Accept-Language 组合 publicfunctionfingerprint(RequestInterface$request): string{$rawimplode(|,[$request-getHeaderLine(User-Agent),$request-getHeaderLine(Accept-Language),$request-getHeaderLine(Accept-Encoding),$request-server(remote_addr),]);returnhash(xxh3,$raw);// PHP8.1 内置极快}--- 选型建议 - 反欺诈/风控 → FingerprintJS Pro付费但精度无敌 - 设备统计/适配 → DeviceDetector免费纯服务端 - 简单会话绑定 → 自建 hash零依赖

更多文章