浏览代码

增加登录成功监听事件

tags/24080901
WendyYang 1年前
父节点
当前提交
0ee95249f4
共有 5 个文件被更改,包括 37 次插入18 次删除
  1. +19
    -15
      pmapi/src/main/java/com/ningdatech/pmapi/user/controller/AuthorizationEventListener.java
  2. +2
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/agent/AgentAuthFilter.java
  3. +2
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/common/CommonAuthFilter.java
  4. +2
    -1
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/credential/CredentialAuthFilter.java
  5. +12
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/user/security/auth/model/WebRequestDetails.java

+ 19
- 15
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;

/**
* <p>
@@ -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;
}

}

+ 2
- 1
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 不能为空");


+ 2
- 1
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("用户状态");


+ 2
- 1
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());


+ 12
- 0
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;

/**
* <p>
@@ -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;
}


正在加载...
取消
保存