[Minor] Cleaned up logging in BasicAuth and ServletRequestHelper

This commit is contained in:
Robert von Burg 2024-04-23 11:37:09 +02:00
parent cf4ae20d0b
commit 3edcec4d08
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
2 changed files with 88 additions and 28 deletions

View File

@ -7,13 +7,14 @@ import li.strolch.privilege.base.InvalidCredentialsException;
import li.strolch.privilege.model.Certificate;
import li.strolch.privilege.model.Usage;
import li.strolch.runtime.sessions.StrolchSessionHandler;
import li.strolch.utils.helper.ExceptionHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Base64;
import static java.nio.charset.StandardCharsets.UTF_8;
import static li.strolch.utils.helper.ExceptionHelper.getRootCause;
import static li.strolch.utils.helper.ExceptionHelper.getRootCauseExceptionMessage;
import static li.strolch.utils.helper.StringHelper.isEmpty;
public class BasicAuth {
@ -47,19 +48,26 @@ public class BasicAuth {
logger.error(e.getMessage());
throw new BasicAuthFailure(Response.Status.FORBIDDEN, "User is not authorized!", e);
} catch (Exception e) {
logger.error(e.getMessage());
Throwable rootCause = ExceptionHelper.getRootCause(e);
Response.Status status;
String msg;
Throwable rootCause = getRootCause(e);
if (rootCause instanceof StrolchNotAuthenticatedException
|| rootCause instanceof InvalidCredentialsException) {
logger.error(e.getMessage());
throw new BasicAuthFailure(Response.Status.UNAUTHORIZED, "Authentication failed", e);
status = Response.Status.UNAUTHORIZED;
msg = "Authentication failed";
} else if (rootCause instanceof StrolchAccessDeniedException) {
logger.error(e.getMessage());
throw new BasicAuthFailure(Response.Status.FORBIDDEN, "User is not authorized!", e);
status = Response.Status.FORBIDDEN;
msg = "User is not authorized!";
} else {
status = Response.Status.INTERNAL_SERVER_ERROR;
msg = "Internal error";
}
throw new BasicAuthFailure(Response.Status.INTERNAL_SERVER_ERROR, "Internal error", e);
if (status == Response.Status.INTERNAL_SERVER_ERROR)
logger.error(e.getMessage(), e);
else
logger.error("Basic Auth failed: {}", getRootCauseExceptionMessage(e));
throw new BasicAuthFailure(status, msg, e);
}
}
}

View File

@ -8,52 +8,104 @@ import org.slf4j.LoggerFactory;
import java.util.Enumeration;
import static li.strolch.utils.helper.StringHelper.*;
public class ServletRequestHelper {
private static final Logger logger = LoggerFactory.getLogger(ServletRequestHelper.class);
private static final int PADDING = 30;
private static final int PADDING_SHORT = 27;
public static void logRequest(HttpServletRequest request) {
if (!RestfulStrolchComponent.getInstance().isRestLogging())
return;
try {
StringBuilder sb = new StringBuilder();
sb.append("REQUEST: ").append(request.getMethod()).append(" ").append(request.getRequestURI()).append("\n");
sb
.append("From: ")
.append(request.getRemoteAddr())
.append("/")
.append(request.getRemoteHost())
.append("\n")
.append(pad("REQUEST URL:"))
.append(request.getMethod())
.append(" ")
.append(request.getRemotePort())
.append(request.getRequestURL())
.append("\n");
sb
.append(pad("REQUEST:"))
.append(request.getMethod())
.append(" ")
.append(request.getRequestURI())
.append("\n");
sb.append("AuthType: ").append(request.getAuthType()).append("\n");
sb.append(pad("QUERY:")).append('?').append(string(request.getQueryString())).append("\n");
Cookie[] cookies = request.getCookies();
if (cookies == null) {
sb.append("Cookies: NONE!\n");
} else {
sb.append("Cookies: \n");
for (Cookie cookie : cookies) {
sb.append(" ").append(cookie.getName()).append(" = ").append(cookie.getValue()).append("\n");
}
}
String from = request.getRemoteAddr();
sb.append(pad("REMOTE:")).append(from);
if (!from.equals(request.getRemoteHost()))
sb.append("/").append(request.getRemoteHost());
sb.append(":").append(request.getRemotePort()).append("\n");
Enumeration<String> headerNames = request.getHeaderNames();
if (!headerNames.hasMoreElements()) {
sb.append("Headers: NONE!\n");
sb.append("HEADERS: (none)!\n");
} else {
sb.append("Headers: \n");
sb.append("HEADERS: \n");
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
sb.append(" ").append(headerName).append(" = ").append(request.getHeader(headerName)).append("\n");
String headerValue = request.getHeader(headerName);
sb.append(" ").append(padShort(headerName)).append(" = ").append(headerValue).append("\n");
}
}
Cookie[] cookies = request.getCookies();
if (cookies == null) {
sb.append("COOKIES: (none)!\n");
} else {
sb.append("COOKIES: \n");
for (Cookie cookie : cookies) {
sb
.append(" ")
.append(padShort(cookie.getName()))
.append(" = ")
.append(cookie.getValue())
.append("\n");
}
}
sb.append(pad("AuthType:")).append(string(request.getAuthType())).append("\n");
sb.append(pad("User-Principal:")).append(string(request.getUserPrincipal())).append("\n");
sb.append(pad("Remote User:")).append(string(request.getRemoteUser())).append("\n");
sb.append(pad("Requested SessionID:")).append(string(request.getRequestedSessionId())).append("\n");
sb.append(pad("Protocol:")).append(string(request.getProtocol())).append("\n");
sb.append(pad("RequestId:")).append(string(request.getRequestId())).append("\n");
sb.append(pad("DispatcherType:")).append(string(request.getDispatcherType())).append("\n");
sb.append(pad("CharacterEncoding:")).append(string(request.getCharacterEncoding())).append("\n");
sb.append(pad("ContentType:")).append(string(request.getContentType())).append("\n");
sb.append(pad("ContentLength:")).append(request.getContentLengthLong()).append("\n");
logger.info(sb.toString());
} catch (Exception e) {
logger.error("Failed to log request", e);
}
}
private static String pad(String string) {
return normalizeLength(string, PADDING, false, ' ');
}
private static String padShort(String string) {
return normalizeLength(string, PADDING_SHORT, false, ' ');
}
private static String string(String string) {
return isEmpty(string) ? "(none)" : string;
}
private static String string(Enum<?> e) {
return e == null ? "(none)" : e.name();
}
private static String string(Object o) {
return o == null ? "(none)" : o.toString();
}
}