Explorar el Código

新增安全模块功能完善

linfutong hace 2 años
padre
commit
45dfb96b11

+ 113 - 134
zd-common/common-core/src/main/java/com/zd/common/core/utils/ParamCheckUtils.java

@@ -7,14 +7,13 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.regex.Pattern;
 
+
 /**
- * 
- * @author Quiet
- *
- * @CreateTime 2019年10月29日 下午4:56:48
- *
- * @Description 这个是参数校验的类
+ * <p>参数检查类</p>
  *
+ * @author: linft
+ * @date: 2023/4/14
+ * @since:
  */
 public class ParamCheckUtils {
 
@@ -37,9 +36,12 @@ public class ParamCheckUtils {
 	private static void throwParamException(String message) {
 		throw new ParamException(message);
 	}
-	
+
 	/**
-	 * # null == obj throw ParamException(1001,message); 
+	 * 检查Object非空
+	 *
+	 * @param obj
+	 * @param message
 	 */
 	public static void notNull(Object obj, String message) {
 		if(null == obj)
@@ -59,51 +61,60 @@ public class ParamCheckUtils {
 		public void throwException(String message) {
 			throwParamException(message);
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == obj throw ParamException
+		 * 检查Object不为null,指定提示message
 		 *
+		 * @param obj
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck notNull(Object obj, String message) {
 			if(null == obj)
 				throwParamException(message);
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == obj throw ParamException
+		 * 检查Object不为null
 		 *
+		 * @param obj
+		 * @return
 		 */
 		public ParamCheck notNull(Object obj) {
 			return notNull(obj,msg);
 		}
 		
+
 		/**
-		 * 
-		 * @Description null==str || "".equals(str) throw ParamException
+		 * 检查字符串不为空,指定提示msg
 		 *
+		 * @param str
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck strNotEmpty(String str, String message) {
 			if(null == str || "".equals(str))
 				throwParamException(message);
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description null==str || "".equals(str) throw ParamException
+		 * 检查字符串不为空,指定提示msg
 		 *
+		 * @param str
+		 * @return
 		 */
 		public ParamCheck strNotEmpty(String str) {
 			return strNotEmpty(str,msg);
 		}
-		
+
 		/**
-		 * 
-		 * @Description param == null || param <= 0 throw ParamException
+		 * 检查参数int大于0,指定提示message
 		 *
+		 * @param param
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck greaterThanZero(Integer param, String message) {
 			notNull(param, message);
@@ -112,64 +123,23 @@ public class ParamCheckUtils {
 			}
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description param == null || param <= 0 throw ParamException
+		 * 检查参数int大于0,指定提示message
 		 *
+		 * @param param
+		 * @return
 		 */
 		public ParamCheck greaterThanZero(Integer param) {
 			return greaterThanZero(param,msg);
 		}
-		
-		/**
-		 * 
-		 * @Description param == null || param <= 0 throw ParamException
-		 *
-		 */
-		public ParamCheck greaterThanZero(Byte param, String message) {
-			notNull(param, message);
-			if(param <= 0) {
-				throwParamException(message);
-			}
-			return this;
-		}
-		
-		/**
-		 * 
-		 * @Description param == null || param <= 0 throw ParamException
-		 *
-		 */
-		public ParamCheck greaterThanZero(Byte param) {
-			return greaterThanZero(param,msg);
-		}
-		
-		/**
-		 * 
-		 * @Description param == null || param <= 0 throw ParamException
-		 *
-		 */
-		public ParamCheck greaterThanZero(Short param, String message) {
-			notNull(param, message);
-			if(param <= 0) {
-				throwParamException(message);
-			}
-			return this;
-		}
-		
-		/**
-		 * 
-		 * @Description param == null || param <= 0 throw ParamException
-		 *
-		 */
-		public ParamCheck greaterThanZero(Short param) {
-			return greaterThanZero(param,msg);
-		}
-		
+
 		/**
-		 * 
-		 * @Description 0 == zero throw ParamException
+		 * 检查数字类不为0,指定提示语message
 		 *
+		 * @param number
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck notZero(Number number, String message) {
 			notNull(number, message);
@@ -203,109 +173,127 @@ public class ParamCheckUtils {
 			}
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description 0 == zero throw ParamException
+		 * 检查数字类不为0
 		 *
+		 * @param number
+		 * @return
 		 */
 		public ParamCheck notZero(Number number) {
 			return notZero(number,msg);
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == arr || arr.length == 0 throw ParamException
+		 * 检查Object[]不为空,指定提示语message
 		 *
+		 * @param arr
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck arrNotEmpty(Object[] arr, String message) {
 			if(null == arr || arr.length == 0)
 				throwParamException(message);
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == arr || arr.length == 0 throw ParamException
+		 * 检查Object[]不为空,指定提示语message
 		 *
+		 * @param arr
+		 * @return
 		 */
 		public ParamCheck arrNotEmpty(Object[] arr) {
 			return arrNotEmpty(arr,msg);
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == collections || collections.size() == 0 throw ParamException
+		 * 检查Collection不为空,指定提示语message
 		 *
+		 * @param collections
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck collectionNotEmpty(Collection<?> collections, String message) {
 			if(null == collections || collections.size() == 0)
 				throwParamException(message);
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == collections || collections.size() == 0 throw ParamException
+		 * 检查Collection不为空
 		 *
+		 * @param collections
+		 * @return
 		 */
 		public ParamCheck collectionNotEmpty(Collection<?> collections) {
 			return collectionNotEmpty(collections,msg);
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == map || map.size() == 0 throw ParamException
+		 * 检查Map不为空,指定提示语message
 		 *
+		 * @param map
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck mapNotEmpty(Map<?, ?> map, String message) {
 			if(null == map || map.size() == 0)
 				throwParamException(message);
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description null == map || map.size() == 0 throw ParamException
+		 * 检查Map不为空
 		 *
+		 * @param map
+		 * @return
 		 */
 		public ParamCheck mapNotEmpty(Map<?, ?> map) {
 			return mapNotEmpty(map,msg);
 		}
-		
+
 		/**
-		 * 
-		 * @Description !Pattern.matches(regex, str) throw ParamException
+		 * 检查regex和str是否一致,指定提示语message
 		 *
+		 * @param regex
+		 * @param str
+		 * @param message
+		 * @return
 		 */
 		public ParamCheck strNotRegex(String regex, String str, String message) {
 			if(!Pattern.matches(regex, str))
 				throwParamException(message);
 			return this;
 		}
-		
+
 		/**
-		 * 
-		 * @Description !Pattern.matches(regex, str) throw ParamException
+		 * 检查regex和str是否一致,指定提示语message
 		 *
+		 * @param regex
+		 * @param str
+		 * @return
 		 */
 		public ParamCheck strNotRegex(String regex, String str) {
 			return strNotRegex(regex,str,msg);
 		}
 
 		/**
+		 * 检查是否为Mail地址
 		 *
-		 * @Description !Pattern.matches(email, msg) throw ParamException
-		 *
+		 * @param email
+		 * @return
 		 */
 		public ParamCheck isNotMail(String email) {
 			return isNotMail(email, this.msg);
 		}
 
 		/**
+		 * 检查是否为Mail地址,指定提示msg
 		 *
-		 * @Description !Pattern.matches(email, msg) throw ParamException
-		 *
+		 * @param email
+		 * @param msg
+		 * @return
 		 */
 		public ParamCheck isNotMail(String email, String msg) {
 			Pattern p = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
@@ -315,18 +303,21 @@ public class ParamCheckUtils {
 		}
 
 		/**
+		 * 检查是否为手机号
 		 *
-		 * @Description !Pattern.matches(mobile, msg) throw ParamException
-		 *
+		 * @param mobile
+		 * @return
 		 */
 		public ParamCheck isNotMobile(String mobile) {
 			return isNotMobile(mobile, this.msg);
 		}
 
 		/**
+		 * 检查是否为手机号,指定提示msg
 		 *
-		 * @Description !Pattern.matches(mobile, msg) throw ParamException
-		 *
+		 * @param mobile
+		 * @param msg
+		 * @return
 		 */
 		public ParamCheck isNotMobile(String mobile,String msg) {
 			Pattern p = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]\\d{8}$");
@@ -334,40 +325,24 @@ public class ParamCheckUtils {
 				throwParamException(msg);
 			return this;
 		}
-		
-		/**
-		 * 
-		 * @Description !condition throw ParamException
-		 *
-		 */
-		public ParamCheck isTrue(boolean condition, String msg) {
-			if(!condition) 
-				throwParamException(msg);
-			return this;
-		}
-		
-		/**
-		 * 
-		 * @Description !condition throw ParamException
-		 *
-		 */
-		public ParamCheck isTrue(boolean condition) {
-			return isTrue(condition, this.msg);
-		}
-		
+
 		/**
+		 * 检查参数是否为数字
 		 *
-		 * @Description !Pattern.matches(number, msg) throw ParamException
-		 *
+		 * @param number
+		 * @return
 		 */
 		public ParamCheck isNotNumber(String number) {
 			return isNotNumber(number, this.msg);
 		}
 		
+
 		/**
+		 * 检查参数是否为数字,指定提示msg
 		 *
-		 * @Description !Pattern.matches(number, msg) throw ParamException
-		 *
+		 * @param number
+		 * @param msg
+		 * @return
 		 */
 		public ParamCheck isNotNumber(String number,String msg) {
 			Pattern p = Pattern.compile("^-?\\d+(\\.\\d+)?$");
@@ -376,13 +351,17 @@ public class ParamCheckUtils {
 			return this;
 		}
 
+		/**
+		 * 检查参数是否包含特殊字符
+		 *
+		 * @param str
+		 * @return
+		 */
 		public ParamCheck isNotSpecialChar(String str) {
 			Pattern p = Pattern.compile("[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t");
 			if(p.matcher(str).find())
 				throwParamException(msg);
 			return this;
 		}
-		
 	}
-	
 }

+ 18 - 6
zd-common/common-core/src/main/java/com/zd/common/core/web/controller/AbstractController.java

@@ -1,10 +1,9 @@
 package com.zd.common.core.web.controller;
 
-import com.alibaba.fastjson.JSONObject;
 import com.zd.common.core.utils.ParamCheckUtils;
+import com.zd.model.constant.SecurityConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StringUtils;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
@@ -48,7 +47,7 @@ public abstract class AbstractController {
      * 获取用户登录信息
      * @return
      */
-    /*protected LoginModel getUserLoginModel() {
+    /*protected LoginUser getUserLoginModel() {
         String modelStr = getRequest().getHeader(BaseConstant.LOGIN_MODEL);
         if(!StringUtils.isEmpty(modelStr)) {
             return JSONObject.parseObject(modelStr, LoginModel.class);
@@ -59,12 +58,25 @@ public abstract class AbstractController {
     /**
      * 	获取当前登录用户的用户id
      */
-    /*protected Long getCurrentUserId() {
-        String idStr = getRequest().getHeader(BaseConstant.USER_ID);
+    protected Long getCurrentUserId() {
+        // 设置用户信息到请求
+        String idStr = getRequest().getHeader(SecurityConstants.DETAILS_USER_ID);
         if(StringUtils.isEmpty(idStr)) {
             return null;
         }
         return Long.parseLong(idStr);
-    }*/
+    }
+
+    /**
+     * 	获取当前登录用户的用户id
+     */
+    protected String getCurrentUserName() {
+        // 设置用户信息到请求
+        String userName = getRequest().getHeader(SecurityConstants.DETAILS_USERNAME);
+        if(StringUtils.isEmpty(userName)) {
+            return null;
+        }
+        return userName;
+    }
 
 }

+ 71 - 0
zd-modules/zd-security/src/main/java/com/zd/security/config/AutoFillHandler.java

@@ -0,0 +1,71 @@
+package com.zd.security.config;
+
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+import com.zd.common.core.utils.StringUtils;
+import com.zd.model.constant.SecurityConstants;
+import org.apache.ibatis.reflection.MetaObject;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+import javax.servlet.http.HttpServletRequest;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * MyBatis自动填充
+ *
+ * @author: linft
+ * @date: 2023/4/14
+ * @since:
+ */
+public class AutoFillHandler implements MetaObjectHandler {
+
+    /**
+     * 插入时填充字段
+     *
+     * @param metaObject 元数据
+     */
+    @Override
+    public void insertFill(MetaObject metaObject) {
+        List<String> strings = Arrays.asList(metaObject.getGetterNames());
+        if (strings.contains("createTime")) {
+            this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
+        }
+        if (strings.contains("createBy")) {
+            Long userId = getCurrentLoginUserId();
+            this.setFieldValByName("createBy", userId, metaObject);
+        }
+
+    }
+
+    /**
+     * 更新时填充字段
+     *
+     * @param metaObject 元数据
+     */
+    @Override
+    public void updateFill(MetaObject metaObject) {
+        List<String> strings = Arrays.asList(metaObject.getGetterNames());
+        if (strings.contains("updateBy")) {
+            Long userId = getCurrentLoginUserId();
+            this.setFieldValByName("updateBy", userId, metaObject);
+        }
+        if (strings.contains("updateTime")) {
+            this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
+        }
+    }
+
+    /**
+     * 获取当前登录用户ID
+     * @return
+     */
+    private static Long getCurrentLoginUserId() {
+        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+        String idStr = request.getHeader(SecurityConstants.DETAILS_USER_ID);
+        if (StringUtils.isNotEmpty(idStr)) {
+            return Long.parseLong(idStr);
+        }
+        return null;
+    }
+}
+

+ 30 - 0
zd-modules/zd-security/src/main/java/com/zd/security/config/MybatisPlusConfig.java

@@ -0,0 +1,30 @@
+package com.zd.security.config;
+
+import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * <p>MybatisPlus配置</p>
+ *
+ * @author: linft
+ * @date: 2023/4/14
+ * @since:
+ */
+@Configuration
+public class MybatisPlusConfig {
+
+    @Bean
+    public MybatisPlusInterceptor mybatisPlusInterceptor() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
+        return interceptor;
+    }
+
+    @Bean
+    public AutoFillHandler autoFillHandler() {
+        return new AutoFillHandler();
+    }
+}

+ 6 - 2
zd-modules/zd-security/src/main/resources/remark.md

@@ -29,7 +29,11 @@
 - 存储长字符型数据尽量用varchar,长度最好控制在2000及以内。 注意:utf-8型varchar最大长度21845。
 - 若业务需求必须使用text、mediumtext、mediumint、mediumblob、longtext、longblob等,列表数据查询不可查询该类型字段。
 
-## 服务及数据库使用
+## 服务开发说明
 1. 开发过程中,最大可能使用Mybatis-Plus生成方法,减少或避免使用自定义方法和自定义Sql,保证服务高可维护性和效率。
 2. 对不频繁变动、需要关联的冷数据,可在自己设计表里冗余 或 服务缓存,避免关联Sql和跨服务频繁调用。
-3. 跨服务模块之间的数据库表,不允许使用关联Sql查询,可用服务RPC远程调用,在业务代码里进行数据封装和逻辑处理。
+3. 跨服务模块之间的数据库表,不允许使用关联Sql查询,可用服务RPC远程调用,在业务代码里进行数据封装和逻辑处理。
+4. 每个Controller需默认继承AbstractController,继承后将自动拥有参数检查、当前登录着信息。
+- 直接使用paramCheck,可检查判断参数;
+- 可使用 getCurrentUserId() 直接获取当前登录用户的userId;
+- 可使用 getCurrentUserName() 直接获取当前登录用户的 userName;