From 0ee95249f4c31ba78d809c537e74d2ee1a067ff4 Mon Sep 17 00:00:00 2001 From: WendyYang Date: Thu, 8 Jun 2023 09:19:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BB=E5=BD=95=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuthorizationEventListener.java | 34 ++++++++++++---------- .../user/security/auth/agent/AgentAuthFilter.java | 3 +- .../security/auth/common/CommonAuthFilter.java | 3 +- .../auth/credential/CredentialAuthFilter.java | 3 +- .../security/auth/model/WebRequestDetails.java | 12 ++++++++ 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/AuthorizationEventListener.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/AuthorizationEventListener.java index 1816a86..09b1527 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/AuthorizationEventListener.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/AuthorizationEventListener.java @@ -16,6 +16,7 @@ import org.springframework.security.core.Authentication; import org.springframework.stereotype.Component; import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; /** *

@@ -47,21 +48,24 @@ public class AuthorizationEventListener { Authentication authentication = event.getAuthentication(); UserInfoDetails userDetails = (UserInfoDetails) authentication.getPrincipal(); WebRequestDetails webDetails = (WebRequestDetails) authentication.getDetails(); - OptLogDTO log = new OptLogDTO(); - log.setActionMethod(webDetails.getServletPath()); - log.setDescription(description); - log.setStartTime(LocalDateTime.now()); - log.setFinishTime(log.getStartTime()); - log.setCreateOn(log.getStartTime()); - log.setHttpMethod(webDetails.getMethod()); - log.setUserName(userDetails.getUsername()); - log.setCreateBy(userDetails.getUserId()); - log.setRequestIp(webDetails.getRequestIp()); - log.setRequestUri(webDetails.getRequestUri()); - log.setRegionByIp(AddressUtil.getRegion(log.getRequestIp())); - log.setUa(webDetails.getUserAgent()); - log.setType(LogType.OPT.name()); - return log; + LocalDateTime now = LocalDateTime.now(); + OptLogDTO optLog = new OptLogDTO(); + optLog.setActionMethod(webDetails.getServletPath()); + optLog.setDescription(description); + optLog.setStartTime(webDetails.getRequestTime()); + optLog.setFinishTime(now); + optLog.setCreateOn(now); + long consumingTime = ChronoUnit.MILLIS.between(optLog.getStartTime(), optLog.getFinishTime()); + optLog.setConsumingTime(consumingTime); + optLog.setHttpMethod(webDetails.getMethod()); + optLog.setUserName(userDetails.getUsername()); + optLog.setCreateBy(userDetails.getUserId()); + optLog.setRequestIp(webDetails.getRequestIp()); + optLog.setRequestUri(webDetails.getRequestUri()); + optLog.setRegionByIp(AddressUtil.getRegion(optLog.getRequestIp())); + optLog.setUa(webDetails.getUserAgent()); + optLog.setType(LogType.OPT.name()); + return optLog; } } \ No newline at end of file diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/agent/AgentAuthFilter.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/agent/AgentAuthFilter.java index 0ab904f..96658b2 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/agent/AgentAuthFilter.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/agent/AgentAuthFilter.java @@ -14,6 +14,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.time.LocalDateTime; /** * @Author LiuXinXin @@ -50,7 +51,7 @@ public class AgentAuthFilter extends AbstractAuthenticationProcessingFilter { userId = trim(userId); try { AgentAuthToken authRequest = new AgentAuthToken(userId, userId); - authRequest.setDetails(new WebRequestDetails(request)); + authRequest.setDetails(new WebRequestDetails(request, LocalDateTime.now())); return this.getAuthenticationManager().authenticate(authRequest); } catch (AuthenticationException e) { throw new BadCredentialsException("用户id 不能为空"); diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/common/CommonAuthFilter.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/common/CommonAuthFilter.java index e06fbe9..1934f78 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/common/CommonAuthFilter.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/common/CommonAuthFilter.java @@ -15,6 +15,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.time.LocalDateTime; /** * @Author LiuXinXin @@ -57,7 +58,7 @@ public class CommonAuthFilter extends AbstractAuthenticationProcessingFilter { credential = trim(credential); try { CommonAuthToken authRequest = new CommonAuthToken(platform, credential); - authRequest.setDetails(new WebRequestDetails(request)); + authRequest.setDetails(new WebRequestDetails(request, LocalDateTime.now())); return this.getAuthenticationManager().authenticate(authRequest); } catch (AuthenticationException e) { throw new BadCredentialsException("用户状态"); diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialAuthFilter.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialAuthFilter.java index 05c1dde..3084988 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialAuthFilter.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialAuthFilter.java @@ -16,6 +16,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.time.LocalDateTime; /** * @Author LiuXinXin @@ -60,7 +61,7 @@ public class CredentialAuthFilter extends AbstractAuthenticationProcessingFilter loginType = trim(loginType); try { CredentialAuthToken authRequest = new CredentialAuthToken(identifier, credential, loginType); - authRequest.setDetails(new WebRequestDetails(request)); + authRequest.setDetails(new WebRequestDetails(request, LocalDateTime.now())); return this.getAuthenticationManager().authenticate(authRequest); } catch (CommonLoginException e) { throw new CommonLoginException(e.getMessage()); diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/model/WebRequestDetails.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/model/WebRequestDetails.java index 9c73060..d672e40 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/model/WebRequestDetails.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/model/WebRequestDetails.java @@ -5,6 +5,7 @@ import cn.hutool.extra.servlet.ServletUtil; import org.springframework.security.web.authentication.WebAuthenticationDetails; import javax.servlet.http.HttpServletRequest; +import java.time.LocalDateTime; /** *

@@ -18,6 +19,8 @@ public class WebRequestDetails extends WebAuthenticationDetails { private static final long serialVersionUID = -4466339683132696235L; + private LocalDateTime requestTime; + private final String requestIp; private final String requestUri; @@ -46,6 +49,15 @@ public class WebRequestDetails extends WebAuthenticationDetails { this.userAgent = StrUtil.sub(request.getHeader("user-agent"), 0, 500); } + public WebRequestDetails(HttpServletRequest request, LocalDateTime requestTime) { + this(request); + this.requestTime = requestTime; + } + + public LocalDateTime getRequestTime() { + return requestTime; + } + public String getRequestIp() { return requestIp; }