Browse Source

log 获取用户

zhangzl 1 week ago
parent
commit
1b8e7cf0bb

+ 60 - 45
farm-common/src/main/java/jp/yamoto/farm/common/security/filter/JwtAuthenticationTokenFilter.java

@@ -1,46 +1,61 @@
-package jp.yamoto.farm.common.security.filter;
-
-import jakarta.servlet.FilterChain;
-import jakarta.servlet.ServletException;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-import jp.yamoto.farm.common.core.domain.model.LoginUser;
-import jp.yamoto.farm.common.utils.SecurityUtils;
-import jp.yamoto.farm.common.utils.StringUtils;
-import jp.yamoto.farm.common.core.service.TokenService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
-import org.springframework.security.core.context.SecurityContextHolder;
-import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
-import org.springframework.stereotype.Component;
-import org.springframework.web.filter.OncePerRequestFilter;
-
-import java.io.IOException;
-
-/**
- * tokenフィルタtokenの有効性を検証する
- * 
- * @author nextosd
- */
-@Component
-public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
-{
-    @Autowired
-    private TokenService tokenService;
-
-    @Override
-    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
-            throws ServletException, IOException
-    {
-        LoginUser loginUser = tokenService.getLoginUser(request);
-        if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
-        {
-            tokenService.verifyToken(loginUser);
-
-            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
-            authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
-            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
-        }
-        chain.doFilter(request, response);
-    }
+package jp.yamoto.farm.common.security.filter;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import jp.yamoto.farm.common.core.domain.model.LoginUser;
+import jp.yamoto.farm.common.utils.SecurityUtils;
+import jp.yamoto.farm.common.utils.StringUtils;
+import jp.yamoto.farm.common.core.service.TokenService;
+import org.slf4j.MDC;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import java.io.IOException;
+
+/**
+ * tokenフィルタtokenの有効性を検証する
+ * 
+ * @author nextosd
+ */
+@Component
+public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
+{
+    @Autowired
+    private TokenService tokenService;
+
+    @Override
+    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
+            throws ServletException, IOException
+    {
+        try
+        {
+            LoginUser loginUser = tokenService.getLoginUser(request);
+            if (StringUtils.isNotNull(loginUser))
+            {
+                // MDCにuserIdを設定
+                MDC.put("userId", loginUser.getUsername());
+                
+                if (StringUtils.isNull(SecurityUtils.getAuthentication()))
+                {
+                    tokenService.verifyToken(loginUser);
+                    
+                    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
+                    authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
+                    SecurityContextHolder.getContext().setAuthentication(authenticationToken);
+                }
+            }
+            chain.doFilter(request, response);
+        }
+        finally
+        {
+            // リクエスト処理が完了したらMDCからuserIdを削除
+            MDC.remove("userId");
+        }
+    }
 }

+ 1 - 1
farm-crm/src/main/resources/logback.xml

@@ -3,7 +3,7 @@
     <!-- ログ・ステージング・パス -->
 	<property name="log.path" value="logs" />
     <!-- ログ出力フォーマット -->
-	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
+	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] [%X{userId:-anonymous}] %-5level %logger{20} - [%method,%line] - %msg%n" />
 
 	<!-- コンソール出力 -->
 	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">

+ 1 - 1
farm-sankin/src/main/resources/logback.xml

@@ -3,7 +3,7 @@
     <!-- ログ・ステージング・パス -->
 	<property name="log.path" value="logs" />
     <!-- ログ出力フォーマット -->
-	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
+	<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] [%X{userId:-anonymous}] %-5level %logger{20} - [%method,%line] - %msg%n" />
 
 	<!-- コンソール出力 -->
 	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">