From 10ce85ef9eed15276534938c7a18c04cb18ca7f2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 15 Jan 2020 21:02:16 -0800 Subject: Clean login failure logging --- src/main/java/org/traccar/helper/LogAction.java | 18 ++++++------------ src/main/java/org/traccar/helper/ServletHelper.java | 13 ++++++------- 2 files changed, 12 insertions(+), 19 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/traccar/helper/LogAction.java b/src/main/java/org/traccar/helper/LogAction.java index 21fe4c3fa..16d55ec60 100644 --- a/src/main/java/org/traccar/helper/LogAction.java +++ b/src/main/java/org/traccar/helper/LogAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2020 Anton Tananaev (anton@traccar.org) * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -38,14 +38,13 @@ public final class LogAction { private static final String ACTION_LOGIN = "login"; private static final String ACTION_LOGOUT = "logout"; - private static final String ACTION_FAILED_LOGIN_NO_IP = "Failed Login Attempt. IP address: failed to retrieve"; private static final String ACTION_DEVICE_ACCUMULATORS = "resetDeviceAccumulators"; private static final String PATTERN_OBJECT = "user: %d, action: %s, object: %s, id: %d"; private static final String PATTERN_LINK = "user: %d, action: %s, owner: %s, id: %d, property: %s, id: %d"; private static final String PATTERN_LOGIN = "user: %d, action: %s"; - private static final String PATTERN_FAILED_LOGIN = "Failed Login Attempt. IP address: %s"; + private static final String PATTERN_LOGIN_FAILED = "login failed from: %s"; private static final String PATTERN_DEVICE_ACCUMULATORS = "user: %d, action: %s, deviceId: %d"; public static void create(long userId, BaseModel object) { @@ -76,18 +75,13 @@ public final class LogAction { logLoginAction(ACTION_LOGOUT, userId); } - public static void failedLogin(String ipAddress) { - - if (ipAddress == null || ipAddress.isEmpty()) { - LOGGER.info(ACTION_FAILED_LOGIN_NO_IP); - } else { - LOGGER.info(String.format( - PATTERN_FAILED_LOGIN, ipAddress)); + public static void failedLogin(String remoteAddress) { + if (remoteAddress == null || remoteAddress.isEmpty()) { + remoteAddress = "unknown"; } - + LOGGER.info(String.format(PATTERN_LOGIN_FAILED, remoteAddress)); } - public static void resetDeviceAccumulators(long userId, long deviceId) { LOGGER.info(String.format( PATTERN_DEVICE_ACCUMULATORS, userId, ACTION_DEVICE_ACCUMULATORS, deviceId)); diff --git a/src/main/java/org/traccar/helper/ServletHelper.java b/src/main/java/org/traccar/helper/ServletHelper.java index f951d17c0..e3481e249 100644 --- a/src/main/java/org/traccar/helper/ServletHelper.java +++ b/src/main/java/org/traccar/helper/ServletHelper.java @@ -5,24 +5,23 @@ import javax.servlet.http.HttpServletRequest; public final class ServletHelper { - private ServletHelper() { } public static String retrieveRemoteAddress(HttpServletRequest request) { if (request != null) { - String ipAddress = request.getHeader("X-FORWARDED-FOR"); + String remoteAddress = request.getHeader("X-FORWARDED-FOR"); - if (ipAddress != null && !ipAddress.isEmpty()) { - return ipAddress.substring(0, ipAddress.indexOf(",")); //Removes the additional data + if (remoteAddress != null && !remoteAddress.isEmpty()) { + return remoteAddress.substring(0, remoteAddress.indexOf(",")); // removes the additional data } else { - ipAddress = request.getRemoteAddr(); - return ipAddress; + remoteAddress = request.getRemoteAddr(); + return remoteAddress; } - } else { return null; } } + } -- cgit v1.2.3