添加ip地址定位

This commit is contained in:
2023-09-10 22:18:25 +08:00
parent 74b9cdd8dd
commit c2bef856f4
3 changed files with 24 additions and 13 deletions

View File

@@ -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);

View File

@@ -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();
}

View File

@@ -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())) {