From 7fd9973319d85f03a2444fa9b4118349224f878e Mon Sep 17 00:00:00 2001
From: Lingxiao108105 <71265122+Lingxiao108105@users.noreply.github.com>
Date: Tue, 19 Oct 2021 22:38:10 +1100
Subject: [PATCH] Add: check whether login from new place
---
.../common/constants/EmailConstants.java | 2 +
.../crm/crmserver/common/utils/IpUtil.java | 93 ++++++
.../crmserver/controller/UserController.java | 42 ++-
.../tech/crm/crmserver/dao/IpAddress.java | 39 +++
.../crm/crmserver/mapper/IpAddressMapper.java | 18 ++
.../crmserver/service/IpAddressService.java | 39 +++
.../crm/crmserver/service/UserService.java | 4 +-
.../service/impl/IpAddressServiceImpl.java | 102 +++++++
.../service/impl/UserServiceImpl.java | 4 +-
src/main/resources/db/schema-test.sql | 284 +++++++++---------
10 files changed, 483 insertions(+), 144 deletions(-)
create mode 100644 src/main/java/tech/crm/crmserver/common/utils/IpUtil.java
create mode 100644 src/main/java/tech/crm/crmserver/dao/IpAddress.java
create mode 100644 src/main/java/tech/crm/crmserver/mapper/IpAddressMapper.java
create mode 100644 src/main/java/tech/crm/crmserver/service/IpAddressService.java
create mode 100644 src/main/java/tech/crm/crmserver/service/impl/IpAddressServiceImpl.java
diff --git a/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java b/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java
index 1515e8b..0b54d03 100644
--- a/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java
+++ b/src/main/java/tech/crm/crmserver/common/constants/EmailConstants.java
@@ -18,4 +18,6 @@ public class EmailConstants {
public static final String ACTIVE_ACCOUNT_TITLE = "Active your account of ConnecTi";
+ public static final String NEW_LOGIN_TITLE = "Safety Notice: Login from new place";
+
}
diff --git a/src/main/java/tech/crm/crmserver/common/utils/IpUtil.java b/src/main/java/tech/crm/crmserver/common/utils/IpUtil.java
new file mode 100644
index 0000000..ed80f0e
--- /dev/null
+++ b/src/main/java/tech/crm/crmserver/common/utils/IpUtil.java
@@ -0,0 +1,93 @@
+package tech.crm.crmserver.common.utils;
+
+
+import javax.servlet.http.HttpServletRequest;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.springframework.http.*;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+import tech.crm.crmserver.dao.IpAddress;
+
+/**
+ * ip util
+ *
+ * @author Lingxiao Li
+ * @since 2021-10-19
+ **/
+public class IpUtil {
+
+ private final static String URL = "http://ip-api.com/json/";
+
+ /**
+ * get ip by httpServletRequest
+ * Keep a good mood
+ * from https://blog.csdn.net/qq_35387940/article/details/84391784
+ * CC 4.0 BY-SA
+ *
+ * @author JCccc
+ * @since 2018-11-23
+ **/
+ public static String getIpAddr(HttpServletRequest request) {
+ String ipAddress = null;
+ try {
+ ipAddress = request.getHeader("x-forwarded-for");
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
+ ipAddress = request.getHeader("Proxy-Client-IP");
+ }
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
+ ipAddress = request.getHeader("WL-Proxy-Client-IP");
+ }
+ if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
+ ipAddress = request.getRemoteAddr();
+ if (ipAddress.equals("127.0.0.1")) {
+ return null;
+ }
+ }
+ if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
+ // = 15
+ if (ipAddress.indexOf(",") > 0) {
+ ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
+ }
+ }
+ } catch (Exception e) {
+ ipAddress="";
+ }
+ // ipAddress = this.getRequest().getRemoteAddr();
+
+ return ipAddress;
+ }
+
+
+
+
+ /**
+ * https://blog.csdn.net/zai_xia/article/details/80926157
+ * @author Seven.wk
+ * @since 2018/07/04
+ * @param ip ip address
+ */
+ public static IpAddress sendPostRequest(String ip){
+
+ RestTemplate client = new RestTemplate();
+ HttpHeaders headers = new HttpHeaders();
+ HttpMethod method = HttpMethod.POST;
+ String url = URL + ip;
+
+ headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+
+ HttpEntity> requestEntity = new HttpEntity<>(null, headers);
+
+ ResponseEntity response = client.exchange(URL, method, requestEntity, IpAddress.class);
+ if(response.getStatusCode() != HttpStatus.OK){
+ return null;
+ }
+
+ return response.getBody();
+ }
+
+
+}
diff --git a/src/main/java/tech/crm/crmserver/controller/UserController.java b/src/main/java/tech/crm/crmserver/controller/UserController.java
index 05b4339..40e2c02 100644
--- a/src/main/java/tech/crm/crmserver/controller/UserController.java
+++ b/src/main/java/tech/crm/crmserver/controller/UserController.java
@@ -9,12 +9,15 @@
import tech.crm.crmserver.common.constants.SecurityConstants;
import tech.crm.crmserver.common.exception.BadPhotoException;
import tech.crm.crmserver.common.response.ResponseResult;
+import tech.crm.crmserver.common.utils.IpUtil;
import tech.crm.crmserver.common.utils.NullAwareBeanUtilsBean;
import tech.crm.crmserver.dao.User;
import tech.crm.crmserver.dto.*;
+import tech.crm.crmserver.service.IpAddressService;
import tech.crm.crmserver.service.TokenKeyService;
import tech.crm.crmserver.service.UserService;
+import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@@ -36,6 +39,9 @@ public class UserController {
@Autowired
private UserService userService;
+ @Autowired
+ private IpAddressService ipAddressService;
+
/**
* The login API
*
@@ -43,9 +49,16 @@ public class UserController {
* @return return 200 when login successfully, return 400 and reason in the msg
*/
@PostMapping("/login")
- public ResponseResult