Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.flexcodelabs'
version = '0.0.24'
version = '0.0.25'
description = 'Flextuma App'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class DataInitializer implements CommandLineRunner {

@Override
public void run(String... args) {
log.info("🚀🚀🚀 DataInitializer.run() called! 🚀🚀🚀");
log.info("🚀 FLEXTUMA: Application started - checking system data seeds...");
try {
log.info("🌱 FLEXTUMA: Calling seeder service...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class RequestLoggingFilter extends OncePerRequestFilter {

private static final Logger log = LoggerFactory.getLogger("FLEXTUMA");
private static final String USERNAME_KEY = "username";

@Override
protected boolean shouldNotFilterErrorDispatch() {
Expand Down Expand Up @@ -80,7 +81,7 @@ private void logRequest(HttpServletRequest request, HttpServletResponse response
String coloredMethod = logColor + request.getMethod() + reset;
String coloredUri = logColor + fullUri + reset;

org.slf4j.MDC.put("username", username);
org.slf4j.MDC.put(USERNAME_KEY, username);
try {
if (isError) {
log.error("{} {} {} {} {}ms - Status: {}", statusLog, userInfo, coloredMethod, coloredUri, duration,
Expand All @@ -90,15 +91,55 @@ private void logRequest(HttpServletRequest request, HttpServletResponse response
status);
}
} finally {
org.slf4j.MDC.remove("username");
org.slf4j.MDC.remove(USERNAME_KEY);
}
}

private String getUsername() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();

// Debug logging to understand the authentication context
if (auth != null) {
log.debug("Auth class: {}", auth.getClass().getSimpleName());
log.debug("Auth authenticated: {}", auth.isAuthenticated());
log.debug("Auth principal: {}", auth.getPrincipal());
log.debug("Auth name: {}", auth.getName());
log.debug("Auth details: {}", auth.getDetails());
} else {
log.debug("Authentication is null");
}

if (auth != null && auth.isAuthenticated() && !"anonymousUser".equals(auth.getPrincipal())) {
return auth.getName();
String username = auth.getName();
// Don't return "SYSTEM" for actual authenticated users
if (username != null && !username.trim().isEmpty() && !"SYSTEM".equalsIgnoreCase(username)) {
return username;
}
}

// For login requests, try to extract username from request parameters
HttpServletRequest request = getCurrentRequest();
if (request != null && request.getRequestURI().contains("/login")) {
String loginUsername = request.getParameter(USERNAME_KEY);
if (loginUsername != null && !loginUsername.trim().isEmpty()) {
return loginUsername;
}
}

return "SYSTEM";
}

private HttpServletRequest getCurrentRequest() {
// Try to get current request from RequestContextHolder
try {
org.springframework.web.context.request.RequestAttributes attrs = org.springframework.web.context.request.RequestContextHolder
.getRequestAttributes();
if (attrs instanceof org.springframework.web.context.request.ServletRequestAttributes servletRequestAttributes) {
return servletRequestAttributes.getRequest();
}
} catch (Exception e) {
// Ignore - can't get current request
}
return null;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ logging.config=classpath:logback-spring.xml
spring.output.ansi.enabled=ALWAYS
spring.devtools.restart.enabled=true

# Enable INFO logging to see startup and seeding
logging.level.com.flexcodelabs.flextuma=INFO
logging.level.org.springframework.boot.autoconfigure=INFO

spring.data.redis.host=${REDIS_HOST:redis}
spring.data.redis.port=${REDIS_PORT:6379}
spring.data.redis.repositories.enabled=false
Expand Down