فهرست منبع

ipv6地址处理

chengyao 3 سال پیش
والد
کامیت
d69bd08a31

+ 5 - 0
common/src/main/java/com/lantone/common/dto/AbnormalLogDTO.java

@@ -68,4 +68,9 @@ public class AbnormalLogDTO implements Serializable {
      * 记录创建时间
      */
     private Date gmtCreate;
+
+    /**
+     * 记录
+     */
+    private String remark;
 }

+ 4 - 0
common/src/main/java/com/lantone/common/dto/OperationLogDTO.java

@@ -79,4 +79,8 @@ public class OperationLogDTO implements Serializable {
      */
     private Date gmtCreate;
 
+    /**
+     * 记录
+     */
+    private String remark;
 }

+ 4 - 0
common/src/main/java/com/lantone/common/util/AddressUtils.java

@@ -1,6 +1,7 @@
 package com.lantone.common.util;
 
 import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -21,6 +22,9 @@ public class AddressUtils {
 
     public static String getRealAddressByIP(String ip) {
         String address = UNKNOWN;
+        if(StringUtils.isBlank(ip)){
+            return UNKNOWN;
+        }
         // 内网不查询
         if (IpUtils.internalIp(ip)) {
             return "内网IP";

+ 50 - 60
common/src/main/java/com/lantone/common/util/IpUtils.java

@@ -11,6 +11,11 @@ import java.net.UnknownHostException;
  */
 
 public class IpUtils {
+
+    private static final int INADDR4SZ = 4;
+    private static final int INADDR16SZ = 16;
+    private static final int INT16SZ = 2;
+
     public static String getIpAddr(HttpServletRequest request) {
         if (request == null) {
             return "unknown";
@@ -76,75 +81,55 @@ public class IpUtils {
     /**
      * 将IPv4地址转换成字节
      *
-     * @param text IPv4地址
+     * @param src IPv4地址
      * @return byte 字节
      */
-    public static byte[] textToNumericFormatV4(String text) {
-        if (text.length() == 0) {
+    public static byte[] textToNumericFormatV4(String src)
+    {
+        byte[] res = new byte[INADDR4SZ];
+
+        long tmpValue = 0;
+        int currByte = 0;
+        boolean newOctet = true;
+
+        int len = src.length();
+        if (len == 0 || len > 15) {
             return null;
         }
 
-        byte[] bytes = new byte[4];
-        String[] elements = text.split("\\.", -1);
-        try {
-            long l;
-            int i;
-            switch (elements.length) {
-                case 1:
-                    l = Long.parseLong(elements[0]);
-                    if ((l < 0L) || (l > 4294967295L)) {
-                        return null;
-                    }
-                    bytes[0] = (byte) (int) (l >> 24 & 0xFF);
-                    bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
-                    bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
-                    bytes[3] = (byte) (int) (l & 0xFF);
-                    break;
-                case 2:
-                    l = Integer.parseInt(elements[0]);
-                    if ((l < 0L) || (l > 255L)) {
-                        return null;
-                    }
-                    bytes[0] = (byte) (int) (l & 0xFF);
-                    l = Integer.parseInt(elements[1]);
-                    if ((l < 0L) || (l > 16777215L)) {
-                        return null;
-                    }
-                    bytes[1] = (byte) (int) (l >> 16 & 0xFF);
-                    bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
-                    bytes[3] = (byte) (int) (l & 0xFF);
-                    break;
-                case 3:
-                    for (i = 0; i < 2; ++i) {
-                        l = Integer.parseInt(elements[i]);
-                        if ((l < 0L) || (l > 255L)) {
-                            return null;
-                        }
-                        bytes[i] = (byte) (int) (l & 0xFF);
-                    }
-                    l = Integer.parseInt(elements[2]);
-                    if ((l < 0L) || (l > 65535L)) {
-                        return null;
-                    }
-                    bytes[2] = (byte) (int) (l >> 8 & 0xFF);
-                    bytes[3] = (byte) (int) (l & 0xFF);
-                    break;
-                case 4:
-                    for (i = 0; i < 4; ++i) {
-                        l = Integer.parseInt(elements[i]);
-                        if ((l < 0L) || (l > 255L)) {
-                            return null;
-                        }
-                        bytes[i] = (byte) (int) (l & 0xFF);
-                    }
-                    break;
-                default:
+        for (int i = 0; i < len; i++) {
+            char c = src.charAt(i);
+            if (c == '.') {
+                if (newOctet || tmpValue < 0 || tmpValue > 0xff || currByte == 3) {
+                    return null;
+                }
+                res[currByte++] = (byte) (tmpValue & 0xff);
+                tmpValue = 0;
+                newOctet = true;
+            } else {
+                int digit = Character.digit(c, 10);
+                if (digit < 0) {
                     return null;
+                }
+                tmpValue *= 10;
+                tmpValue += digit;
+                newOctet = false;
             }
-        } catch (NumberFormatException e) {
+        }
+        if (newOctet || tmpValue < 0 || tmpValue >= (1L << ((4 - currByte) * 8))) {
             return null;
         }
-        return bytes;
+        switch (currByte) {
+            case 0:
+                res[0] = (byte) ((tmpValue >> 24) & 0xff);
+            case 1:
+                res[1] = (byte) ((tmpValue >> 16) & 0xff);
+            case 2:
+                res[2] = (byte) ((tmpValue >>  8) & 0xff);
+            case 3:
+                res[3] = (byte) ((tmpValue >>  0) & 0xff);
+        }
+        return res;
     }
 
     public static String getHostIp() {
@@ -162,4 +147,9 @@ public class IpUtils {
         }
         return "未知";
     }
+
+    public static boolean isIPv4LiteralAddress(String src) {
+        return textToNumericFormatV4(src) != null;
+    }
+
 }

+ 2 - 2
common/src/main/java/com/lantone/common/vo/SaveDictionaryVO.java

@@ -35,8 +35,8 @@ public class SaveDictionaryVO implements Serializable {
     private String val;
 
     @ApiModelProperty(value = "是否启用(0-否 1-是)",required = true)
-    @NotNull(message = "是否启用不能为空")
-    private Long status;
+    @NotBlank(message = "是否启用不能为空")
+    private String status;
 
     @ApiModelProperty(value = "字典说明",required = true)
     @NotBlank(message = "字典说明不能为空")

+ 1 - 1
common/src/main/java/com/lantone/common/vo/SaveHospitalSetVO.java

@@ -60,7 +60,7 @@ public class SaveHospitalSetVO implements Serializable {
     private String val;
 
     @ApiModelProperty(value = "是否启用(0-否 1-是)",required = true)
-    @NotNull(message = "是否启用不能为空")
+    @NotBlank(message = "是否启用不能为空")
     private String status;
 
     /**

+ 10 - 2
dblayer-mbg/src/main/resources/mapper/AbnormalLogMapper.xml

@@ -2,8 +2,16 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.lantone.dblayermbg.mapper.AbnormalLogMapper">
     <insert id="addOperErrorLog">
-    insert into sys_abnormal_log(operation_id,operation_name,operation_ip,operation_agent,operation_error_info,operation_method,operation_way,operation_url,operation_param,gmt_create)
-    values (#{operationId},#{operationName},INET_ATON(#{operationIp}),#{operationAgent},#{operationErrorInfo},#{operationMethod},#{operationWay},#{operationUrl},#{operationParam},#{gmtCreate})
+        insert into
+        sys_abnormal_log(operation_id,operation_name,operation_ip,operation_agent,operation_error_info,operation_method,operation_way,operation_url,operation_param,gmt_create)
+        values (#{operationId},#{operationName},
+        <choose>
+            <when test="operationIp != null || operationIp = !''">
+                INET_ATON(#{operationIp}),
+            </when>
+            <otherwise>#{operationIp}),</otherwise>
+        </choose>
+        #{operationAgent},#{operationErrorInfo},#{operationMethod},#{operationWay},#{operationUrl},#{operationParam},#{gmtCreate})
     </insert>
 
     <select id="getAbnormalLog" resultType="com.lantone.common.dto.AbnormalLogDTO">

+ 11 - 3
dblayer-mbg/src/main/resources/mapper/OperationLogMapper.xml

@@ -2,8 +2,16 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.lantone.dblayermbg.mapper.OperationLogMapper">
     <insert id="addOperLog">
-    insert into sys_operation_log(operation_id,operation_name,operation_date,operation_ip,operation_address,operation_method,operation_way,operation_url,operation_param,json_result,gmt_create)
-    values (#{operationId},#{operationName},#{operationDate},INET_ATON(#{operationIp}),#{operationAddress},#{operationMethod},#{operationWay},#{operationUrl},#{operationParam},#{jsonResult},#{gmtCreate})
+        insert into
+        sys_operation_log(operation_id,operation_name,operation_date,operation_ip,operation_address,operation_method,operation_way,operation_url,operation_param,json_result,gmt_create,remark)
+        values (#{operationId},#{operationName},#{operationDate},
+        <choose>
+            <when test="operationIp != null || operationIp = !''">
+                INET_ATON(#{operationIp}),
+            </when>
+            <otherwise>#{operationIp}),</otherwise>
+        </choose>
+        #{operationAddress},#{operationMethod},#{operationWay},#{operationUrl},#{operationParam},#{jsonResult},#{gmtCreate},#{remark})
     </insert>
 
     <select id="getOperationLog" resultType="com.lantone.common.dto.OperationLogDTO">
@@ -30,6 +38,6 @@
             <![CDATA[ AND a.operation_date >= #{startDate}]]>
             <![CDATA[ AND a.operation_date <= #{endDate}]]>
         </if>
-            order by a.operation_date desc
+        order by a.operation_date desc
     </select>
 </mapper>

+ 8 - 1
security-center/src/main/java/com/lantone/security/aop/LogAspect.java

@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.lantone.common.dto.AbnormalLogDTO;
 import com.lantone.common.dto.OperationLogDTO;
 import com.lantone.common.util.HttpUtils;
+import com.lantone.common.util.IpUtils;
 import com.lantone.common.util.SysUserUtils;
 import com.lantone.security.service.MessageService;
 import org.apache.commons.collections4.MapUtils;
@@ -129,7 +130,13 @@ public class LogAspect {
         operationLog.setGmtCreate(date);
         operationLog.setOperationId(SysUserUtils.getCurrentPrincipleId());
         operationLog.setOperationName(SysUserUtils.getCurrentPrinciple());
-        operationLog.setOperationIp(HttpUtils.getIpAddress());
+        String ip = HttpUtils.getIpAddress();
+       if(IpUtils.isIPv4LiteralAddress(ip)){
+           operationLog.setOperationIp(ip);
+       }else{
+           operationLog.setRemark("[非ipv4地址]:"+ip);
+       }
+
         // 设置方法名称
         String className = joinPoint.getTarget().getClass().getName();
         String methodName = joinPoint.getSignature().getName();