通过 php 添加管理 mysql 用户的安全方法

发布于 2025-01-05 02:30:31 字数 425 浏览 3 评论 0原文

我正在构建一个在 mysql 中添加和删除用户的 php 系统。目前的设置如下: - 用户尝试登录网站 - 在projectdb.users中查找用户名和哈希值(sha256、salt、密码)(这也是mysql用户凭据) - 找到它,然后使用该用户名和密码在登录时执行任何 mysql_connect

因此,当我在系统中添加用户时,我将其凭据添加到表中,并将它们添加为仅对projectdb数据库具有最小权限的mysql用户。

但是当我添加管理员帐户时,我授予他们对该数据库的完全权限,并授予他们 mysql 授予/添加用户权限,以便他们可以添加更多用户。这似乎太过强大,即使这些用户无法直接连接并登录 mysql(但如果可以的话,他们可以添加任何人)。

与其实际将 mysql 用户添加到系统中,不如在 php 文件中硬编码单个用户和管理员权限更好吗?还有比这两种想法更安全的方法吗?

I'm building a php system that adds and deletes users in mysql. It's currently set up like so:
- user attempts to log in to site
- looks up username and hash(sha256, salt, password) in projectdb.users (which is also a mysql users credentials)
- finds it and then uses that username and password to do any mysql_connect's while logged in

So when I add a user in the system I'm adding their credentials to a table and adding them as a mysql user with minimal permissions only on the projectdb database.

But when I add an admin account I'm giving them full permission to that db and giving them mysql grant/add user permissions so they can add more users. This seems like too much power, even though these users can't directly connect and log into mysql (but if they could they could add anyone).

Instead of actually adding mysql users to the system is it better to hard code a single user and admin's permissions in the php files? Is there a safer way than those two ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2025-01-12 02:30:31

如果用户不直接访问数据库,则不必直接向他们授予/添加用户权限。

大多数 CMS 都会实现一个权限表,允许 PHP 代码检查用户是否具有执行特定操作的适当权限。因此,您的 PHP 代码可以简单地检查登录用户是否具有权限表设置的权限,然后让实际的管理员用户(即 root)自行创建用户。它更加迂回,但通过在该权限结构上创建额外的层来提供稍微更细粒度的控制。

If the user is not accessing the database directly, you don't have to give them the grant/add user permission directly.

Most CMS implements a permissions table that will allow the PHP code to check if the user has the proper permissions to perform a certain action. As such, your PHP code can simply check to if the logged-in user has the permission set by the permissions table, then have an actual admin user (i.e., root) create the user itself. It is more roundabout, but gives slightly more fine-grain control by creating an extra layer upon that permission structure.

妥活 2025-01-12 02:30:31

根据 David 的建议...如果这是一个 Web 应用程序,为什么要将它们添加到 MySQL 用户中?这是为了记录吗?如果没有真正令人信服的理由,我不认为以编程方式添加用户是一种好的做法。这通常是有意抽象的,以最大限度地降低风险。

Per David's suggestion... If this is a web app, why are you adding them to the MySQL users at all? Is this for logging? I can't see programmatically adding users as a good practice without a really compelling reason. This is usually abstracted on purpose and to minimize risk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文