简体   繁体   English

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

[英]check current user has access to database

I log into mysql as root and run 我以root用户身份登录mysql并运行

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

Now from my code I log in as 'ssc'@'localhost' . 现在,从我的代码中,我以'ssc'@'localhost'登录。

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

How can the user 'ssc'@'localhost' check that it has the above grant? 用户'ssc'@'localhost'检查它是否具有上述授予?

I am thinking maybe I can execute some queries in a try / catch (SQLException) block, but perhaps there's a more elegant way. 我在想也许我可以在try / catch (SQLException)块中执行一些查询,但是也许有一种更优雅的方法。

Also if I am to take that approach I don't know what queries would be the most appropriate (fastest, least intensive). 同样,如果我采用这种方法,我也不知道哪种查询最合适(最快,最不密集)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM