检查当前用户是否有权访问数据库

时间:2019-02-13 16:55:29

标签: java mysql jdbc

我以root用户身份登录mysql并运行

GRANT SELECT (`creator`, `name`, `disguise`), SHOW VIEW ON `kit` TO 'ssc'@'localhost';

现在,从我的代码中,我以'ssc'@'localhost'登录。

Connection db = DriverManager.getConnection(..., 'ssc', ...);

用户'ssc'@'localhost'如何检查其是否具有上述授权?

我在想也许我可以在try / catch (SQLException)块中执行一些查询,但是也许有一种更优雅的方式。

如果我采用这种方法,我也不知道哪种查询最合适(最快,最不密集)。

1 个答案:

答案 0 :(得分:0)

  1. 您可以解析SHOW GRANT查询。这是参考。 https://dev.mysql.com/doc/refman/5.7/en/show-grants.html
  2. 您可以查询information_schema.USER_PRIVILEGES表。这是参考。 https://dev.mysql.com/doc/refman/5.7/en/user-privileges-table.html