|
TOP
|
|
分布式数据库中间件?(2)Cobar与客户端的握手认证(三)
| 03 |
if (data.length == QuitPacket.QUIT.length && data[4] == MySQLPacket.COM_QUIT) { |
| 09 |
AuthPacket auth = new AuthPacket(); |
| 13 |
if (!checkUser(auth.user, source.getHost())) { |
| 14 |
failure(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + auth.user + "'"); |
| 18 |
if (!checkPassword(auth.password, auth.user)) { |
| 19 |
failure(ErrorCode.ER_ACCESS_DENIED_ERROR, "Access denied for user '" + auth.user + "'"); |
| 23 |
switch (checkSchema(auth.database, auth.user)) { |
| 24 |
case ErrorCode.ER_BAD_DB_ERROR: |
| 25 |
failure(ErrorCode.ER_BAD_DB_ERROR, "Unknown database '" + auth.database + "'"); |
| 27 |
case ErrorCode.ER_DBACCESS_DENIED_ERROR: |
| 28 |
String s = "Access denied for user '" + auth.user + "' to database '" + auth.database + "'"; |
| 29 |
failure(ErrorCode.ER_DBACCESS_DENIED_ERROR, s); |
在上面的auth.read函数中会按9中的协议格式进行读取数据到auth对象。认证成功后会执行:
| 01 |
protected void success(AuthPacket auth) { |
| 03 |
source.setAuthenticated(true); |
| 04 |
source.setUser(auth.user); |
| 05 |
source.setSchema(auth.database); |
| 06 |
source.setCharsetIndex(auth.charsetIndex); |
| 08 |
source.setHandler(new FrontendCommandHandler(source)); |
| 10 |
ByteBuffer buffer = source.allocate(); |
| 11 |
source.write(source.writeToBuffer(AUTH_OK, buffer)); |
可以看到,在上面的函数中,设置连接对象source中的成员(是否认证、用户、数据库、编码、处理该连接后续数据包的处理器【handle方法】) 然后回复认证成功的消息。后面客户端再发送消息,会交给前端命令处理器进行处理。 客户端进行链接的时候Cobar服务器的输出:
| 01 |
16:59:19,388 INFO =============================================== |
| 02 |
16:59:19,389 INFO Cobar is ready to startup ... |
| 03 |
16:59:19,389 INFO Startup processors ... |
| 04 |
16:59:19,455 INFO Startup connector ... |
| 05 |
16:59:19,460 INFO Initialize dataNodes ... |
| 06 |
16:59:19,506 INFO dnTest1:0 init success |
| 07 |
16:59:19,514 INFO dnTest3:0 init success |
| 08 |
16:59:19,517 INFO dnTest2:0 init success |
| 09 |
16:59:19,527 INFO CobarServer is started and listening on 8066 |
| 10 |
16:59:19,527 INFO =============================================== |
| 11 |
16:59:23,459 DEBUG 1>>NIOReactor接受连接数:0 |
| 12 |
16:59:23,464 DEBUG 2>>NIOReactor接受连接数:1 |
| 13 |
16:59:23,465 DEBUG select读事件 |
| 14 |
16:59:23,465 INFO com.alibaba.cobar.net.handler.FrontendAuthenticator接收的请求长度:62 |
| 15 |
58 0 0 1 5 166 15 0 0 0 0 1 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 114 111 111 116 0 20 169 171 247 102 133 96 158 224 121 22 226 229 88 244 119 238 185 61 124 219 |
| 16 |
16:59:23,468 INFO [thread=Processor1-H0,class=ServerConnection,host=192.168.137.8,port=46101,schema=null]'root' login success |
客户端得到的回复:
| 01 |
yan@yan-Z400:~$ mysql -uroot -p** -P8066 -h192.168.137.8 |
| 02 |
Welcome to the MySQL monitor. Commands end with ; or \g. |
| 03 |
Your MySQL connection id is 1 |
|