30
30
*/
31
31
public class MysqlConnector {
32
32
33
+ public static final int timeout = 5 * 1000 ; // 5s
33
34
private static final Logger logger = LoggerFactory .getLogger (MysqlConnector .class );
34
35
private InetSocketAddress address ;
35
36
private String username ;
36
37
private String password ;
37
38
private SslInfo sslInfo ;
38
-
39
39
private String defaultSchema ;
40
40
private int soTimeout = 30 * 1000 ;
41
41
private int connTimeout = 5 * 1000 ;
42
42
private int receiveBufferSize = 16 * 1024 ;
43
43
private int sendBufferSize = 16 * 1024 ;
44
-
45
44
private SocketChannel channel ;
46
45
private volatile boolean dumping = false ;
47
46
// mysql connectionId
@@ -50,8 +49,6 @@ public class MysqlConnector {
50
49
// serverVersion
51
50
private String serverVersion ;
52
51
53
- public static final int timeout = 5 * 1000 ; // 5s
54
-
55
52
public MysqlConnector (){
56
53
}
57
54
@@ -200,14 +197,15 @@ private void negotiate(SocketChannel channel) throws IOException {
200
197
}
201
198
HandshakeInitializationPacket handshakePacket = new HandshakeInitializationPacket ();
202
199
handshakePacket .fromBytes (body );
200
+ byte serverCharsetNumber = (handshakePacket .serverCharsetNumber != 0 ) ? handshakePacket .serverCharsetNumber : 33 ;
203
201
SslMode sslMode = sslInfo != null ? sslInfo .getSslMode () : SslMode .DISABLED ;
204
202
if (sslMode != SslMode .DISABLED ) {
205
203
boolean serverSupportSsl = (handshakePacket .serverCapabilities & CLIENT_SSL ) > 0 ;
206
204
if (!serverSupportSsl ) {
207
205
throw new IOException ("MySQL Server does not support SSL: " + address + " serverCapabilities: "
208
206
+ handshakePacket .serverCapabilities );
209
207
}
210
- byte [] sslPacket = new SslRequestCommandPacket (handshakePacket . serverCharsetNumber ).toBytes ();
208
+ byte [] sslPacket = new SslRequestCommandPacket (serverCharsetNumber ).toBytes ();
211
209
HeaderPacket sslHeader = new HeaderPacket ();
212
210
sslHeader .setPacketBodyLength (sslPacket .length );
213
211
sslHeader .setPacketSequenceNumber ((byte ) (header .getPacketSequenceNumber () + 1 ));
@@ -225,23 +223,27 @@ private void negotiate(SocketChannel channel) throws IOException {
225
223
connectionId = handshakePacket .threadId ; // 记录一下connection
226
224
serverVersion = handshakePacket .serverVersion ; // 记录serverVersion
227
225
logger .info ("handshake initialization packet received, prepare the client authentication packet to send" );
228
- logger .info ("auth plugin: {}" , new String (handshakePacket .authPluginName ));
226
+ // 某些老协议的 server 默认不返回 auth plugin,需要使用默认的 mysql_native_password
227
+ String authPluginName = (handshakePacket .authPluginName != null
228
+ && handshakePacket .authPluginName .length > 0 ) ? new String (
229
+ handshakePacket .authPluginName ) : "mysql_native_password" ;
230
+ logger .info ("auth plugin: {}" , authPluginName );
229
231
boolean isSha2Password = false ;
230
232
ClientAuthenticationPacket clientAuth ;
231
- if ("caching_sha2_password" .equals (new String ( handshakePacket . authPluginName ) )) {
233
+ if ("caching_sha2_password" .equals (authPluginName )) {
232
234
clientAuth = new ClientAuthenticationSHA2Packet ();
233
235
isSha2Password = true ;
234
236
} else {
235
237
clientAuth = new ClientAuthenticationPacket ();
236
238
}
237
- clientAuth .setCharsetNumber (handshakePacket . serverCharsetNumber );
239
+ clientAuth .setCharsetNumber (serverCharsetNumber );
238
240
239
241
clientAuth .setUsername (username );
240
242
clientAuth .setPassword (password );
241
243
clientAuth .setServerCapabilities (handshakePacket .serverCapabilities );
242
244
clientAuth .setDatabaseName (defaultSchema );
243
245
clientAuth .setScrumbleBuff (joinAndCreateScrumbleBuff (handshakePacket ));
244
- clientAuth .setAuthPluginName (handshakePacket . authPluginName );
246
+ clientAuth .setAuthPluginName (authPluginName . getBytes () );
245
247
246
248
byte [] clientAuthPkgBody = clientAuth .toBytes ();
247
249
HeaderPacket h = new HeaderPacket ();
@@ -284,7 +286,7 @@ private void negotiate(SocketChannel channel) throws IOException {
284
286
encryptedPassword = getPassword ().getBytes ();
285
287
header = authSwitchAfterAuth (encryptedPassword , header );
286
288
body = PacketManager .readBytes (channel , header .getPacketBodyLength (), timeout );
287
- } else if ("mysql_native_password" .equals (pluginName )) {
289
+ } else if (pluginName == null || "mysql_native_password" .equals (pluginName )) {
288
290
try {
289
291
encryptedPassword = MySQLPasswordEncrypter .scramble411 (getPassword ().getBytes (), authData );
290
292
} catch (NoSuchAlgorithmException e ) {
@@ -481,10 +483,6 @@ public void setChannel(SocketChannel channel) {
481
483
this .channel = channel ;
482
484
}
483
485
484
- public void setPassword (String password ) {
485
- this .password = password ;
486
- }
487
-
488
486
public long getConnectionId () {
489
487
return connectionId ;
490
488
}
@@ -513,6 +511,10 @@ public String getPassword() {
513
511
return password ;
514
512
}
515
513
514
+ public void setPassword (String password ) {
515
+ this .password = password ;
516
+ }
517
+
516
518
public String getServerVersion () {
517
519
return serverVersion ;
518
520
}
0 commit comments