Enhance authentication logging and update MyBatis configuration

This commit is contained in:
zyh
2025-08-24 16:52:20 +08:00
parent 51d6319121
commit bc1f10381a
20 changed files with 122 additions and 36 deletions

View File

@@ -5,6 +5,8 @@ import com.gameplatform.server.model.dto.auth.LoginRequest;
import com.gameplatform.server.model.dto.auth.LoginResponse;
import com.gameplatform.server.model.entity.account.UserAccount;
import com.gameplatform.server.security.JwtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@@ -14,6 +16,7 @@ import java.util.Map;
@Service
public class AuthService {
private static final Logger log = LoggerFactory.getLogger(AuthService.class);
private final UserAccountMapper userAccountMapper;
private final PasswordEncoder passwordEncoder;
private final JwtService jwtService;
@@ -27,22 +30,25 @@ public class AuthService {
}
public Mono<LoginResponse> login(LoginRequest req) {
String userType = normalizeType(req.getUserType());
return Mono.fromCallable(() -> userAccountMapper.findByUsernameAndType(req.getUsername(), userType))
log.info("login attempt username={}", req.getUsername());
long start = System.currentTimeMillis();
return Mono.fromCallable(() -> userAccountMapper.findByUsername(req.getUsername()))
.subscribeOn(Schedulers.boundedElastic())
.flatMap(acc -> validatePasswordAndBuild(acc, userType, req.getPassword()));
.doOnNext(acc -> {
if (acc == null) {
log.warn("login account not found username={}", req.getUsername());
} else {
log.debug("login account loaded id={}, status={}, role={} userType={}", acc.getId(), acc.getStatus(), acc.getRole(), acc.getUserType());
}
})
.flatMap(acc -> validatePasswordAndBuild(acc, req.getPassword()))
.doOnSuccess(r -> log.info("login success username={}, tookMs={}", req.getUsername(), (System.currentTimeMillis()-start)))
.doOnError(e -> log.warn("login failed username={}, err={}, tookMs={}", req.getUsername(), e.toString(), (System.currentTimeMillis()-start)));
}
private String normalizeType(String t) {
if (t == null) return "";
t = t.trim().toLowerCase();
if ("admin".equals(t)) return "ADMIN";
if ("agent".equals(t)) return "AGENT";
throw new IllegalArgumentException("unsupported userType: " + t);
}
private Mono<LoginResponse> validatePasswordAndBuild(UserAccount acc, String userType, String rawPwd) {
private Mono<LoginResponse> validatePasswordAndBuild(UserAccount acc, String rawPwd) {
if (acc == null || acc.getPasswordHash() == null) {
log.debug("validatePasswordAndBuild: account missing or no password hash");
return Mono.error(new IllegalArgumentException("用户名或密码错误"));
}
boolean ok;
@@ -54,19 +60,24 @@ public class AuthService {
} else {
ok = false;
}
if (!ok) return Mono.error(new IllegalArgumentException("用户名或密码错误"));
if (!ok) {
log.debug("validatePasswordAndBuild: password not match for user id={}", acc.getId());
return Mono.error(new IllegalArgumentException("用户名或密码错误"));
}
if (!"ENABLED".equalsIgnoreCase(acc.getStatus())) {
log.debug("validatePasswordAndBuild: account disabled id={}", acc.getId());
return Mono.error(new IllegalStateException("账户已禁用"));
}
String userType = acc.getUserType() == null ? "agent" : acc.getUserType().toLowerCase();
String token = jwtService.generateToken(
userType.toLowerCase() + ":" + acc.getId(),
userType.toLowerCase(), acc.getId(), acc.getUsername(),
userType.equals("ADMIN") ? Map.of("role", acc.getRole()) : Map.of("displayName", acc.getDisplayName())
userType + ":" + acc.getId(),
userType, acc.getId(), acc.getUsername(),
"admin".equals(userType) ? Map.of("role", acc.getRole()) : Map.of("displayName", acc.getDisplayName())
);
LoginResponse resp = new LoginResponse();
resp.setAccessToken(token);
resp.setUserType(userType.toLowerCase());
resp.setUserType(userType);
resp.setUserId(acc.getId());
resp.setUsername(acc.getUsername());
resp.setExpiresIn(60L * 30);