modify audit logging
This commit is contained in:
parent
ec8b501fef
commit
b42fcc42d6
5 changed files with 63 additions and 29 deletions
|
@ -23,6 +23,7 @@ import org.codelibs.core.beans.util.BeanUtil;
|
|||
import org.codelibs.core.beans.util.CopyOptions;
|
||||
import org.codelibs.core.lang.StringUtil;
|
||||
import org.codelibs.fess.exception.UserRoleLoginException;
|
||||
import org.codelibs.fess.util.ActivityUtil;
|
||||
import org.dbflute.optional.OptionalThing;
|
||||
import org.lastaflute.di.util.LdiFileUtil;
|
||||
import org.lastaflute.web.login.LoginManager;
|
||||
|
@ -39,8 +40,6 @@ import org.slf4j.LoggerFactory;
|
|||
*/
|
||||
public abstract class FessAdminAction extends FessBaseAction {
|
||||
|
||||
private static final Logger auditLogger = LoggerFactory.getLogger("fess.log.audit");
|
||||
|
||||
// ===================================================================================
|
||||
// Attribute
|
||||
// =========
|
||||
|
@ -102,18 +101,10 @@ public abstract class FessAdminAction extends FessBaseAction {
|
|||
|
||||
@Override
|
||||
public ActionResponse hookBefore(ActionRuntime runtime) {
|
||||
final String client = LaRequestUtil.getOptionalRequest().map(req -> {
|
||||
final String value = req.getHeader("x-forwarded-for");
|
||||
if (StringUtil.isNotBlank(value)) {
|
||||
return value;
|
||||
} else {
|
||||
return req.getRemoteAddr();
|
||||
}
|
||||
}).orElse("-");
|
||||
final String username = getUserBean().map(u -> u.getUserId()).orElse("-");
|
||||
final String requestPath = runtime.getRequestPath();
|
||||
final String executeName = runtime.getExecuteMethod().getName();
|
||||
auditLogger.info("{} {} {} {}", client, username, requestPath, executeName);
|
||||
ActivityUtil.access(username, requestPath, executeName);
|
||||
return super.hookBefore(runtime);
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.codelibs.fess.app.web.login;
|
|||
import org.codelibs.fess.app.web.admin.dashboard.AdminDashboardAction;
|
||||
import org.codelibs.fess.app.web.base.FessSearchAction;
|
||||
import org.codelibs.fess.mylasta.action.FessUserBean;
|
||||
import org.codelibs.fess.util.ActivityUtil;
|
||||
import org.lastaflute.web.Execute;
|
||||
import org.lastaflute.web.login.exception.LoginFailureException;
|
||||
import org.lastaflute.web.response.HtmlResponse;
|
||||
|
@ -51,11 +52,12 @@ public class LoginAction extends FessSearchAction {
|
|||
form.clearSecurityInfo();
|
||||
return asHtml(path_Login_IndexJsp);
|
||||
});
|
||||
final String email = form.username;
|
||||
final String username = form.username;
|
||||
final String password = form.password;
|
||||
form.clearSecurityInfo();
|
||||
ActivityUtil.login(username);
|
||||
try {
|
||||
return fessLoginAssist.loginRedirect(email, password, op -> {}, () -> getHtmlResponse());
|
||||
return fessLoginAssist.loginRedirect(username, password, op -> {}, () -> getHtmlResponse());
|
||||
} catch (final LoginFailureException lfe) {
|
||||
throwValidationError(messages -> messages.addErrorsLoginError(GLOBAL), () -> {
|
||||
return asHtml(path_Login_IndexJsp);
|
||||
|
|
|
@ -17,6 +17,7 @@ package org.codelibs.fess.app.web.logout;
|
|||
|
||||
import org.codelibs.fess.app.web.base.FessSearchAction;
|
||||
import org.codelibs.fess.app.web.login.LoginAction;
|
||||
import org.codelibs.fess.util.ActivityUtil;
|
||||
import org.lastaflute.web.Execute;
|
||||
import org.lastaflute.web.response.HtmlResponse;
|
||||
|
||||
|
@ -40,6 +41,8 @@ public class LogoutAction extends FessSearchAction {
|
|||
|
||||
@Execute
|
||||
public HtmlResponse index() {
|
||||
final String username = getUserBean().map(u -> u.getUserId()).orElse("-");
|
||||
ActivityUtil.logout(username);
|
||||
fessLoginAssist.logout();
|
||||
return redirect(LoginAction.class);
|
||||
}
|
||||
|
|
|
@ -15,9 +15,14 @@
|
|||
*/
|
||||
package org.codelibs.fess.util;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.codelibs.core.lang.StringUtil;
|
||||
import org.lastaflute.web.util.LaRequestUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -26,30 +31,63 @@ import org.slf4j.LoggerFactory;
|
|||
*
|
||||
*/
|
||||
public class ActivityUtil {
|
||||
private static Logger logger = LoggerFactory.getLogger("activity");
|
||||
private static Logger logger = LoggerFactory.getLogger("fess.log.audit");
|
||||
|
||||
public static void login(final String username, final HttpServletRequest request) {
|
||||
log(Action.LOGIN, username + ' ' + getRemoteAddr(request));
|
||||
public static void login(final String username) {
|
||||
final StringBuilder buf = new StringBuilder(100);
|
||||
buf.append("action:");
|
||||
buf.append(Action.LOGIN);
|
||||
buf.append('\t');
|
||||
buf.append("user:");
|
||||
buf.append(username);
|
||||
log(buf);
|
||||
}
|
||||
|
||||
public static void logout(final String username, final HttpServletRequest request) {
|
||||
log(Action.LOGOUT, username + ' ' + getRemoteAddr(request));
|
||||
public static void logout(final String username) {
|
||||
final StringBuilder buf = new StringBuilder(100);
|
||||
buf.append("action:");
|
||||
buf.append(Action.LOGOUT);
|
||||
buf.append('\t');
|
||||
buf.append("user:");
|
||||
buf.append(username);
|
||||
log(buf);
|
||||
}
|
||||
|
||||
public static void access(final String username, final HttpServletRequest request) {
|
||||
log(Action.ACCESS, username + ' ' + getRemoteAddr(request) + ' ' + request.getRequestURL());
|
||||
public static void access(final String username, final String path, final String execute) {
|
||||
final StringBuilder buf = new StringBuilder(100);
|
||||
buf.append("action:");
|
||||
buf.append(Action.ACCESS);
|
||||
buf.append('\t');
|
||||
buf.append("user:");
|
||||
buf.append(username);
|
||||
buf.append('\t');
|
||||
buf.append("path:");
|
||||
buf.append(path);
|
||||
buf.append('\t');
|
||||
buf.append("execute:");
|
||||
buf.append(execute);
|
||||
log(buf);
|
||||
}
|
||||
|
||||
protected static void log(final Action action, final String msg) {
|
||||
logger.info("[" + action + "] " + msg);
|
||||
private static void log(final StringBuilder buf) {
|
||||
buf.append('\t');
|
||||
buf.append("ip:");
|
||||
buf.append(getClientIp());
|
||||
buf.append('\t');
|
||||
buf.append("time:");
|
||||
buf.append(DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now()));
|
||||
logger.info(buf.toString());
|
||||
}
|
||||
|
||||
protected static String getRemoteAddr(final HttpServletRequest request) {
|
||||
final String clientIp = request.getHeader("x-forwarded-for");
|
||||
if (StringUtil.isNotBlank(clientIp)) {
|
||||
return clientIp;
|
||||
}
|
||||
return request.getRemoteAddr();
|
||||
protected static String getClientIp() {
|
||||
return LaRequestUtil.getOptionalRequest().map(req -> {
|
||||
final String value = req.getHeader("x-forwarded-for");
|
||||
if (StringUtil.isNotBlank(value)) {
|
||||
return value;
|
||||
} else {
|
||||
return req.getRemoteAddr();
|
||||
}
|
||||
}).orElse("-");
|
||||
}
|
||||
|
||||
protected enum Action {
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<property name="log.file.basedir" value="${fess.log.path:-target/logs}" />
|
||||
<property name="backup.date.suffix" value="_%d{yyyyMMdd}" />
|
||||
<property name="backup.max.history" value="90" />
|
||||
<property name="audit.log.pattern" value="%d %msg%n" />
|
||||
<property name="audit.log.pattern" value="%msg%n" />
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
|
|
Loading…
Add table
Reference in a new issue