php - 从 PHP GET 方法中的查询参数解密基本身份验证令牌
问题描述
我想解密使用基本身份验证加密的用户名和密码。
在我的客户端应用程序中,我有一个文件列表。如果用户想要下载,他们将点击指向带有参数id
(即fileId
)和.php 文件的 PHP 文件的锚链接token
。
我有一个客户端锚标签,例如
<a href="http://localhost:8080/management/download.php?id=1&token=YmFsYTphYWFhYWFhYWFh">
download
</a>
令牌是使用以下 javascript 代码创建的
const token = window.btoa(username + ':' + password)
文件:下载.php
$fileId = $_GET['id'];
$token = $_GET['token'];
$filePath = getFileById($fileId);
if (file_exists($filePath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
readfile($filePath);
exit;
}
我试图从默认值中获取用户名和密码,$_SERVER['PHP_AUTH_USER']
但我未能获取信息。
请帮助我如何从令牌中解密用户名和密码。
解决方案
在 PHP 中使用base64_decode()来获取用 . 分隔的用户名和密码:
。
<?php
$decoded = base64_decode($_GET['token']);
list($username,$password) = explode(":",$decoded);
echo $username," ",$password;
此外,这似乎不够安全,无法像 GET 参数那样发送密码(不确定此处描述的是什么密码)。最好改为发送一个标头Authorization: Basic YmFsYTphYWFhYWFhYWFh
,例如它实际上是一些客户端 ID 和客户端机密 base-64 编码的位置。
推荐阅读
- java - 我的适配器不会更新值,直到我返回然后再次返回该活动。什么原因
- python - 如何使用漂亮的汤 4 和 python 和 selenium 循环页面?
- sql-server - 通过视图将数据插入多个表(f_k链接在一起)
- css - 如何使用 Material-ui 从 DevExtreme React Grid 获取数据表的完全控制样式?
- ios - 如何将按下的表格视图单元格作为导航标题传递给其他视图控制器?
- c# - 如何在不破坏实例相对位置的情况下更改生成实例的位置?
- javascript - 按特定模式对数组进行排序
- c# - 使用 Web API 端点,但希望在记录之前隐藏查询字符串中的密码等敏感信息
- php - Wordpress - 如何使 URL 返回具有自定义内容类型的动态 JSON?
- excel - 从用户提示中查找具有条目的单元格并设置活动单元格