feat: admin API for login history, locked accounts, unlock and settings
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
6c6734baa3
commit
5ce684168a
7 files changed
+423
-2
No files matched your search
+5
@@ -95,6 +95,11 @@ public class RestApiSystemServiceConstants {
|
||||
//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";
|
||||
public static final String API_REST_LOGIN_SECURITY_AUDIT = "/loginSecurity/audit";
|
||||
public static final String API_REST_LOGIN_SECURITY_LOCKED = "/loginSecurity/locked";
|
||||
public static final String API_REST_LOGIN_SECURITY_UNLOCK = "/loginSecurity/unlock";
|
||||
public static final String API_REST_LOGIN_SECURITY_CONFIG = "/loginSecurity/config";
|
||||
public static final String API_REST_LOGIN_SECURITY_PASSWORD_RULES = "/loginSecurity/passwordRules";
|
||||
|
||||
//Message
|
||||
public static final String API_REST_MESSAGE_FIND_BY_CODE_AND_LOCALE = "/message/findByCodeAndLocale/{code}/{locale}";
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package com.solvway.mobile.api.controller.admin;
|
||||
|
||||
import com.solvway.core.annotation.AppAuth;
|
||||
import com.solvway.mobile.api.service.AdminLoginSecurityService;
|
||||
import com.solvway.mobile.api.useraccess.AdminModule;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/** Login security screen: sign-in history, locked accounts, lockout settings, password rules. Must stay under {@code controller..} for AppAccessAspect. */
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/login-security")
|
||||
@RequiredArgsConstructor
|
||||
@AdminModule("BO_LOGIN_SECURITY")
|
||||
public class AdminLoginSecurityController {
|
||||
|
||||
private final AdminLoginSecurityService loginSecurity;
|
||||
|
||||
@AppAuth
|
||||
@GetMapping("/audit")
|
||||
public ResponseEntity<Map<String, Object>> audit(
|
||||
@RequestParam(required = false) String username,
|
||||
@RequestParam(required = false) String event,
|
||||
@RequestParam(required = false) String from,
|
||||
@RequestParam(required = false) String to,
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "20") int size) {
|
||||
return ResponseEntity.ok(loginSecurity.audit(username, event, from, to, page, size));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@GetMapping("/locked")
|
||||
public ResponseEntity<List<Map<String, Object>>> locked() {
|
||||
return ResponseEntity.ok(loginSecurity.locked());
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@PostMapping("/locked/{username}/unlock")
|
||||
public ResponseEntity<Void> unlock(@PathVariable String username) {
|
||||
loginSecurity.unlock(username);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@GetMapping("/config")
|
||||
public ResponseEntity<Map<String, Object>> config() {
|
||||
return ResponseEntity.ok(loginSecurity.config());
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@PutMapping("/config")
|
||||
public ResponseEntity<Map<String, Object>> saveConfig(@RequestBody Map<String, Object> config) {
|
||||
return ResponseEntity.ok(loginSecurity.saveConfig(config));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@GetMapping("/password-rules")
|
||||
public ResponseEntity<List<Map<String, Object>>> passwordRules() {
|
||||
return ResponseEntity.ok(loginSecurity.passwordRules());
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@PutMapping("/password-rules")
|
||||
public ResponseEntity<List<Map<String, Object>>> savePasswordRules(@RequestBody List<Map<String, Object>> rules) {
|
||||
return ResponseEntity.ok(loginSecurity.savePasswordRules(rules));
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package com.solvway.mobile.api.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.core.model.User;
|
||||
import com.solvway.core.utils.HttpReqRespUtils;
|
||||
import com.solvway.mobile.api.constant.api.RestApiSystemServiceConstants;
|
||||
import com.solvway.mobile.api.errorhandler.CodedResponseStatusException;
|
||||
import com.solvway.mobile.api.util.RestTemplateCustomUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** Passes the Login security screen's calls to services-system, turning its 400s into coded admin errors. */
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AdminLoginSecurityService {
|
||||
|
||||
private static final ParameterizedTypeReference<Map<String, Object>> MAP = new ParameterizedTypeReference<>() {};
|
||||
private static final ParameterizedTypeReference<List<Map<String, Object>>> LIST = new ParameterizedTypeReference<>() {};
|
||||
private static final ObjectMapper JSON = new ObjectMapper();
|
||||
|
||||
private final RestTemplateCustomUtil restTemplateUtil;
|
||||
private final AdminAuthenticationService adminAuthenticationService;
|
||||
|
||||
public Map<String, Object> audit(String username, String event, String from, String to, int page, int size) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
if (StringUtils.hasText(username)) body.put("username", username.trim());
|
||||
if (StringUtils.hasText(event)) body.put("event", event.trim());
|
||||
if (StringUtils.hasText(from)) body.put("from", from.trim());
|
||||
if (StringUtils.hasText(to)) body.put("to", to.trim());
|
||||
body.put("page", page);
|
||||
body.put("size", size);
|
||||
return post(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_AUDIT, MAP, body);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> locked() {
|
||||
return restTemplateUtil.getForEntity(url(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_LOCKED), LIST).getBody();
|
||||
}
|
||||
|
||||
public void unlock(String username) {
|
||||
String by = callerName();
|
||||
post(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_UNLOCK, MAP, Map.of("username", username, "by", by));
|
||||
log.info("login lock for {} cleared by {}", username, by);
|
||||
}
|
||||
|
||||
public Map<String, Object> config() {
|
||||
return restTemplateUtil.getForEntity(url(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_CONFIG), MAP).getBody();
|
||||
}
|
||||
|
||||
public Map<String, Object> saveConfig(Map<String, Object> config) {
|
||||
Map<String, Object> saved = post(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_CONFIG, MAP, config);
|
||||
log.info("lockout settings changed by {}", callerName());
|
||||
return saved;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> passwordRules() {
|
||||
return restTemplateUtil.getForEntity(url(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_PASSWORD_RULES), LIST).getBody();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> savePasswordRules(List<Map<String, Object>> rules) {
|
||||
List<Map<String, Object>> saved = post(RestApiSystemServiceConstants.API_REST_LOGIN_SECURITY_PASSWORD_RULES, LIST, rules);
|
||||
log.info("password rules changed by {}", callerName());
|
||||
return saved;
|
||||
}
|
||||
|
||||
private <T> T post(String api, ParameterizedTypeReference<T> type, Object body) {
|
||||
return upstream(() -> restTemplateUtil.postForEntity(url(api), type, body).getBody());
|
||||
}
|
||||
|
||||
private static <T> T upstream(Supplier<T> call) {
|
||||
try {
|
||||
return call.get();
|
||||
} catch (HttpClientErrorException.BadRequest e) {
|
||||
throw new CodedResponseStatusException(HttpStatus.BAD_REQUEST, reason(e), "INVALID_SETTING");
|
||||
}
|
||||
}
|
||||
|
||||
private static String reason(HttpClientErrorException e) {
|
||||
try {
|
||||
JsonNode message = JSON.readTree(e.getResponseBodyAsString()).get("message");
|
||||
if (message != null && message.isTextual() && StringUtils.hasText(message.asText())) return message.asText();
|
||||
} catch (Exception ignored) {
|
||||
// empty or unparsable body: fall through to the generic reason
|
||||
}
|
||||
return "Invalid request";
|
||||
}
|
||||
|
||||
private String callerName() {
|
||||
User caller = adminAuthenticationService.loadUserByToken(HttpReqRespUtils.getJwtToken());
|
||||
if (caller == null) throw new CodedResponseStatusException(HttpStatus.UNAUTHORIZED, "Not signed in", "UNAUTHORIZED");
|
||||
return caller.getUsername();
|
||||
}
|
||||
|
||||
private String url(String api) {
|
||||
return restTemplateUtil.getURLforApiService(api);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user