This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8c188ba5d1e Refactor MySQLAuthenticationEngine (#37902)
8c188ba5d1e is described below
commit 8c188ba5d1e544114420f85b00c4e0b3a1923019
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jan 30 19:02:15 2026 +0800
Refactor MySQLAuthenticationEngine (#37902)
---
.../mysql/authentication/MySQLAuthenticationEngine.java | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)
diff --git
a/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
b/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
index c6ef9772394..67f7c249e25 100644
---
a/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
+++
b/proxy/frontend/dialect/mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngine.java
@@ -46,6 +46,7 @@ import
org.apache.shardingsphere.database.protocol.mysql.packet.handshake.MySQLH
import
org.apache.shardingsphere.database.protocol.mysql.packet.handshake.MySQLHandshakeResponse41Packet;
import
org.apache.shardingsphere.database.protocol.mysql.payload.MySQLPacketPayload;
import org.apache.shardingsphere.database.protocol.payload.PacketPayload;
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -99,12 +100,9 @@ public final class MySQLAuthenticationEngine implements
AuthenticationEngine {
authenticateMismatchedMethod((MySQLPacketPayload) payload);
}
Grantee grantee = new Grantee(currentAuthResult.getUsername(),
getHostAddress(context));
- if (!login(rule, grantee, authResponse)) {
- throw new AccessDeniedException(currentAuthResult.getUsername(),
grantee.getHostname(), 0 != authResponse.length);
- }
- if (!authorizeDatabase(rule, grantee,
currentAuthResult.getDatabase())) {
- throw new
DatabaseAccessDeniedException(currentAuthResult.getUsername(),
grantee.getHostname(), currentAuthResult.getDatabase());
- }
+ ShardingSpherePreconditions.checkState(login(rule, grantee,
authResponse), () -> new AccessDeniedException(currentAuthResult.getUsername(),
grantee.getHostname(), 0 != authResponse.length));
+ ShardingSpherePreconditions.checkState(authorizeDatabase(rule,
grantee, currentAuthResult.getDatabase()),
+ () -> new
DatabaseAccessDeniedException(currentAuthResult.getUsername(),
grantee.getHostname(), currentAuthResult.getDatabase()));
writeOKPacket(context);
return AuthenticationResultBuilder.finished(grantee.getUsername(),
grantee.getHostname(), currentAuthResult.getDatabase());
}
@@ -123,9 +121,8 @@ public final class MySQLAuthenticationEngine implements
AuthenticationEngine {
setMultiStatementsOption(context, handshakeResponsePacket);
setCharacterSet(context, handshakeResponsePacket);
String database = handshakeResponsePacket.getDatabase();
- if (!Strings.isNullOrEmpty(database) &&
!ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().containsDatabase(database))
{
- throw new UnknownDatabaseException(database);
- }
+ ShardingSpherePreconditions.checkState(Strings.isNullOrEmpty(database)
||
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().containsDatabase(database),
+ () -> new UnknownDatabaseException(database));
String username = handshakeResponsePacket.getUsername();
String hostname = getHostAddress(context);
ShardingSphereUser user = rule.findUser(new Grantee(username,
hostname)).orElseGet(() -> new ShardingSphereUser(username, "", hostname));