feat: login history and lockout rule in services-system

LoginSecurityService derives the admin lock from login_audit and the
ADMIN_LOGIN_BLOCK settings; /loginSecurity exposes lock check, record,
history, locked list, unlock, lockout settings and password rules. The
gateway routes /loginSecurity/** and lets the lock check and record
through without a user token for the sign-in path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-30 13:26:20 +08:00
1 parent a0146be325
commit f7ec74c7f0
11 files changed
+1074 -2

No files matched your search

@@ -0,0 +1,161 @@
package com.solvway.services.system.controller;
import com.solvway.services.system.service.LoginSecurityService;
import com.solvway.services.system.service.LoginSecurityService.Config;
import com.solvway.services.system.service.LoginSecurityService.Rule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ExtendWith(MockitoExtension.class)
class LoginSecurityControllerTest {
@Mock private LoginSecurityService service;
@InjectMocks private LoginSecurityController controller;
private MockMvc mvc;
@BeforeEach
void setUp() {
mvc = MockMvcBuilders.standaloneSetup(controller).build();
}
private org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder json(String url, String body) {
return post(url).contentType(MediaType.APPLICATION_JSON).content(body);
}
@Test
void isLockedReturnsFlag() throws Exception {
when(service.isLocked("admin")).thenReturn(true);
mvc.perform(get("/loginSecurity/isLocked/admin")).andExpect(status().isOk()).andExpect(jsonPath("$.locked").value(true));
}
@Test
void recordPassesFieldsAndReturnsEmpty200() throws Exception {
mvc.perform(json("/loginSecurity/record",
"{\"username\":\"bob\",\"event\":\"LOGIN_FAIL\",\"ip\":\"1.2.3.4\",\"userAgent\":\"ua\",\"message\":\"m\"}"))
.andExpect(status().isOk()).andExpect(content().string(""));
verify(service).record("bob", "LOGIN_FAIL", "1.2.3.4", "ua", "m");
}
@Test
void recordRefusalIsA400WithMessage() throws Exception {
doThrow(new IllegalArgumentException("username is required")).when(service).record(any(), any(), any(), any(), any());
mvc.perform(json("/loginSecurity/record", "{\"username\":\" \",\"event\":\"LOGIN_FAIL\"}"))
.andExpect(status().isBadRequest()).andExpect(jsonPath("$.message").value("username is required"));
}
@Test
void auditParsesDatesAndPassesFilters() throws Exception {
when(service.audit(any(), any(), any(), any(), any(), any())).thenReturn(Map.of("content", List.of(), "totalElements", 0L, "number", 1, "size", 100));
mvc.perform(json("/loginSecurity/audit",
"{\"username\":\"bo\",\"event\":\"LOGOUT\",\"from\":\"2025-06-01\",\"to\":\"2025-06-02\",\"page\":1,\"size\":500}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.size").value(100)).andExpect(jsonPath("$.totalElements").value(0));
verify(service).audit("bo", "LOGOUT", LocalDate.of(2025, 6, 1), LocalDate.of(2025, 6, 2), 1, 500);
}
@Test
void auditWithBadDateIs400() throws Exception {
mvc.perform(json("/loginSecurity/audit", "{\"from\":\"01/06/2025\"}")).andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").exists());
verify(service, never()).audit(any(), any(), any(), any(), any(), any());
}
@Test
void lockedListIsReturned() throws Exception {
when(service.lockedUsers()).thenReturn(List.of(new LoginSecurityService.Locked("a", 3,
LocalDateTime.of(2025, 6, 15, 11, 59), LocalDateTime.of(2025, 6, 15, 12, 5))));
mvc.perform(get("/loginSecurity/locked")).andExpect(status().isOk())
.andExpect(jsonPath("$[0].username").value("a")).andExpect(jsonPath("$[0].failures").value(3))
.andExpect(jsonPath("$[0].lastFailure").exists()).andExpect(jsonPath("$[0].lockedUntil").exists());
}
@Test
void unlockPassesUsernameAndBy() throws Exception {
mvc.perform(json("/loginSecurity/unlock", "{\"username\":\"bob\",\"by\":\"admin\"}")).andExpect(status().isOk());
verify(service).unlock("bob", "admin");
}
@Test
void unlockBlankIs400() throws Exception {
doThrow(new IllegalArgumentException("username is required")).when(service).unlock(any(), any());
mvc.perform(json("/loginSecurity/unlock", "{\"username\":\"\",\"by\":\"admin\"}")).andExpect(status().isBadRequest());
}
@Test
void configIsReadAndSaved() throws Exception {
when(service.config()).thenReturn(new Config(true, 5, 10, "M"));
when(service.saveConfig(new Config(false, 3, 2, "H"))).thenReturn(new Config(false, 3, 2, "H"));
mvc.perform(get("/loginSecurity/config")).andExpect(jsonPath("$.enabled").value(true))
.andExpect(jsonPath("$.maxAttempts").value(5)).andExpect(jsonPath("$.period").value(10)).andExpect(jsonPath("$.unit").value("M"));
mvc.perform(json("/loginSecurity/config", "{\"enabled\":false,\"maxAttempts\":3,\"period\":2,\"unit\":\"H\"}"))
.andExpect(status().isOk()).andExpect(jsonPath("$.unit").value("H"));
}
@Test
void invalidConfigIs400WithMessage() throws Exception {
when(service.saveConfig(any())).thenThrow(new IllegalArgumentException("unit must be S, M, H or D"));
mvc.perform(json("/loginSecurity/config", "{\"enabled\":true,\"maxAttempts\":3,\"period\":2,\"unit\":\"X\"}"))
.andExpect(status().isBadRequest()).andExpect(jsonPath("$.message").value("unit must be S, M, H or D"));
}
@Test
void passwordRulesAreReadAndSaved() throws Exception {
when(service.passwordRules()).thenReturn(List.of(new Rule(2L, "INPUT_LENGTH_MIN", "Y", "8")));
when(service.savePasswordRules(eq(List.of(new Rule(2L, null, "N", "12"))))).thenReturn(List.of(new Rule(2L, "INPUT_LENGTH_MIN", "N", "12")));
mvc.perform(get("/loginSecurity/passwordRules")).andExpect(jsonPath("$[0].param").value("INPUT_LENGTH_MIN"));
mvc.perform(json("/loginSecurity/passwordRules", "[{\"id\":2,\"status\":\"N\",\"value\":\"12\"}]"))
.andExpect(status().isOk()).andExpect(jsonPath("$[0].status").value("N"));
}
@Test
void invalidPasswordRulesAre400() throws Exception {
when(service.savePasswordRules(any())).thenThrow(new IllegalArgumentException("status must be Y or N"));
mvc.perform(json("/loginSecurity/passwordRules", "[{\"id\":2,\"status\":\"X\"}]")).andExpect(status().isBadRequest());
}
@Test
void nullOrMissingBodyIs400WithMessage() throws Exception {
for (String url : List.of("/loginSecurity/record", "/loginSecurity/audit", "/loginSecurity/unlock",
"/loginSecurity/config", "/loginSecurity/passwordRules")) {
mvc.perform(json(url, "null")).andExpect(status().isBadRequest()).andExpect(jsonPath("$.message").exists());
mvc.perform(post(url).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").exists());
}
}
}