From e245296c7ed50829288c985ac69e08fe116e23ee Mon Sep 17 00:00:00 2001 From: dqjdda <201507802@qq.com> Date: Sat, 26 Oct 2019 19:56:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E5=8A=A0=E5=85=A5=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eladmin-common/pom.xml | 1 - .../java/me/zhengjie/utils/StringUtils.java | 8 ++++ .../java/me/zhengjie/aspect/LogAspect.java | 8 +++- .../src/main/java/me/zhengjie/domain/Log.java | 2 + .../java/me/zhengjie/service/LogService.java | 2 +- .../me/zhengjie/service/dto/LogErrorDTO.java | 2 + .../me/zhengjie/service/dto/LogSmallDTO.java | 2 + .../zhengjie/service/impl/LogServiceImpl.java | 3 +- .../modules/monitor/domain/vo/OnlineUser.java | 24 ++++++++++++ .../rest/AuthenticationController.java | 8 ++-- ...{AuthenticationInfo.java => AuthInfo.java} | 38 +++++++++---------- .../{AuthorizationUser.java => AuthUser.java} | 2 +- .../security/JwtAuthorizationTokenFilter.java | 1 - pom.xml | 5 +++ 14 files changed, 76 insertions(+), 30 deletions(-) create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/monitor/domain/vo/OnlineUser.java rename eladmin-system/src/main/java/me/zhengjie/modules/security/security/{AuthenticationInfo.java => AuthInfo.java} (79%) rename eladmin-system/src/main/java/me/zhengjie/modules/security/security/{AuthorizationUser.java => AuthUser.java} (93%) diff --git a/eladmin-common/pom.xml b/eladmin-common/pom.xml index af94c4df..3424108f 100644 --- a/eladmin-common/pom.xml +++ b/eladmin-common/pom.xml @@ -11,5 +11,4 @@ eladmin-common 公共模块 - \ No newline at end of file diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java b/eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java index 264fee0e..8f865f2e 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java @@ -1,6 +1,8 @@ package me.zhengjie.utils; import cn.hutool.core.io.resource.ClassPathResource; +import eu.bitwalker.useragentutils.Browser; +import eu.bitwalker.useragentutils.UserAgent; import org.lionsoul.ip2region.DataBlock; import org.lionsoul.ip2region.DbConfig; import org.lionsoul.ip2region.DbSearcher; @@ -157,6 +159,12 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils { return ""; } + public static String getBrowser(HttpServletRequest request){ + UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent")); + Browser browser = userAgent.getBrowser(); + return browser.getName(); + } + /** * 获得当天是周几 */ diff --git a/eladmin-logging/src/main/java/me/zhengjie/aspect/LogAspect.java b/eladmin-logging/src/main/java/me/zhengjie/aspect/LogAspect.java index 9f6d4912..a212ddf4 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/aspect/LogAspect.java +++ b/eladmin-logging/src/main/java/me/zhengjie/aspect/LogAspect.java @@ -15,6 +15,8 @@ import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; +import javax.servlet.http.HttpServletRequest; + /** * @author Zheng Jie * @date 2018-11-24 @@ -51,7 +53,8 @@ public class LogAspect { currentTime = System.currentTimeMillis(); result = joinPoint.proceed(); Log log = new Log("INFO",System.currentTimeMillis() - currentTime); - logService.save(getUsername(), StringUtils.getIp(RequestHolder.getHttpServletRequest()),joinPoint, log); + HttpServletRequest request = RequestHolder.getHttpServletRequest(); + logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request),joinPoint, log); return result; } @@ -65,7 +68,8 @@ public class LogAspect { public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { Log log = new Log("ERROR",System.currentTimeMillis() - currentTime); log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes()); - logService.save(getUsername(), StringUtils.getIp(RequestHolder.getHttpServletRequest()), (ProceedingJoinPoint)joinPoint, log); + HttpServletRequest request = RequestHolder.getHttpServletRequest(); + logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint)joinPoint, log); } public String getUsername() { diff --git a/eladmin-logging/src/main/java/me/zhengjie/domain/Log.java b/eladmin-logging/src/main/java/me/zhengjie/domain/Log.java index 564ab941..a977fee9 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/domain/Log.java +++ b/eladmin-logging/src/main/java/me/zhengjie/domain/Log.java @@ -45,6 +45,8 @@ public class Log implements Serializable { @Column(name = "address") private String address; + private String browser; + // 请求耗时 private Long time; diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java b/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java index b5e52732..7c05812c 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/LogService.java @@ -17,7 +17,7 @@ public interface LogService { Object queryAllByUser(LogQueryCriteria criteria, Pageable pageable); @Async - void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log); + void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log); /** * 查询异常详情 diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogErrorDTO.java b/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogErrorDTO.java index 7aaa54b2..1548ef00 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogErrorDTO.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogErrorDTO.java @@ -25,6 +25,8 @@ public class LogErrorDTO implements Serializable { // 参数 private String params; + private String browser; + // 请求ip private String requestIp; diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogSmallDTO.java b/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogSmallDTO.java index 5d11c347..22e41fb2 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogSmallDTO.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/dto/LogSmallDTO.java @@ -22,6 +22,8 @@ public class LogSmallDTO implements Serializable { private String address; + private String browser; + // 创建日期 private Timestamp createTime; } diff --git a/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java b/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java index 66bff12f..8a1158c6 100644 --- a/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java +++ b/eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java @@ -57,7 +57,7 @@ public class LogServiceImpl implements LogService { @Override @Transactional(rollbackFor = Exception.class) - public void save(String username, String ip, ProceedingJoinPoint joinPoint, Log log){ + public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); @@ -96,6 +96,7 @@ public class LogServiceImpl implements LogService { log.setMethod(methodName); log.setUsername(username); log.setParams(params.toString() + " }"); + log.setBrowser(browser); logRepository.save(log); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/monitor/domain/vo/OnlineUser.java b/eladmin-system/src/main/java/me/zhengjie/modules/monitor/domain/vo/OnlineUser.java new file mode 100644 index 00000000..ae4fb2ca --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/monitor/domain/vo/OnlineUser.java @@ -0,0 +1,24 @@ +package me.zhengjie.modules.monitor.domain.vo; + +import lombok.Data; +import java.sql.Timestamp; +import java.util.Date; + +/** + * @author Zheng Jie + */ +@Data +public class OnlineUser { + + private String userName; + + private String browser; + + private String ip; + + private String address; + + private Date createTime; + + private Date lastAccessTime; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/rest/AuthenticationController.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/rest/AuthenticationController.java index d27e1fd5..b62329c2 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/rest/AuthenticationController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/rest/AuthenticationController.java @@ -8,8 +8,8 @@ import lombok.extern.slf4j.Slf4j; import me.zhengjie.aop.log.Log; import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.monitor.service.RedisService; -import me.zhengjie.modules.security.security.AuthenticationInfo; -import me.zhengjie.modules.security.security.AuthorizationUser; +import me.zhengjie.modules.security.security.AuthInfo; +import me.zhengjie.modules.security.security.AuthUser; import me.zhengjie.modules.security.security.ImgResult; import me.zhengjie.modules.security.security.JwtUser; import me.zhengjie.utils.EncryptUtils; @@ -53,7 +53,7 @@ public class AuthenticationController { @Log("用户登录") @ApiOperation("登录授权") @PostMapping(value = "/login") - public ResponseEntity login(@Validated @RequestBody AuthorizationUser authorizationUser){ + public ResponseEntity login(@Validated @RequestBody AuthUser authorizationUser){ // 查询验证码 String code = redisService.getCodeVal(authorizationUser.getUuid()); @@ -79,7 +79,7 @@ public class AuthenticationController { final String token = jwtTokenUtil.generateToken(jwtUser); // 返回 token - return ResponseEntity.ok(new AuthenticationInfo(token,jwtUser)); + return ResponseEntity.ok(new AuthInfo(token,jwtUser)); } @ApiOperation("获取用户信息") diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthenticationInfo.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthInfo.java similarity index 79% rename from eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthenticationInfo.java rename to eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthInfo.java index 65fa7232..94bb2ab6 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthenticationInfo.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthInfo.java @@ -1,19 +1,19 @@ -package me.zhengjie.modules.security.security; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import java.io.Serializable; - -/** - * @author Zheng Jie - * @date 2018-11-23 - * 返回token - */ -@Getter -@AllArgsConstructor -public class AuthenticationInfo implements Serializable { - - private final String token; - - private final JwtUser user; -} +package me.zhengjie.modules.security.security; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import java.io.Serializable; + +/** + * @author Zheng Jie + * @date 2018-11-23 + * 返回token + */ +@Getter +@AllArgsConstructor +public class AuthInfo implements Serializable { + + private final String token; + + private final JwtUser user; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthorizationUser.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthUser.java similarity index 93% rename from eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthorizationUser.java rename to eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthUser.java index 1b93aca5..8c3a9608 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthorizationUser.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/security/AuthUser.java @@ -11,7 +11,7 @@ import javax.validation.constraints.NotBlank; */ @Getter @Setter -public class AuthorizationUser { +public class AuthUser { @NotBlank private String username; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/security/security/JwtAuthorizationTokenFilter.java b/eladmin-system/src/main/java/me/zhengjie/modules/security/security/JwtAuthorizationTokenFilter.java index 1693bcc1..99a8dbf5 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/security/security/JwtAuthorizationTokenFilter.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/security/security/JwtAuthorizationTokenFilter.java @@ -7,7 +7,6 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; import org.springframework.stereotype.Component; diff --git a/pom.xml b/pom.xml index 87bd2062..4d91d796 100644 --- a/pom.xml +++ b/pom.xml @@ -200,6 +200,11 @@ easy-captcha 1.6.2 + + eu.bitwalker + UserAgentUtils + 1.20 +