feat: lock check and login history on admin sign-in and sign-out
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
f6fe246ad0
commit
39708224ad
7 files changed
+349
-3
No files matched your search
+4
@@ -91,6 +91,10 @@ public class RestApiSystemServiceConstants {
|
||||
public static final String API_REST_GENERIC_POST_FIND_BY_UUID = "/system/genericPost/findByUuid/{uuid}";
|
||||
public static final String API_REST_GENERIC_POST_CONTENT_FIND_BY_UUID_AND_LOCALE = "/system/genericPost/content/findByPostUuidAndLocale/{uuid}/{locale}";
|
||||
|
||||
//LoginSecurity (admin sign-in lock check and history)
|
||||
public static final String API_REST_LOGIN_SECURITY_IS_LOCKED = "/loginSecurity/isLocked/{username}";
|
||||
public static final String API_REST_LOGIN_SECURITY_RECORD = "/loginSecurity/record";
|
||||
|
||||
//Message
|
||||
public static final String API_REST_MESSAGE_FIND_BY_CODE_AND_LOCALE = "/message/findByCodeAndLocale/{code}/{locale}";
|
||||
}
|
||||
+19
-1
@@ -34,6 +34,8 @@ import java.util.Map;
|
||||
public class AdminAuthController {
|
||||
private static final Map<String, String> BAD_LOGIN =
|
||||
Map.of("result", "false", "message", "Invalid username or password");
|
||||
private static final Map<String, String> ACCOUNT_LOCKED = Map.of("result", "false", "code", "ACCOUNT_LOCKED",
|
||||
"message", "Too many failed sign-ins. Try again later or ask an administrator to unlock your account.");
|
||||
private final AdminAuthenticationService adminAuthenticationService;
|
||||
private final AuthenticationService authenticationService;
|
||||
|
||||
@@ -44,6 +46,11 @@ public class AdminAuthController {
|
||||
if (!StringUtils.hasText(request.getUsername()) || !StringUtils.hasText(request.getPassword())) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(BAD_LOGIN);
|
||||
}
|
||||
String username = request.getUsername().trim();
|
||||
if (adminAuthenticationService.isLocked(username)) {
|
||||
adminAuthenticationService.record(username, "LOGIN_LOCKED", null);
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ACCOUNT_LOCKED);
|
||||
}
|
||||
String token;
|
||||
User u;
|
||||
try {
|
||||
@@ -60,8 +67,10 @@ public class AdminAuthController {
|
||||
token = null;
|
||||
}
|
||||
if (u == null) {
|
||||
adminAuthenticationService.record(username, "LOGIN_FAIL", null);
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(BAD_LOGIN);
|
||||
}
|
||||
adminAuthenticationService.record(username, "LOGIN_SUCCESS", null);
|
||||
return ResponseEntity.ok(new AdminLoginResponse(token, u.getName(), u.getRoles(), u.getAclMenu(),
|
||||
GeneralEnum.LoginStatus.N.equals(u.getLogin()) ? "true" : "false"));
|
||||
}
|
||||
@@ -105,7 +114,16 @@ public class AdminAuthController {
|
||||
@AppAuth
|
||||
@PostMapping("/auth/logout")
|
||||
public ResponseEntity<Map<String, String>> logout() {
|
||||
authenticationService.logoutToken(HttpReqRespUtils.getJwtToken());
|
||||
String token = HttpReqRespUtils.getJwtToken();
|
||||
try {
|
||||
User u = adminAuthenticationService.loadUserByToken(token); // before the token is revoked
|
||||
if (u != null) {
|
||||
adminAuthenticationService.record(u.getUsername(), "LOGOUT", null);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("Logout history skipped: {}", e.getClass().getSimpleName());
|
||||
}
|
||||
authenticationService.logoutToken(token);
|
||||
return ResponseEntity.ok(Map.of("result", "true"));
|
||||
}
|
||||
|
||||
|
||||
+39
@@ -5,16 +5,22 @@ import com.solvway.mobile.api.constant.api.RestApiAuthServiceConstants;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.model.AuthRequest;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class AdminAuthenticationService {
|
||||
private final RestTemplateCustomUtil restTemplateUtil;
|
||||
|
||||
@@ -65,4 +71,37 @@ public class AdminAuthenticationService {
|
||||
Integer.class, Map.of("username", username, "email", email));
|
||||
return ent.getBody() == null ? 0 : ent.getBody();
|
||||
}
|
||||
|
||||
/** True when services-system says the account is locked; fails open (false) on any error so a fault never blocks sign-in. */
|
||||
public boolean isLocked(String username) {
|
||||
try {
|
||||
ResponseEntity<Map> ent = restTemplateUtil.getForEntity(
|
||||
restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_IS_LOCKED),
|
||||
Map.class, username);
|
||||
return ent.getStatusCode() == HttpStatus.OK && ent.getBody() != null
|
||||
&& Boolean.TRUE.equals(ent.getBody().get("locked"));
|
||||
} catch (Exception e) {
|
||||
log.warn("Login lock check failed, allowing sign-in: {}", e.getClass().getSimpleName());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Best-effort login history entry with the caller's X-Client-IP and User-Agent; never throws. */
|
||||
public void record(String username, String event, String message) {
|
||||
try {
|
||||
HttpServletRequest req = RequestContextHolder.getRequestAttributes() instanceof ServletRequestAttributes a
|
||||
? a.getRequest() : null;
|
||||
Map<String, Object> body = new HashMap<>();
|
||||
body.put("username", username);
|
||||
body.put("event", event);
|
||||
body.put("ip", req == null ? null : req.getHeader("X-Client-IP"));
|
||||
body.put("userAgent", req == null ? null : req.getHeader("User-Agent"));
|
||||
body.put("message", message);
|
||||
restTemplateUtil.postForEntity(
|
||||
restTemplateUtil.getURLforApiService(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_RECORD),
|
||||
String.class, body);
|
||||
} catch (Exception e) {
|
||||
log.warn("Login history record failed: {}", e.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user