diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2020-01-15 20:50:44 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-15 20:50:44 -0800 |
commit | 729021a19086d982c1b4ec5661d9140c42f030f8 (patch) | |
tree | a31c0870cc6e62b1f002d62f8c602ef124f173ab /src/main/java/org/traccar | |
parent | 50d8c39d41113a73fdae7a30971b4b39c35c5566 (diff) | |
parent | b62d3ba2a545a2f6b2963a2aa2c36b53abdfd6bc (diff) | |
download | trackermap-server-729021a19086d982c1b4ec5661d9140c42f030f8.tar.gz trackermap-server-729021a19086d982c1b4ec5661d9140c42f030f8.tar.bz2 trackermap-server-729021a19086d982c1b4ec5661d9140c42f030f8.zip |
Merge pull request #4460 from DeveloperKurt/failedLoginLogsIp-developerKurt
Failed Login Attempts logs IP Addresses
Diffstat (limited to 'src/main/java/org/traccar')
-rw-r--r-- | src/main/java/org/traccar/api/resource/SessionResource.java | 2 | ||||
-rw-r--r-- | src/main/java/org/traccar/helper/LogAction.java | 16 | ||||
-rw-r--r-- | src/main/java/org/traccar/helper/ServletHelper.java | 28 |
3 files changed, 45 insertions, 1 deletions
diff --git a/src/main/java/org/traccar/api/resource/SessionResource.java b/src/main/java/org/traccar/api/resource/SessionResource.java index fd331c766..e3c5d457f 100644 --- a/src/main/java/org/traccar/api/resource/SessionResource.java +++ b/src/main/java/org/traccar/api/resource/SessionResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.helper.DataConverter; +import org.traccar.helper.ServletHelper; import org.traccar.helper.LogAction; import org.traccar.model.User; @@ -106,6 +107,7 @@ public class SessionResource extends BaseResource { LogAction.login(user.getId()); return user; } else { + LogAction.failedLogin(ServletHelper.retrieveRemoteAddress(request)); throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).build()); } } diff --git a/src/main/java/org/traccar/helper/LogAction.java b/src/main/java/org/traccar/helper/LogAction.java index db13337b8..21fe4c3fa 100644 --- a/src/main/java/org/traccar/helper/LogAction.java +++ b/src/main/java/org/traccar/helper/LogAction.java @@ -38,12 +38,14 @@ 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_DEVICE_ACCUMULATORS = "user: %d, action: %s, deviceId: %d"; public static void create(long userId, BaseModel object) { @@ -74,6 +76,18 @@ 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 resetDeviceAccumulators(long userId, long deviceId) { LOGGER.info(String.format( PATTERN_DEVICE_ACCUMULATORS, userId, ACTION_DEVICE_ACCUMULATORS, deviceId)); @@ -85,7 +99,7 @@ public final class LogAction { } private static void logLinkAction(String action, long userId, - Class<?> owner, long ownerId, Class<?> property, long propertyId) { + Class<?> owner, long ownerId, Class<?> property, long propertyId) { LOGGER.info(String.format( PATTERN_LINK, userId, action, Introspector.decapitalize(owner.getSimpleName()), ownerId, diff --git a/src/main/java/org/traccar/helper/ServletHelper.java b/src/main/java/org/traccar/helper/ServletHelper.java new file mode 100644 index 000000000..f951d17c0 --- /dev/null +++ b/src/main/java/org/traccar/helper/ServletHelper.java @@ -0,0 +1,28 @@ +package org.traccar.helper; + +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"); + + if (ipAddress != null && !ipAddress.isEmpty()) { + return ipAddress.substring(0, ipAddress.indexOf(",")); //Removes the additional data + } else { + ipAddress = request.getRemoteAddr(); + return ipAddress; + } + + } else { + return null; + } + } +} |