From 08fefe5916471c34b440131e76a05d1bc7332c56 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 5 Dec 2023 16:20:09 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D1=81=D0=B5=D1=81=D1=81=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../giprolab/config/MvcConfiguration.java | 22 ++++++++++ .../giprolab/services/IpAddressResolver.java | 22 ++++++++++ .../services/UserSessionLoginHandler.java | 41 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/main/java/com/gipro/giprolab/config/MvcConfiguration.java create mode 100644 src/main/java/com/gipro/giprolab/services/IpAddressResolver.java create mode 100644 src/main/java/com/gipro/giprolab/services/UserSessionLoginHandler.java diff --git a/src/main/java/com/gipro/giprolab/config/MvcConfiguration.java b/src/main/java/com/gipro/giprolab/config/MvcConfiguration.java new file mode 100644 index 0000000..4f90a49 --- /dev/null +++ b/src/main/java/com/gipro/giprolab/config/MvcConfiguration.java @@ -0,0 +1,22 @@ +package com.gipro.giprolab.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class MvcConfiguration implements WebMvcConfigurer { + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/{articlename:\\w+}"); + registry.addRedirectViewController("/", "/home"); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry + .addResourceHandler("/webjars/**") + .addResourceLocations("/webjars/"); + } +} diff --git a/src/main/java/com/gipro/giprolab/services/IpAddressResolver.java b/src/main/java/com/gipro/giprolab/services/IpAddressResolver.java new file mode 100644 index 0000000..f3e8f71 --- /dev/null +++ b/src/main/java/com/gipro/giprolab/services/IpAddressResolver.java @@ -0,0 +1,22 @@ +package com.gipro.giprolab.services; + +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.util.StringUtils; + +public final class IpAddressResolver { + private static final String CLIENT_IP_HEADER = "Client-IP"; + private static final String FORWARDED_FOR_HEADER = "X-Forwarded-For"; + + public static String getRemoteAddr(HttpServletRequest request) { + String headerClientIp = request.getHeader(""); + String headerXForwardedFor = request.getHeader(HttpServletRequest.FORM_AUTH); + if (StringUtils.isEmpty(request.getRemoteAddr()) && !StringUtils.isEmpty(headerClientIp)) { + return headerClientIp; + } + if (!StringUtils.isEmpty(headerXForwardedFor)) { + return headerXForwardedFor; + } + return request.getRemoteAddr(); + } + +} diff --git a/src/main/java/com/gipro/giprolab/services/UserSessionLoginHandler.java b/src/main/java/com/gipro/giprolab/services/UserSessionLoginHandler.java new file mode 100644 index 0000000..6d77002 --- /dev/null +++ b/src/main/java/com/gipro/giprolab/services/UserSessionLoginHandler.java @@ -0,0 +1,41 @@ +package com.gipro.giprolab.services; + +import com.gipro.giprolab.config.Constants; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class UserSessionLoginHandler extends SavedRequestAwareAuthenticationSuccessHandler implements AuthenticationSuccessHandler { + private final Logger log = LoggerFactory.getLogger(UserSessionLoginHandler.class); + + public UserSessionLoginHandler() { + super(); + } + + @Override + public void onAuthenticationSuccess(HttpServletRequest request, + HttpServletResponse response, + Authentication authentication) throws IOException, ServletException { + super.onAuthenticationSuccess(request, response, authentication); + final String login = authentication.getName(); + final String ipAddress = IpAddressResolver.getRemoteAddr(request); + final String host = request.getRemoteHost(); + log.debug("Authentication Success for {}@{} ({})", login, ipAddress, host); + HttpSession session = request.getSession(false); + if (session != null) { + final String sessionId = session.getId(); + session.setAttribute(Constants.SESSION_ID_ATTR, sessionId); + session.setMaxInactiveInterval(Constants.SESSION_TIMEOUT_SECONDS); + } + } +}