Fix issue#381: No IP address is recorded in log

pull/389/head
johnniang 2019-11-16 20:52:07 +08:00
parent 9dd02ee8c4
commit 07eea75e13
4 changed files with 19 additions and 1 deletions

View File

@ -1,10 +1,15 @@
package run.halo.app.event.logger; package run.halo.app.event.logger;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.ServletContextScope;
import run.halo.app.model.enums.LogType; import run.halo.app.model.enums.LogType;
import run.halo.app.model.params.LogParam; import run.halo.app.model.params.LogParam;
import run.halo.app.utils.ServletUtils;
import run.halo.app.utils.ValidationUtils; import run.halo.app.utils.ValidationUtils;
import javax.servlet.ServletContext;
/** /**
* @author johnniang * @author johnniang
* @date 19-4-20 * @date 19-4-20
@ -25,6 +30,9 @@ public class LogEvent extends ApplicationEvent {
// Validate the log param // Validate the log param
ValidationUtils.validate(logParam); ValidationUtils.validate(logParam);
// Set ip address
logParam.setIpAddress(ServletUtils.getRequestIp());
this.logParam = logParam; this.logParam = logParam;
} }

View File

@ -26,6 +26,7 @@ public class LogEventListener {
public void onApplicationEvent(LogEvent event) { public void onApplicationEvent(LogEvent event) {
// Convert to log // Convert to log
Log logToCreate = event.getLogParam().convertTo(); Log logToCreate = event.getLogParam().convertTo();
// Create log // Create log
logService.create(logToCreate); logService.create(logToCreate);
} }

View File

@ -60,7 +60,8 @@ public class Log extends BaseEntity {
} }
// Get ip address // Get ip address
ipAddress = ServletUtils.getRequestIp(); // ###!!! Do not get request IP from here due to asynchronous
// ipAddress = ServletUtils.getRequestIp();
if (ipAddress == null) { if (ipAddress == null) {
logKey = ""; logKey = "";

View File

@ -29,4 +29,12 @@ public class LogParam implements InputConverter<Log> {
@NotBlank(message = "Log content must not be blank") @NotBlank(message = "Log content must not be blank")
@Size(max = 1023, message = "Log content must not be more than 1023") @Size(max = 1023, message = "Log content must not be more than 1023")
private String content; private String content;
private String ipAddress;
public LogParam(String logKey, LogType type, String content) {
this.logKey = logKey;
this.type = type;
this.content = content;
}
} }