情景还原
后端应用部署启动的时候,时不时地出现 MySQL Public Key Retrieval is not allowed
的错误。
问题探究
在网上检索 MySQL 连接相关资料看到了以下说明:
If the user account uses sha256_password authentication, the password must be protected during transmission; TLS is the preferred mechanism for this, but if it is not available then RSA public key encryption will be used. To specify the server’s RSA public key, use the ServerRSAPublicKeyFile connection string setting, or set AllowPublicKeyRetrieval=True to allow the client to automatically request the public key from the server. Note that AllowPublicKeyRetrieval=True could allow a malicious proxy to perform a MITM attack to get the plaintext password, so it is False by default and must be explicitly enabled.
大概是说如果 MySQL 的密码是通过 SHA256 方式加密认证的,就需要在传输过程中保证连接密码是加密过的。通过将 allowPublicKeyRetrieval
参数设为 true
可以让客户端拿到 RSA 公钥,并以此来保证连接密码是加密的。但开启这个参数同时也会带来另一个问题:黑客可以发起中间人攻击( MITM attack )来获取明文密码。
解决方案
可以通过在 MySQL 连接地址上加上参数 allowPublicKeyRetrieval=true
来解决该问题。但必须注意改动这个参数所带来的弊端。