Commit 3dfd935e by xieshaohua

feat: add loginout log

parent 2b274570
package com.keymobile.sso.conf; package com.keymobile.sso.conf;
import com.keymobile.sso.logging.LogConstants;
import com.keymobile.sso.logging.LogManager; import com.keymobile.sso.logging.LogManager;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -17,11 +16,15 @@ import java.io.IOException; ...@@ -17,11 +16,15 @@ import java.io.IOException;
public class RESTLogoutSuccessHandler implements LogoutSuccessHandler { public class RESTLogoutSuccessHandler implements LogoutSuccessHandler {
@Override @Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
HttpSession session = request.getSession(false);
if (authentication != null && authentication.getName() != null) { if (authentication != null && authentication.getName() != null) {
LogManager.logInfo(LogConstants.CTX_AUDIT, "User logged out: " + authentication.getName()); LogManager.logLogout(authentication.getName(), session);
}
if (session != null) {
session.invalidate();
} }
request.getSession().invalidate();
response.setStatus(HttpStatus.OK.value()); response.setStatus(HttpStatus.OK.value());
response.getWriter().flush(); response.getWriter().flush();
} }
......
...@@ -55,16 +55,15 @@ public class LogManager { ...@@ -55,16 +55,15 @@ public class LogManager {
} }
public static void logLogin(String userNameWithIdAttached, HttpSession session) { public static void logLogin(String userNameWithIdAttached, HttpSession session) {
String[] parts = userNameWithIdAttached.split(":"); putUserSessionMdc(userNameWithIdAttached, session);
String userName = parts[0];
String userDName = parts.length > 2 ? parts[2] : "";
if (session != null) {
MDC.put("user", getLogUserInfo(userName, userDName));
MDC.put("session", session.getId());
}
logInfo(LogConstants.SSO_API, "登录"); logInfo(LogConstants.SSO_API, "登录");
} }
public static void logLogout(String userNameWithIdAttached, HttpSession session) {
putUserSessionMdc(userNameWithIdAttached, session);
logInfo(LogConstants.SSO_API, "登出");
}
public static String getLogUserInfo(String userName, String userDName) { public static String getLogUserInfo(String userName, String userDName) {
if (userDName == null || userDName.isEmpty()) { if (userDName == null || userDName.isEmpty()) {
return userName; return userName;
...@@ -72,5 +71,17 @@ public class LogManager { ...@@ -72,5 +71,17 @@ public class LogManager {
return userName + "(" + userDName + ")"; return userName + "(" + userDName + ")";
} }
private static void putUserSessionMdc(String userNameWithIdAttached, HttpSession session) {
if (userNameWithIdAttached == null) {
return;
}
String[] parts = userNameWithIdAttached.split(":");
String userName = parts[0];
String userDName = parts.length > 2 ? parts[2] : "";
if (session != null) {
MDC.put("user", getLogUserInfo(userName, userDName));
MDC.put("session", session.getId());
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment