diff --git a/src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java b/src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java index 235d543061..ba59da634c 100644 --- a/src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java +++ b/src/main/java/ru/mystamps/web/support/spring/security/SecurityConfig.java @@ -203,4 +203,13 @@ public FilterRegistrationBean getResetLocaleFilter( return bean; } + @Bean + public FilterRegistrationBean userMdcLoggingFilter() { + FilterRegistrationBean bean = new FilterRegistrationBean( + new UserMdcLoggingFilter() + ); + bean.addUrlPatterns("/*"); + return bean; + } + } diff --git a/src/main/java/ru/mystamps/web/support/spring/security/UserMdcLoggingFilter.java b/src/main/java/ru/mystamps/web/support/spring/security/UserMdcLoggingFilter.java new file mode 100644 index 0000000000..724756302f --- /dev/null +++ b/src/main/java/ru/mystamps/web/support/spring/security/UserMdcLoggingFilter.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2009-2019 Slava Semushin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package ru.mystamps.web.support.spring.security; + +import org.slf4j.MDC; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class UserMdcLoggingFilter extends OncePerRequestFilter { + private static final String USER_ID = "userId"; + + @Override + protected void doFilterInternal( + HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain) + throws ServletException, IOException { + + Integer userId = SecurityContextUtils.getUserId(); + if (userId != null) { + MDC.put(USER_ID, userId.toString()); + } + + try { + filterChain.doFilter(request, response); + } finally { + MDC.remove(USER_ID); + } + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 516c32ae2d..327c1d81af 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -36,6 +36,11 @@ server.compression.min-response-size: 512 security.filter-dispatcher-types: REQUEST, ERROR +# See for details: +# https://docs.spring.io/spring-boot/docs/1.5.x/reference/html/boot-features-logging.html#boot-features-custom-log-configuration +# https://logback.qos.ch/manual/layouts.html +logging.pattern.level: [user:%-2X{userId}] %5p + app.mail.admin.email: slava.semushin@gmail.com app.mail.admin.lang: ru app.mail.robot.email: dont-reply@my-stamps.ru