简体   繁体   English

显示用于连接到服务器的用户名和密码的代码

[英]Code showing username and password for connecting to server

I'm writing a method that make it possible for my Java program to create a database connection that will eventually make me able to access it from other classes/methods.我正在编写一种方法,使我的 Java 程序可以创建一个数据库连接,最终使我能够从其他类/方法访问它。

public class DatabaseConnection
{
    
    private Connection databaseLink;
    
    public Connection getConnection()
    {
        String url = "jdbc:mysql://localhost/DBname";
        
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            databaseLink = DriverManager.getConnection(url, "fakeUsr", "fakePsw");  //these are not the real username/password
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        return databaseLink;
    }
}

I've got a couple of issues: 1)people not using my computer will not be able to get into my server since I wrote "localhost":我有几个问题:1)自从我写了“localhost”后,不使用我的电脑的人将无法进入我的服务器:

String url = "jdbc:mysql://localhost/DBname";

2)I've typed the real username and password instead of "fakeUsr" and "fakePsw". 2) 我输入了真实的用户名和密码,而不是“fakeUsr”和“fakePsw”。 The thing is: I'm quite sure that the average user of my program should NOT be able to access that information.问题是:我很确定我的程序的普通用户不应该能够访问该信息。 Is there any other way to permit access to a DB without making username and password readable by virtually anyone getting access to my source code?有没有其他方法允许访问数据库而不让几乎任何访问我的源代码的人都可以读取用户名和密码?

For issue n.对于问题 n。 1: I tried to type my IP address instead of "localhost" here: String url = "jdbc:mysql://localhost/DBname"; //changed localhost to my IP address 1:我试着在这里输入我的 IP 地址而不是“localhost”: String url = "jdbc:mysql://localhost/DBname"; //changed localhost to my IP address String url = "jdbc:mysql://localhost/DBname"; //changed localhost to my IP address

but then I get "Communications link failure".但后来我得到“通信链接失败”。

For issue n.对于问题 n。 2: I have literally no idea how to solve this. 2:我真的不知道如何解决这个问题。 I've never coded a program that needs access to a DB so I had to improvise a bit for that.我从来没有编写过需要访问数据库的程序,所以我不得不为此即兴创作。

About Issue #2:关于问题 #2:

There is no secure way of storing the password inside the code itself.没有安全的方法将密码存储在代码本身中。 You can of course try to encrypt the password, but then your code has to decrypt it when the connection is established and therefore the encryption key is visible virtually "to all that have access to your source code".您当然可以尝试加密密码,但是您的代码必须在建立连接时对其进行解密,因此加密密钥几乎“对所有有权访问您的源代码的人”都是可见的。 With this key, it is possible to get to the real password, just a little bit more complicated.有了这个密钥,就可以得到真正的密码,只是稍微复杂一点。

The only secure way is to have the user enter the login credentials by his own .唯一安全的方法是让用户自己输入登录凭据 Either low level (program arguments when starting your application) or by some form of "login dialog", if the application has a GUI.低级别(启动应用程序时程序 arguments)或某种形式的“登录对话框”,如果应用程序有 GUI。

A third option would be to create a technical user with restricted DB access, depending on the application you are working on.第三种选择是创建一个具有受限数据库访问权限的技术用户,具体取决于您正在处理的应用程序。 But this usually causes security issues.但这通常会导致安全问题。

You could create your application such that it sends an https request and authenticate itself against a webserver.您可以创建您的应用程序,使其发送 https 请求并针对网络服务器进行身份验证。 What you use to authenticate is up to you: Client IP, username, password, client certificates, ...您使用什么来进行身份验证取决于您:客户端 IP、用户名、密码、客户端证书……

Once authenticated, your webserver could transfer a one-time username/password that the client uses to login into your database.一旦通过身份验证,您的网络服务器可以传输客户端用于登录到您的数据库的一次性用户名/密码。

The advantage here is that you can still control whether the user gets full or restricted access, or gets no password any more for whatever reason.这里的优点是您仍然可以控制用户是获得完全访问权限还是受限访问权限,或者出于任何原因不再获得密码。 And there is no security hole in your application.而且您的应用程序中没有安全漏洞。

1) Most Inte.net providers don't allow ordinary users to accept incoming socket connections, both for security reasons and because the.network traffic can quickly overwhelm consumer grade.networks. 1) 大多数 Inte.net 提供商不允许普通用户接受传入的套接字连接,这既是出于安全原因,也是因为.network 流量会很快淹没消费级.networks。 You will have to either purchase a commercial Inte.net connection which allows incoming connections, or look for a server you can lease or borrow.您将不得不购买允许传入连接的商业互联网连接,或者寻找可以租赁或借用的服务器。 I'm afraid I don't know what options are available.恐怕我不知道有哪些选择。

2) As MrFreeze correctly pointed out, there is no way to safely embed credentials in an application. 2) 正如 MrFreeze 正确指出的那样,没有办法在应用程序中安全地嵌入凭据。 No matter what you do to obscure your database login credentials, someone can always decompile your program and figure out how you are decrypting those credentials.无论您做什么来隐藏您的数据库登录凭据,总有人可以反编译您的程序并弄清楚您是如何解密这些凭据的。 The only truly safe solution is to tell users you trust what the credentials are, then write your application so the user must enter them.唯一真正安全的解决方案是告诉用户您信任凭据是什么,然后编写您的应用程序以便用户必须输入它们。

Side note: Class.forName("com.mysql.cj.jdbc.Driver");旁注: Class.forName("com.mysql.cj.jdbc.Driver"); has not been needed for many years.多年来一直不需要。 You can remove that line.您可以删除该行。

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

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