diff --git a/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java b/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java index 2f9d592d..0d0d8985 100644 --- a/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java +++ b/src/main/java/cn/lihongjie/coal/aop/ControllerAop.java @@ -2,6 +2,7 @@ package cn.lihongjie.coal.aop; import cn.lihongjie.coal.annotation.SysLog; import cn.lihongjie.coal.common.RequestUtils; +import cn.lihongjie.coal.ip.IpQueryService; import cn.lihongjie.coal.syslog.entity.SysLogEntity; import cn.lihongjie.coal.session.SessionService; import cn.lihongjie.coal.syslog.service.SysLogService; @@ -93,7 +94,9 @@ public class ControllerAop { } } - private static SysLogEntity createSysLog(Method method, HttpServletRequest request) { + @Autowired + IpQueryService ipQueryService; + private SysLogEntity createSysLog(Method method, HttpServletRequest request) { SysLog sysLog = AnnotationUtils.findAnnotation(method, SysLog.class); SysLogEntity sysLogEntity = null; @@ -116,7 +119,7 @@ public class ControllerAop { sysLogEntity.setMessage(sysLog.message()); sysLogEntity.setAction(sysLog.action()); sysLogEntity.setIp(RequestUtils.getIp(request)); - sysLogEntity.setIpLocation(RequestUtils.getIp(request)); + sysLogEntity.setIpLocation(ipQueryService.query(sysLogEntity.getIp())); sysLogEntity.setUrl(request.getRequestURI()); sysLogEntity.setOptStatus("0"); sysLogEntity.setTimeCost(0); diff --git a/src/main/java/cn/lihongjie/coal/ip/IpQueryService.java b/src/main/java/cn/lihongjie/coal/ip/IpQueryService.java index f9c0ec5d..2f20edcf 100644 --- a/src/main/java/cn/lihongjie/coal/ip/IpQueryService.java +++ b/src/main/java/cn/lihongjie/coal/ip/IpQueryService.java @@ -1,6 +1,7 @@ package cn.lihongjie.coal.ip; import com.maxmind.geoip2.DatabaseReader; +import com.maxmind.geoip2.model.CityResponse; import jakarta.annotation.PostConstruct; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -122,14 +123,18 @@ public class IpQueryService { @SneakyThrows - public Object query(String ipaddr) { + public String query(String ipaddr) { if (reader != null) { InetAddress ipAddress = InetAddress.getByName(ipaddr); + boolean siteLocalAddress = ipAddress.isSiteLocalAddress(); + if (siteLocalAddress) { + return "内网地址"; + } - - return reader.city(ipAddress); + CityResponse city = reader.city(ipAddress); + return city.getCountry().getName() + city.getCity().getName(); } diff --git a/src/main/java/cn/lihongjie/coal/session/SessionService.java b/src/main/java/cn/lihongjie/coal/session/SessionService.java index 5fc40d77..0bce1a21 100644 --- a/src/main/java/cn/lihongjie/coal/session/SessionService.java +++ b/src/main/java/cn/lihongjie/coal/session/SessionService.java @@ -86,16 +86,19 @@ public class SessionService { public void login(LoginDto dto) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); - String captchaId = dto.getCaptchaId(); + if (sysConfigService.isEnable(Constants.SYSCONFIG_ENABLE_CAPTCHA)) { - String expectCaptcha = stringRedisTemplate.opsForValue().get(captchaId); + String captchaId = dto.getCaptchaId(); - if (expectCaptcha == null) { - throw new BizException("验证码已失效"); - } + String expectCaptcha = stringRedisTemplate.opsForValue().get(captchaId); - if (!StringUtils.equals(expectCaptcha, dto.getCaptcha())) { - throw new BizException("验证码错误"); + if (expectCaptcha == null) { + throw new BizException("验证码已失效"); + } + + if (!StringUtils.equals(expectCaptcha, dto.getCaptcha())) { + throw new BizException("验证码错误"); + } } @@ -108,7 +111,7 @@ public class SessionService { OrganizationEntity organization = organizationService.get(user.getOrganizationId()); if (organization.isDisabled()) { - throw new BizException("用户所在单位被禁用"); + throw new BizException("用户所在机构被禁用"); } if (!userService.isValidPassword(dto.getPassword(), user.getPassword())) {