@@ -16,6 +16,7 @@ import org.springframework.security.core.Authentication; | |||||
import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||||
import java.time.temporal.ChronoUnit; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
@@ -47,21 +48,24 @@ public class AuthorizationEventListener { | |||||
Authentication authentication = event.getAuthentication(); | Authentication authentication = event.getAuthentication(); | ||||
UserInfoDetails userDetails = (UserInfoDetails) authentication.getPrincipal(); | UserInfoDetails userDetails = (UserInfoDetails) authentication.getPrincipal(); | ||||
WebRequestDetails webDetails = (WebRequestDetails) authentication.getDetails(); | 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; | |||||
} | } | ||||
} | } |
@@ -14,6 +14,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | |||||
import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
import java.time.LocalDateTime; | |||||
/** | /** | ||||
* @Author LiuXinXin | * @Author LiuXinXin | ||||
@@ -50,7 +51,7 @@ public class AgentAuthFilter extends AbstractAuthenticationProcessingFilter { | |||||
userId = trim(userId); | userId = trim(userId); | ||||
try { | try { | ||||
AgentAuthToken authRequest = new AgentAuthToken(userId, userId); | AgentAuthToken authRequest = new AgentAuthToken(userId, userId); | ||||
authRequest.setDetails(new WebRequestDetails(request)); | |||||
authRequest.setDetails(new WebRequestDetails(request, LocalDateTime.now())); | |||||
return this.getAuthenticationManager().authenticate(authRequest); | return this.getAuthenticationManager().authenticate(authRequest); | ||||
} catch (AuthenticationException e) { | } catch (AuthenticationException e) { | ||||
throw new BadCredentialsException("用户id 不能为空"); | throw new BadCredentialsException("用户id 不能为空"); | ||||
@@ -15,6 +15,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | |||||
import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
import java.time.LocalDateTime; | |||||
/** | /** | ||||
* @Author LiuXinXin | * @Author LiuXinXin | ||||
@@ -57,7 +58,7 @@ public class CommonAuthFilter extends AbstractAuthenticationProcessingFilter { | |||||
credential = trim(credential); | credential = trim(credential); | ||||
try { | try { | ||||
CommonAuthToken authRequest = new CommonAuthToken(platform, credential); | CommonAuthToken authRequest = new CommonAuthToken(platform, credential); | ||||
authRequest.setDetails(new WebRequestDetails(request)); | |||||
authRequest.setDetails(new WebRequestDetails(request, LocalDateTime.now())); | |||||
return this.getAuthenticationManager().authenticate(authRequest); | return this.getAuthenticationManager().authenticate(authRequest); | ||||
} catch (AuthenticationException e) { | } catch (AuthenticationException e) { | ||||
throw new BadCredentialsException("用户状态"); | throw new BadCredentialsException("用户状态"); | ||||
@@ -16,6 +16,7 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | |||||
import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
import javax.servlet.http.HttpServletResponse; | import javax.servlet.http.HttpServletResponse; | ||||
import java.time.LocalDateTime; | |||||
/** | /** | ||||
* @Author LiuXinXin | * @Author LiuXinXin | ||||
@@ -60,7 +61,7 @@ public class CredentialAuthFilter extends AbstractAuthenticationProcessingFilter | |||||
loginType = trim(loginType); | loginType = trim(loginType); | ||||
try { | try { | ||||
CredentialAuthToken authRequest = new CredentialAuthToken(identifier, credential, loginType); | CredentialAuthToken authRequest = new CredentialAuthToken(identifier, credential, loginType); | ||||
authRequest.setDetails(new WebRequestDetails(request)); | |||||
authRequest.setDetails(new WebRequestDetails(request, LocalDateTime.now())); | |||||
return this.getAuthenticationManager().authenticate(authRequest); | return this.getAuthenticationManager().authenticate(authRequest); | ||||
} catch (CommonLoginException e) { | } catch (CommonLoginException e) { | ||||
throw new CommonLoginException(e.getMessage()); | throw new CommonLoginException(e.getMessage()); | ||||
@@ -5,6 +5,7 @@ import cn.hutool.extra.servlet.ServletUtil; | |||||
import org.springframework.security.web.authentication.WebAuthenticationDetails; | import org.springframework.security.web.authentication.WebAuthenticationDetails; | ||||
import javax.servlet.http.HttpServletRequest; | import javax.servlet.http.HttpServletRequest; | ||||
import java.time.LocalDateTime; | |||||
/** | /** | ||||
* <p> | * <p> | ||||
@@ -18,6 +19,8 @@ public class WebRequestDetails extends WebAuthenticationDetails { | |||||
private static final long serialVersionUID = -4466339683132696235L; | private static final long serialVersionUID = -4466339683132696235L; | ||||
private LocalDateTime requestTime; | |||||
private final String requestIp; | private final String requestIp; | ||||
private final String requestUri; | private final String requestUri; | ||||
@@ -46,6 +49,15 @@ public class WebRequestDetails extends WebAuthenticationDetails { | |||||
this.userAgent = StrUtil.sub(request.getHeader("user-agent"), 0, 500); | 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() { | public String getRequestIp() { | ||||
return requestIp; | return requestIp; | ||||
} | } | ||||