Browse Source

简单校验 不通过抛出异常

yufeng0528 4 years ago
parent
commit
d21cccdf2b
31 changed files with 1113 additions and 32 deletions
  1. 7 6
      pom.xml
  2. 7 0
      spring5-auth/spring5-auth-client/pom.xml
  3. 10 11
      spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/config/FeignOkHttpConfig.java
  4. 5 1
      spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/intercept/OkHttpTokenInterceptor.java
  5. 19 13
      spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/intercept/ServiceAuthRestInterceptor.java
  6. 1 1
      spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/utils/JwtUtils.java
  7. 1 0
      spring5-common/.gitignore
  8. 21 0
      spring5-common/pom.xml
  9. 22 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/constant/CommonConstants.java
  10. 10 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/constant/RestCodeConstants.java
  11. 11 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/constant/UserConstant.java
  12. 40 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/BaseException.java
  13. 15 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/ClientForbiddenException.java
  14. 14 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/ClientInvalidException.java
  15. 14 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/ClientTokenException.java
  16. 14 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/UserInvalidException.java
  17. 14 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/UserTokenException.java
  18. 61 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/handler/GlobalExceptionHandler.java
  19. 35 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/BaseResponse.java
  20. 60 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/ListRestResponse.java
  21. 39 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/ObjectRestResponse.java
  22. 69 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/TableResultResponse.java
  23. 13 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/auth/TokenErrorResponse.java
  24. 13 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/auth/TokenForbiddenResponse.java
  25. 29 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/util/ClientUtil.java
  26. 346 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/util/ReflectionUtils.java
  27. 10 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/util/StringHelper.java
  28. 74 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/util/TreeUtil.java
  29. 28 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/util/UUIDUtils.java
  30. 42 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/vo/TreeNode.java
  31. 69 0
      spring5-common/src/main/java/com/yaozhitech/spring5/common/web/ParameterRequestWrapper.java

+ 7 - 6
pom.xml

@@ -16,6 +16,7 @@
16 16
         <module>spring5-gateway</module>
17 17
         <module>spring5-admin</module>
18 18
         <module>spring5-order</module>
19
+        <module>spring5-common</module>
19 20
     </modules>
20 21
 
21 22
 	<properties>
@@ -84,12 +85,12 @@
84 85
         </dependency>
85 86
        
86 87
        <!--热加载-->
87
-        <dependency>
88
-            <groupId>org.springframework.boot</groupId>
89
-            <artifactId>spring-boot-devtools</artifactId>
90
-            <scope>runtime</scope>
91
-            <optional>true</optional>
92
-        </dependency>
88
+<!--         <dependency> -->
89
+<!--             <groupId>org.springframework.boot</groupId> -->
90
+<!--             <artifactId>spring-boot-devtools</artifactId> -->
91
+<!--             <scope>runtime</scope> -->
92
+<!--             <optional>true</optional> -->
93
+<!--         </dependency> -->
93 94
 
94 95
 			
95 96
 		<!-- Spring test 依赖 -->

+ 7 - 0
spring5-auth/spring5-auth-client/pom.xml

@@ -44,5 +44,12 @@
44 44
             <groupId>io.github.openfeign</groupId>
45 45
             <artifactId>feign-okhttp</artifactId>
46 46
         </dependency>
47
+        
48
+        <!-- 二方包 -->
49
+        <dependency>
50
+            <groupId>com.yaozhitech</groupId>
51
+            <artifactId>spring5-common</artifactId>
52
+            <version>0.1.0</version>
53
+        </dependency>
47 54
 	</dependencies>
48 55
 </project>

+ 10 - 11
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/config/FeignOkHttpConfig.java

@@ -12,7 +12,6 @@ import org.springframework.context.annotation.Configuration;
12 12
 import com.yaozhitech.spring5.intercept.OkHttpTokenInterceptor;
13 13
 
14 14
 import feign.Feign;
15
-import okhttp3.ConnectionPool;
16 15
 
17 16
 @AutoConfigureBefore(FeignAutoConfiguration.class)
18 17
 @Configuration
@@ -25,16 +24,16 @@ public class FeignOkHttpConfig {
25 24
 	private int feignOkHttpReadTimeout = 60;
26 25
 	private int feignConnectTimeout = 60;
27 26
 	private int feignWriteTimeout = 120;
28
-
29
-//	@Bean
30
-//	public okhttp3.OkHttpClient okHttpClient() {
31
-//		return new okhttp3.OkHttpClient.Builder()
32
-//				.readTimeout(feignOkHttpReadTimeout, TimeUnit.SECONDS)
33
-//				.connectTimeout(feignConnectTimeout, TimeUnit.SECONDS)
34
-//				.writeTimeout(feignWriteTimeout, TimeUnit.SECONDS)
27
+	
28
+	@Bean
29
+	public okhttp3.OkHttpClient okHttpClient() {
30
+		return new okhttp3.OkHttpClient.Builder()
31
+				.readTimeout(feignOkHttpReadTimeout, TimeUnit.SECONDS)
32
+				.connectTimeout(feignConnectTimeout, TimeUnit.SECONDS)
33
+				.writeTimeout(feignWriteTimeout, TimeUnit.SECONDS)
35 34
 //				.connectionPool(new ConnectionPool())
36
-////				.addInterceptor(okHttpLoggingInterceptor)
35
+//				.addInterceptor(okHttpLoggingInterceptor)
37 36
 //				.addInterceptor(interceptor)
38
-//				.build();
39
-//	}
37
+				.build();
38
+	}
40 39
 }

+ 5 - 1
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/intercept/OkHttpTokenInterceptor.java

@@ -1,5 +1,6 @@
1 1
 package com.yaozhitech.spring5.intercept;
2 2
 
3
+import org.springframework.beans.factory.annotation.Value;
3 4
 import org.springframework.context.annotation.Configuration;
4 5
 
5 6
 import feign.RequestInterceptor;
@@ -8,9 +9,12 @@ import feign.RequestTemplate;
8 9
 @Configuration
9 10
 public class OkHttpTokenInterceptor implements RequestInterceptor{
10 11
 
12
+	@Value("${spring.application.name}")
13
+    private String applicationName;
14
+
11 15
 	@Override
12 16
 	public void apply(RequestTemplate template) {
13
-		template.header("X-Forwarded-For", "origin.host.com");
17
+		template.header("x-auth-client", applicationName);
14 18
 	}
15 19
 
16 20
 }

+ 19 - 13
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/intercept/ServiceAuthRestInterceptor.java

@@ -1,5 +1,6 @@
1 1
 package com.yaozhitech.spring5.intercept;
2 2
 
3
+import java.util.Arrays;
3 4
 import java.util.List;
4 5
 
5 6
 import javax.servlet.http.HttpServletRequest;
@@ -7,10 +8,12 @@ import javax.servlet.http.HttpServletResponse;
7 8
 
8 9
 import org.slf4j.Logger;
9 10
 import org.slf4j.LoggerFactory;
10
-import org.springframework.beans.factory.annotation.Autowired;
11 11
 import org.springframework.web.method.HandlerMethod;
12 12
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
13 13
 
14
+import com.alibaba.nacos.client.utils.IPUtil;
15
+import com.yaozhitech.spring5.common.exception.auth.ClientForbiddenException;
16
+import com.yaozhitech.spring5.common.util.ClientUtil;
14 17
 import com.yaozhitech.spring5.config.ServiceAuthConfig;
15 18
 
16 19
 /**
@@ -27,11 +30,16 @@ public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
27 30
 //  @Autowired
28 31
   private ServiceAuthConfig serviceAuthConfig;
29 32
 
30
-  private List<String> allowedClient;
33
+  private List<String> allowedClient = Arrays.asList("admin", "order", "gateway");
31 34
 
32 35
   @Override
33 36
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
34
-      HandlerMethod handlerMethod = (HandlerMethod) handler;
37
+		HandlerMethod handlerMethod = (HandlerMethod) handler;
38
+		// ip
39
+//		if (ClientUtil.isLocalhost(request)) {
40
+//			return super.preHandle(request, response, handler);
41
+//		}
42
+		
35 43
 //      // 配置该注解,说明不进行服务拦截
36 44
 //      IgnoreClientToken annotation = handlerMethod.getBeanType().getAnnotation(IgnoreClientToken.class);
37 45
 //      if (annotation == null) {
@@ -41,18 +49,16 @@ public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
41 49
 //          return super.preHandle(request, response, handler);
42 50
 //      }
43 51
 
44
-      String token = request.getHeader("X-Forwarded-For");
52
+      String token = request.getHeader("x-auth-client");
45 53
       logger.info(token);
46 54
       
47 55
 //      IJWTInfo infoFromToken = serviceAuthUtil.getInfoFromToken(token);
48
-//      String uniqueName = infoFromToken.getUniqueName();
49
-//      for(String client:serviceAuthUtil.getAllowedClient()){
50
-//          if(client.equals(uniqueName)){
51
-//              return super.preHandle(request, response, handler);
52
-//          }
53
-//      }
54
-//      throw new ClientForbiddenException("Client is Forbidden!");
55
-      
56
-      return super.preHandle(request, response, handler);
56
+      String uniqueName = token;
57
+      for(String client:allowedClient){
58
+          if(client.equals(uniqueName)){
59
+              return super.preHandle(request, response, handler);
60
+          }
61
+      }
62
+      throw new ClientForbiddenException("Client is Forbidden!");
57 63
   }
58 64
 }

+ 1 - 1
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/utils/JwtUtils.java

@@ -14,7 +14,7 @@ import com.auth0.jwt.interfaces.DecodedJWT;
14 14
 public class JwtUtils {
15 15
 
16 16
 	/**
17
-          * 获得token中的信息无需secret解密也能获得
17
+     * 获得token中的信息无需secret解密也能获得
18 18
      * @return token中包含的签发时间
19 19
      */
20 20
     public static Date getIssuedAt(String token) {

+ 1 - 0
spring5-common/.gitignore

@@ -0,0 +1 @@
1
+/target/

+ 21 - 0
spring5-common/pom.xml

@@ -0,0 +1,21 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+	<modelVersion>4.0.0</modelVersion>
5
+	
6
+	<artifactId>spring5-common</artifactId>
7
+	<packaging>jar</packaging>
8
+	<name>${project.artifactId}</name>
9
+    <description>spring5 demo</description>
10
+	
11
+	<parent>
12
+        <groupId>com.yaozhitech</groupId>
13
+        <artifactId>spring5</artifactId>
14
+        <version>0.1.0</version>
15
+    </parent>
16
+    
17
+	<dependencies>
18
+
19
+		
20
+	</dependencies>
21
+</project>

+ 22 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/constant/CommonConstants.java

@@ -0,0 +1,22 @@
1
+package com.yaozhitech.spring5.common.constant;
2
+
3
+/**
4
+ * Created by ace on 2017/8/29.
5
+ */
6
+public class CommonConstants {
7
+    public final static String RESOURCE_TYPE_MENU = "menu";
8
+    public final static String RESOURCE_TYPE_BTN = "button";
9
+    // 用户token异常
10
+    public static final Integer EX_USER_INVALID_CODE = 40101;
11
+    public static final Integer EX_USER_PASS_INVALID_CODE = 40001;
12
+    // 客户端token异常
13
+    public static final Integer EX_CLIENT_INVALID_CODE = 40301;
14
+    public static final Integer EX_CLIENT_FORBIDDEN_CODE = 40331;
15
+    public static final Integer EX_OTHER_CODE = 500;
16
+    public static final String CONTEXT_KEY_USER_ID = "currentUserId";
17
+    public static final String CONTEXT_KEY_USERNAME = "currentUserName";
18
+    public static final String CONTEXT_KEY_USER_NAME = "currentUser";
19
+    public static final String CONTEXT_KEY_USER_TOKEN = "currentUserToken";
20
+    public static final String JWT_KEY_USER_ID = "userId";
21
+    public static final String JWT_KEY_NAME = "name";
22
+}

+ 10 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/constant/RestCodeConstants.java

@@ -0,0 +1,10 @@
1
+package com.yaozhitech.spring5.common.constant;
2
+
3
+/**
4
+ * Created by ace on 2017/8/23.
5
+ */
6
+public class RestCodeConstants {
7
+
8
+    public static final int TOKEN_ERROR_CODE = 40101;
9
+    public static final int TOKEN_FORBIDDEN_CODE = 40301;
10
+}

+ 11 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/constant/UserConstant.java

@@ -0,0 +1,11 @@
1
+package com.yaozhitech.spring5.common.constant;
2
+
3
+/**
4
+ * ${DESCRIPTION}
5
+ *
6
+ * @author wanghaobin
7
+ * @create 2017-06-14 8:36
8
+ */
9
+public class UserConstant {
10
+    public static int PW_ENCORDER_SALT = 12;
11
+}

+ 40 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/BaseException.java

@@ -0,0 +1,40 @@
1
+package com.yaozhitech.spring5.common.exception;
2
+
3
+/**
4
+ * Created by ace on 2017/9/8.
5
+ */
6
+public class BaseException extends RuntimeException {
7
+    private int status = 200;
8
+
9
+    public int getStatus() {
10
+        return status;
11
+    }
12
+
13
+    public void setStatus(int status) {
14
+        this.status = status;
15
+    }
16
+
17
+    public BaseException() {
18
+    }
19
+
20
+    public BaseException(String message,int status) {
21
+        super(message);
22
+        this.status = status;
23
+    }
24
+
25
+    public BaseException(String message) {
26
+        super(message);
27
+    }
28
+
29
+    public BaseException(String message, Throwable cause) {
30
+        super(message, cause);
31
+    }
32
+
33
+    public BaseException(Throwable cause) {
34
+        super(cause);
35
+    }
36
+
37
+    public BaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
38
+        super(message, cause, enableSuppression, writableStackTrace);
39
+    }
40
+}

+ 15 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/ClientForbiddenException.java

@@ -0,0 +1,15 @@
1
+package com.yaozhitech.spring5.common.exception.auth;
2
+
3
+
4
+import com.yaozhitech.spring5.common.constant.CommonConstants;
5
+import com.yaozhitech.spring5.common.exception.BaseException;
6
+
7
+/**
8
+ * Created by ace on 2017/9/12.
9
+ */
10
+public class ClientForbiddenException extends BaseException {
11
+    public ClientForbiddenException(String message) {
12
+        super(message, CommonConstants.EX_CLIENT_FORBIDDEN_CODE);
13
+    }
14
+
15
+}

+ 14 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/ClientInvalidException.java

@@ -0,0 +1,14 @@
1
+package com.yaozhitech.spring5.common.exception.auth;
2
+
3
+
4
+import com.yaozhitech.spring5.common.constant.CommonConstants;
5
+import com.yaozhitech.spring5.common.exception.BaseException;
6
+
7
+/**
8
+ * Created by ace on 2017/9/10.
9
+ */
10
+public class ClientInvalidException extends BaseException {
11
+    public ClientInvalidException(String message) {
12
+        super(message, CommonConstants.EX_CLIENT_INVALID_CODE);
13
+    }
14
+}

+ 14 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/ClientTokenException.java

@@ -0,0 +1,14 @@
1
+package com.yaozhitech.spring5.common.exception.auth;
2
+
3
+
4
+import com.yaozhitech.spring5.common.constant.CommonConstants;
5
+import com.yaozhitech.spring5.common.exception.BaseException;
6
+
7
+/**
8
+ * Created by ace on 2017/9/10.
9
+ */
10
+public class ClientTokenException extends BaseException {
11
+    public ClientTokenException(String message) {
12
+        super(message, CommonConstants.EX_CLIENT_INVALID_CODE);
13
+    }
14
+}

+ 14 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/UserInvalidException.java

@@ -0,0 +1,14 @@
1
+package com.yaozhitech.spring5.common.exception.auth;
2
+
3
+
4
+import com.yaozhitech.spring5.common.constant.CommonConstants;
5
+import com.yaozhitech.spring5.common.exception.BaseException;
6
+
7
+/**
8
+ * Created by ace on 2017/9/8.
9
+ */
10
+public class UserInvalidException extends BaseException {
11
+    public UserInvalidException(String message) {
12
+        super(message, CommonConstants.EX_USER_PASS_INVALID_CODE);
13
+    }
14
+}

+ 14 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/exception/auth/UserTokenException.java

@@ -0,0 +1,14 @@
1
+package com.yaozhitech.spring5.common.exception.auth;
2
+
3
+
4
+import com.yaozhitech.spring5.common.constant.CommonConstants;
5
+import com.yaozhitech.spring5.common.exception.BaseException;
6
+
7
+/**
8
+ * Created by ace on 2017/9/8.
9
+ */
10
+public class UserTokenException extends BaseException {
11
+    public UserTokenException(String message) {
12
+        super(message, CommonConstants.EX_USER_INVALID_CODE);
13
+    }
14
+}

+ 61 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/handler/GlobalExceptionHandler.java

@@ -0,0 +1,61 @@
1
+package com.yaozhitech.spring5.common.handler;
2
+
3
+import javax.servlet.http.HttpServletResponse;
4
+
5
+import org.slf4j.Logger;
6
+import org.slf4j.LoggerFactory;
7
+import org.springframework.web.bind.annotation.ControllerAdvice;
8
+import org.springframework.web.bind.annotation.ExceptionHandler;
9
+import org.springframework.web.bind.annotation.ResponseBody;
10
+
11
+import com.yaozhitech.spring5.common.constant.CommonConstants;
12
+import com.yaozhitech.spring5.common.exception.BaseException;
13
+import com.yaozhitech.spring5.common.exception.auth.ClientTokenException;
14
+import com.yaozhitech.spring5.common.exception.auth.UserInvalidException;
15
+import com.yaozhitech.spring5.common.exception.auth.UserTokenException;
16
+import com.yaozhitech.spring5.common.msg.BaseResponse;
17
+
18
+/**
19
+ * Created by ace on 2017/9/8.
20
+ */
21
+@ControllerAdvice("com.yaozhitech")
22
+@ResponseBody
23
+public class GlobalExceptionHandler {
24
+    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
25
+
26
+    @ExceptionHandler(ClientTokenException.class)
27
+    public BaseResponse clientTokenExceptionHandler(HttpServletResponse response, ClientTokenException ex) {
28
+        response.setStatus(403);
29
+        logger.error(ex.getMessage(),ex);
30
+        return new BaseResponse(ex.getStatus(), ex.getMessage());
31
+    }
32
+
33
+    @ExceptionHandler(UserTokenException.class)
34
+    public BaseResponse userTokenExceptionHandler(HttpServletResponse response, UserTokenException ex) {
35
+        response.setStatus(200);
36
+        logger.error(ex.getMessage(),ex);
37
+        return new BaseResponse(ex.getStatus(), ex.getMessage());
38
+    }
39
+
40
+    @ExceptionHandler(UserInvalidException.class)
41
+    public BaseResponse userInvalidExceptionHandler(HttpServletResponse response, UserInvalidException ex) {
42
+        response.setStatus(200);
43
+        logger.error(ex.getMessage(),ex);
44
+        return new BaseResponse(ex.getStatus(), ex.getMessage());
45
+    }
46
+
47
+    @ExceptionHandler(BaseException.class)
48
+    public BaseResponse baseExceptionHandler(HttpServletResponse response, BaseException ex) {
49
+        logger.error(ex.getMessage(),ex);
50
+        response.setStatus(500);
51
+        return new BaseResponse(ex.getStatus(), ex.getMessage());
52
+    }
53
+
54
+    @ExceptionHandler(Exception.class)
55
+    public BaseResponse otherExceptionHandler(HttpServletResponse response, Exception ex) {
56
+        response.setStatus(500);
57
+        logger.error(ex.getMessage(),ex);
58
+        return new BaseResponse(CommonConstants.EX_OTHER_CODE, ex.getMessage());
59
+    }
60
+
61
+}

+ 35 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/BaseResponse.java

@@ -0,0 +1,35 @@
1
+package com.yaozhitech.spring5.common.msg;
2
+
3
+/**
4
+ * Created by ace on 2017/8/23.
5
+ */
6
+public class BaseResponse {
7
+    private int status = 200;
8
+    private String message;
9
+
10
+    public BaseResponse(int status, String message) {
11
+        this.status = status;
12
+        this.message = message;
13
+    }
14
+
15
+    public BaseResponse() {
16
+    }
17
+
18
+    public String getMessage() {
19
+        return message;
20
+    }
21
+
22
+    public void setMessage(String message) {
23
+        this.message = message;
24
+    }
25
+
26
+    public int getStatus() {
27
+        return status;
28
+    }
29
+
30
+    public void setStatus(int status) {
31
+        this.status = status;
32
+    }
33
+
34
+
35
+}

+ 60 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/ListRestResponse.java

@@ -0,0 +1,60 @@
1
+package com.yaozhitech.spring5.common.msg;
2
+
3
+/**
4
+ * ${DESCRIPTION}
5
+ *
6
+ * @author wanghaobin
7
+ * @create 2017-06-09 7:32
8
+ */
9
+public class ListRestResponse<T> {
10
+    String msg;
11
+    T result;
12
+    int count;
13
+
14
+
15
+
16
+    public String getMsg() {
17
+        return msg;
18
+    }
19
+
20
+    public void setMsg(String msg) {
21
+        this.msg = msg;
22
+    }
23
+
24
+    public T getResult() {
25
+        return result;
26
+    }
27
+
28
+    public void setResult(T result) {
29
+        this.result = result;
30
+    }
31
+
32
+    public int getCount() {
33
+        return count;
34
+    }
35
+
36
+    public void setCount(int count) {
37
+        this.count = count;
38
+    }
39
+
40
+    public ListRestResponse count(int count) {
41
+        this.setCount(count);
42
+        return this;
43
+    }
44
+
45
+    public ListRestResponse count(Long count) {
46
+        this.setCount(count.intValue());
47
+        return this;
48
+    }
49
+
50
+    public ListRestResponse msg(String msg) {
51
+        this.setMsg(msg);
52
+        return this;
53
+    }
54
+
55
+    public ListRestResponse result(T result) {
56
+        this.setResult(result);
57
+        return this;
58
+    }
59
+
60
+}

+ 39 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/ObjectRestResponse.java

@@ -0,0 +1,39 @@
1
+package com.yaozhitech.spring5.common.msg;
2
+
3
+/**
4
+ * Created by Ace on 2017/6/11.
5
+ */
6
+public class ObjectRestResponse<T> extends BaseResponse {
7
+
8
+    T data;
9
+    boolean rel;
10
+
11
+    public boolean isRel() {
12
+        return rel;
13
+    }
14
+
15
+    public void setRel(boolean rel) {
16
+        this.rel = rel;
17
+    }
18
+
19
+
20
+    public ObjectRestResponse rel(boolean rel) {
21
+        this.setRel(rel);
22
+        return this;
23
+    }
24
+
25
+
26
+    public ObjectRestResponse data(T data) {
27
+        this.setData(data);
28
+        return this;
29
+    }
30
+    public T getData() {
31
+        return data;
32
+    }
33
+
34
+    public void setData(T data) {
35
+        this.data = data;
36
+    }
37
+
38
+
39
+}

+ 69 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/TableResultResponse.java

@@ -0,0 +1,69 @@
1
+package com.yaozhitech.spring5.common.msg;
2
+
3
+import java.util.List;
4
+
5
+/**
6
+ * ${DESCRIPTION}
7
+ *
8
+ * @author wanghaobin
9
+ * @create 2017-06-14 22:40
10
+ */
11
+public class TableResultResponse<T> extends BaseResponse {
12
+
13
+    TableData<T> data;
14
+
15
+    public TableResultResponse(long total, List<T> rows) {
16
+        this.data = new TableData<T>(total, rows);
17
+    }
18
+
19
+    public TableResultResponse() {
20
+        this.data = new TableData<T>();
21
+    }
22
+
23
+    TableResultResponse<T> total(int total) {
24
+        this.data.setTotal(total);
25
+        return this;
26
+    }
27
+
28
+    TableResultResponse<T> total(List<T> rows) {
29
+        this.data.setRows(rows);
30
+        return this;
31
+    }
32
+
33
+    public TableData<T> getData() {
34
+        return data;
35
+    }
36
+
37
+    public void setData(TableData<T> data) {
38
+        this.data = data;
39
+    }
40
+
41
+    class TableData<T> {
42
+        long total;
43
+        List<T> rows;
44
+
45
+        public TableData(long total, List<T> rows) {
46
+            this.total = total;
47
+            this.rows = rows;
48
+        }
49
+
50
+        public TableData() {
51
+        }
52
+
53
+        public long getTotal() {
54
+            return total;
55
+        }
56
+
57
+        public void setTotal(long total) {
58
+            this.total = total;
59
+        }
60
+
61
+        public List<T> getRows() {
62
+            return rows;
63
+        }
64
+
65
+        public void setRows(List<T> rows) {
66
+            this.rows = rows;
67
+        }
68
+    }
69
+}

+ 13 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/auth/TokenErrorResponse.java

@@ -0,0 +1,13 @@
1
+package com.yaozhitech.spring5.common.msg.auth;
2
+
3
+import com.yaozhitech.spring5.common.constant.RestCodeConstants;
4
+import com.yaozhitech.spring5.common.msg.BaseResponse;
5
+
6
+/**
7
+ * Created by ace on 2017/8/23.
8
+ */
9
+public class TokenErrorResponse extends BaseResponse {
10
+    public TokenErrorResponse(String message) {
11
+        super(RestCodeConstants.TOKEN_ERROR_CODE, message);
12
+    }
13
+}

+ 13 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/msg/auth/TokenForbiddenResponse.java

@@ -0,0 +1,13 @@
1
+package com.yaozhitech.spring5.common.msg.auth;
2
+
3
+import com.yaozhitech.spring5.common.constant.RestCodeConstants;
4
+import com.yaozhitech.spring5.common.msg.BaseResponse;
5
+
6
+/**
7
+ * Created by ace on 2017/8/25.
8
+ */
9
+public class TokenForbiddenResponse  extends BaseResponse {
10
+    public TokenForbiddenResponse(String message) {
11
+        super(RestCodeConstants.TOKEN_FORBIDDEN_CODE, message);
12
+    }
13
+}

+ 29 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/util/ClientUtil.java

@@ -0,0 +1,29 @@
1
+package com.yaozhitech.spring5.common.util;
2
+
3
+import javax.servlet.http.HttpServletRequest;
4
+
5
+public class ClientUtil {
6
+	/**
7
+	 * 获取客户端真实ip
8
+	 * @param request
9
+	 * @return
10
+	 */
11
+	public static String getClientIp(HttpServletRequest request){
12
+		String ip = request.getHeader("x-forwarded-for");
13
+		if (ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)) {
14
+			ip = request.getHeader("Proxy-Client-IP");
15
+		}
16
+		if (ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)) {
17
+			ip = request.getHeader("WL-Proxy-Client-IP");
18
+		}
19
+		if (ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)) {
20
+			ip = request.getRemoteAddr();
21
+		}
22
+		return ip;
23
+	}
24
+	
25
+	public static boolean isLocalhost(HttpServletRequest request) {
26
+		String ip = getClientIp(request);
27
+		return "127.0.0.1".equals(ip);
28
+	}
29
+}

+ 346 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/util/ReflectionUtils.java

@@ -0,0 +1,346 @@
1
+package com.yaozhitech.spring5.common.util;
2
+
3
+import java.lang.reflect.Field;
4
+import java.lang.reflect.InvocationTargetException;
5
+import java.lang.reflect.Method;
6
+import java.lang.reflect.Modifier;
7
+import java.lang.reflect.ParameterizedType;
8
+import java.lang.reflect.Type;
9
+
10
+import org.apache.commons.lang3.StringUtils;
11
+import org.apache.commons.lang3.Validate;
12
+import org.slf4j.Logger;
13
+import org.slf4j.LoggerFactory;
14
+import org.springframework.util.Assert;
15
+
16
+/**
17
+ * 反射工具类.
18
+ * 提供调用getter/setter方法, 访问私有变量, 调用私有方法, 获取泛型类型Class, 被AOP过的真实类等工具函数.
19
+ * @author calvin
20
+ * @version 2013-01-15
21
+ */
22
+@SuppressWarnings("rawtypes")
23
+public class ReflectionUtils {
24
+	
25
+	private static final String SETTER_PREFIX = "set";
26
+
27
+	private static final String GETTER_PREFIX = "get";
28
+
29
+	private static final String CGLIB_CLASS_SEPARATOR = "$$";
30
+	
31
+	private static Logger logger = LoggerFactory.getLogger(ReflectionUtils.class);
32
+
33
+	/**
34
+	 * 调用Getter方法.
35
+	 * 支持多级,如:对象名.对象名.方法
36
+	 */
37
+	public static Object invokeGetter(Object obj, String propertyName) {
38
+		Object object = obj;
39
+		for (String name : StringUtils.split(propertyName, ".")){
40
+			String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(name);
41
+			object = invokeMethod(object, getterMethodName, new Class[] {}, new Object[] {});
42
+		}
43
+		return object;
44
+	}
45
+
46
+	/**
47
+	 * 调用Setter方法, 仅匹配方法名。
48
+	 * 支持多级,如:对象名.对象名.方法
49
+	 */
50
+	public static void invokeSetter(Object obj, String propertyName, Object value) {
51
+		Object object = obj;
52
+		String[] names = StringUtils.split(propertyName, ".");
53
+		for (int i=0; i<names.length; i++){
54
+			if(i<names.length-1){
55
+				String getterMethodName = GETTER_PREFIX + StringUtils.capitalize(names[i]);
56
+				object = invokeMethod(object, getterMethodName, new Class[] {}, new Object[] {});
57
+			}else{
58
+				String setterMethodName = SETTER_PREFIX + StringUtils.capitalize(names[i]);
59
+				invokeMethodByName(object, setterMethodName, new Object[] { value });
60
+			}
61
+		}
62
+	}
63
+
64
+	/**
65
+	 * 直接读取对象属性值, 无视private/protected修饰符, 不经过getter函数.
66
+	 */
67
+	public static Object getFieldValue(final Object obj, final String fieldName) {
68
+		Field field = getAccessibleField(obj, fieldName);
69
+
70
+		if (field == null) {
71
+			throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
72
+		}
73
+
74
+		Object result = null;
75
+		try {
76
+			result = field.get(obj);
77
+		} catch (IllegalAccessException e) {
78
+			logger.error("不可能抛出的异常{}", e.getMessage());
79
+		}
80
+		return result;
81
+	}
82
+
83
+	/**
84
+	 * 直接设置对象属性值, 无视private/protected修饰符, 不经过setter函数.
85
+	 */
86
+	public static void setFieldValue(final Object obj, final String fieldName, final Object value) {
87
+		Field field = getAccessibleField(obj, fieldName);
88
+
89
+		if (field == null) {
90
+			logger.error("Could not find field [" + fieldName + "] on target [" + obj + "]");
91
+			return;
92
+			//throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
93
+		}
94
+		try {
95
+			field.set(obj, convert(value, field.getType()));
96
+		} catch (IllegalAccessException e) {
97
+			logger.error("不可能抛出的异常:{}", e.getMessage());
98
+		}
99
+	}
100
+	
101
+	public static Object convert(Object object, Class<?> type) {
102
+	    if (object instanceof Number) {
103
+	        Number number = (Number) object;
104
+	        if (type.equals(byte.class) || type.equals(Byte.class)) {
105
+	            return number.byteValue();
106
+	        }
107
+	        if (type.equals(short.class) || type.equals(Short.class)) {
108
+	            return number.shortValue();
109
+	        }
110
+	        if (type.equals(int.class) || type.equals(Integer.class)) {
111
+	            return number.intValue();
112
+	        }
113
+	        if (type.equals(long.class) || type.equals(Long.class)) {
114
+	            return number.longValue();
115
+	        }
116
+	        if (type.equals(float.class) || type.equals(Float.class)) {
117
+	            return number.floatValue();
118
+	        }
119
+	        if (type.equals(double.class) || type.equals(Double.class)) {
120
+	            return number.doubleValue();
121
+	        }
122
+	    }
123
+	    if(type.equals(String.class)){
124
+	    	return object==null?"":object.toString();
125
+	    }
126
+	    return object;
127
+	}
128
+
129
+	/**
130
+	 * 直接调用对象方法, 无视private/protected修饰符.
131
+	 * 用于一次性调用的情况,否则应使用getAccessibleMethod()函数获得Method后反复调用.
132
+	 * 同时匹配方法名+参数类型,
133
+	 */
134
+	public static Object invokeMethod(final Object obj, final String methodName, final Class<?>[] parameterTypes,
135
+			final Object[] args) {
136
+		Method method = getAccessibleMethod(obj, methodName, parameterTypes);
137
+		if (method == null) {
138
+			throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + "]");
139
+		}
140
+
141
+		try {
142
+			return method.invoke(obj, args);
143
+		} catch (Exception e) {
144
+			throw convertReflectionExceptionToUnchecked(e);
145
+		}
146
+	}
147
+
148
+	/**
149
+	 * 直接调用对象方法, 无视private/protected修饰符,
150
+	 * 用于一次性调用的情况,否则应使用getAccessibleMethodByName()函数获得Method后反复调用.
151
+	 * 只匹配函数名,如果有多个同名函数调用第一个。
152
+	 */
153
+	public static Object invokeMethodByName(final Object obj, final String methodName, final Object[] args) {
154
+		Method method = getAccessibleMethodByName(obj, methodName);
155
+		if (method == null) {
156
+			throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + obj + "]");
157
+		}
158
+
159
+		try {
160
+			return method.invoke(obj, args);
161
+		} catch (Exception e) {
162
+			throw convertReflectionExceptionToUnchecked(e);
163
+		}
164
+	}
165
+
166
+	/**
167
+	 * 循环向上转型, 获取对象的DeclaredField, 并强制设置为可访问.
168
+	 * 
169
+	 * 如向上转型到Object仍无法找到, 返回null.
170
+	 */
171
+	public static Field getAccessibleField(final Object obj, final String fieldName) {
172
+		Validate.notNull(obj, "object can't be null");
173
+		Validate.notBlank(fieldName, "fieldName can't be blank");
174
+		for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
175
+			try {
176
+				Field field = superClass.getDeclaredField(fieldName);
177
+				makeAccessible(field);
178
+				return field;
179
+			} catch (NoSuchFieldException e) {//NOSONAR
180
+				// Field不在当前类定义,继续向上转型
181
+				continue;// new add
182
+			}
183
+		}
184
+		return null;
185
+	}
186
+
187
+	/**
188
+	 * 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.
189
+	 * 如向上转型到Object仍无法找到, 返回null.
190
+	 * 匹配函数名+参数类型。
191
+	 * 
192
+	 * 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)
193
+	 */
194
+	public static Method getAccessibleMethod(final Object obj, final String methodName,
195
+			final Class<?>... parameterTypes) {
196
+		Validate.notNull(obj, "object can't be null");
197
+		Validate.notBlank(methodName, "methodName can't be blank");
198
+
199
+		for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) {
200
+			try {
201
+				Method method = searchType.getDeclaredMethod(methodName, parameterTypes);
202
+				makeAccessible(method);
203
+				return method;
204
+			} catch (NoSuchMethodException e) {
205
+				// Method不在当前类定义,继续向上转型
206
+				continue;// new add
207
+			}
208
+		}
209
+		return null;
210
+	}
211
+
212
+	/**
213
+	 * 循环向上转型, 获取对象的DeclaredMethod,并强制设置为可访问.
214
+	 * 如向上转型到Object仍无法找到, 返回null.
215
+	 * 只匹配函数名。
216
+	 * 
217
+	 * 用于方法需要被多次调用的情况. 先使用本函数先取得Method,然后调用Method.invoke(Object obj, Object... args)
218
+	 */
219
+	public static Method getAccessibleMethodByName(final Object obj, final String methodName) {
220
+		Validate.notNull(obj, "object can't be null");
221
+		Validate.notBlank(methodName, "methodName can't be blank");
222
+
223
+		for (Class<?> searchType = obj.getClass(); searchType != Object.class; searchType = searchType.getSuperclass()) {
224
+			Method[] methods = searchType.getDeclaredMethods();
225
+			for (Method method : methods) {
226
+				if (method.getName().equals(methodName)) {
227
+					makeAccessible(method);
228
+					return method;
229
+				}
230
+			}
231
+		}
232
+		return null;
233
+	}
234
+
235
+	/**
236
+	 * 改变private/protected的方法为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
237
+	 */
238
+	public static void makeAccessible(Method method) {
239
+		if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers()))
240
+				&& !method.isAccessible()) {
241
+			method.setAccessible(true);
242
+		}
243
+	}
244
+
245
+	/**
246
+	 * 改变private/protected的成员变量为public,尽量不调用实际改动的语句,避免JDK的SecurityManager抱怨。
247
+	 */
248
+	public static void makeAccessible(Field field) {
249
+		if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier
250
+				.isFinal(field.getModifiers())) && !field.isAccessible()) {
251
+			field.setAccessible(true);
252
+		}
253
+	}
254
+
255
+	/**
256
+	 * 通过反射, 获得Class定义中声明的泛型参数的类型, 注意泛型必须定义在父类处
257
+	 * 如无法找到, 返回Object.class.
258
+	 * eg.
259
+	 * public UserDao extends HibernateDao<User>
260
+	 *
261
+	 * @param clazz The class to introspect
262
+	 * @return the first generic declaration, or Object.class if cannot be determined
263
+	 */
264
+	@SuppressWarnings("unchecked")
265
+	public static <T> Class<T> getClassGenricType(final Class clazz) {
266
+		return getClassGenricType(clazz, 0);
267
+	}
268
+
269
+	/**
270
+	 * 通过反射, 获得Class定义中声明的父类的泛型参数的类型.
271
+	 * 如无法找到, 返回Object.class.
272
+	 * 
273
+	 * 如public UserDao extends HibernateDao<User,Long>
274
+	 *
275
+	 * @param clazz clazz The class to introspect
276
+	 * @param index the Index of the generic ddeclaration,start from 0.
277
+	 * @return the index generic declaration, or Object.class if cannot be determined
278
+	 */
279
+	public static Class getClassGenricType(final Class clazz, final int index) {
280
+
281
+		Type genType = clazz.getGenericSuperclass();
282
+
283
+		if (!(genType instanceof ParameterizedType)) {
284
+			logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
285
+			return Object.class;
286
+		}
287
+
288
+		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
289
+
290
+		if (index >= params.length || index < 0) {
291
+			logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
292
+					+ params.length);
293
+			return Object.class;
294
+		}
295
+		if (!(params[index] instanceof Class)) {
296
+			logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
297
+			return Object.class;
298
+		}
299
+
300
+		return (Class) params[index];
301
+	}
302
+	
303
+	public static Class<?> getUserClass(Object instance) {
304
+		Assert.notNull(instance, "Instance must not be null");
305
+		Class clazz = instance.getClass();
306
+		if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) {
307
+			Class<?> superClass = clazz.getSuperclass();
308
+			if (superClass != null && !Object.class.equals(superClass)) {
309
+				return superClass;
310
+			}
311
+		}
312
+		return clazz;
313
+
314
+	}
315
+	
316
+	/**
317
+	 * 将反射时的checked exception转换为unchecked exception.
318
+	 */
319
+	public static RuntimeException convertReflectionExceptionToUnchecked(Exception e) {
320
+		if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException
321
+				|| e instanceof NoSuchMethodException) {
322
+			return new IllegalArgumentException(e);
323
+		} else if (e instanceof InvocationTargetException) {
324
+			return new RuntimeException(((InvocationTargetException) e).getTargetException());
325
+		} else if (e instanceof RuntimeException) {
326
+			return (RuntimeException) e;
327
+		}
328
+		return new RuntimeException("Unexpected Checked Exception.", e);
329
+	}
330
+	/**
331
+	 * 判断某个对象是否拥有某个属性
332
+	 * 
333
+	 * @param obj 对象
334
+	 * @param fieldName 属性名
335
+	 * @return 有属性返回true
336
+	 * 		        无属性返回false
337
+	 */
338
+	public static boolean hasField(final Object obj, final String fieldName){
339
+		Field field = getAccessibleField(obj, fieldName);
340
+		if (field == null) {
341
+			return false;
342
+		}
343
+		return true;
344
+
345
+	}
346
+}

+ 10 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/util/StringHelper.java

@@ -0,0 +1,10 @@
1
+package com.yaozhitech.spring5.common.util;
2
+
3
+/**
4
+ * Created by ace on 2017/9/10.
5
+ */
6
+public class StringHelper {
7
+    public static String getObjectValue(Object obj){
8
+        return obj==null?"":obj.toString();
9
+    }
10
+}

+ 74 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/util/TreeUtil.java

@@ -0,0 +1,74 @@
1
+package com.yaozhitech.spring5.common.util;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+import com.yaozhitech.spring5.common.vo.TreeNode;
7
+
8
+/**
9
+ * Created by Ace on 2017/6/12.
10
+ */
11
+public class TreeUtil{
12
+  /**
13
+   * 两层循环实现建树
14
+   * 
15
+   * @param treeNodes 传入的树节点列表
16
+   * @return
17
+   */
18
+  public static <T extends TreeNode> List<T> bulid(List<T> treeNodes,Object root) {
19
+
20
+    List<T> trees = new ArrayList<T>();
21
+
22
+    for (T treeNode : treeNodes) {
23
+
24
+      if (root.equals(treeNode.getParentId())) {
25
+        trees.add(treeNode);
26
+      }
27
+
28
+      for (T it : treeNodes) {
29
+        if (it.getParentId() == treeNode.getId()) {
30
+          if (treeNode.getChildren() == null) {
31
+            treeNode.setChildren(new ArrayList<TreeNode>());
32
+          }
33
+          treeNode.add(it);
34
+        }
35
+      }
36
+    }
37
+    return trees;
38
+  }
39
+
40
+  /**
41
+   * 使用递归方法建树
42
+   * 
43
+   * @param treeNodes
44
+   * @return
45
+   */
46
+  public static <T extends TreeNode> List<T> buildByRecursive(List<T> treeNodes,Object root) {
47
+    List<T> trees = new ArrayList<T>();
48
+    for (T treeNode : treeNodes) {
49
+      if (root.equals(treeNode.getParentId())) {
50
+        trees.add(findChildren(treeNode, treeNodes));
51
+      }
52
+    }
53
+    return trees;
54
+  }
55
+
56
+  /**
57
+   * 递归查找子节点
58
+   * 
59
+   * @param treeNodes
60
+   * @return
61
+   */
62
+  public static <T extends TreeNode> T findChildren(T treeNode, List<T> treeNodes) {
63
+    for (T it : treeNodes) {
64
+      if (treeNode.getId() == it.getParentId()) {
65
+        if (treeNode.getChildren() == null) {
66
+          treeNode.setChildren(new ArrayList<TreeNode>());
67
+        }
68
+        treeNode.add(findChildren(it, treeNodes));
69
+      }
70
+    }
71
+    return treeNode;
72
+  }
73
+
74
+}

+ 28 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/util/UUIDUtils.java

@@ -0,0 +1,28 @@
1
+package com.yaozhitech.spring5.common.util;
2
+
3
+import java.util.UUID;
4
+
5
+/**
6
+ * Created by ace on 2017/9/27.
7
+ */
8
+public class UUIDUtils {
9
+    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
10
+            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
11
+            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
12
+            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
13
+            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
14
+            "W", "X", "Y", "Z" };
15
+
16
+
17
+    public static String generateShortUuid() {
18
+        StringBuffer shortBuffer = new StringBuffer();
19
+        String uuid = UUID.randomUUID().toString().replace("-", "");
20
+        for (int i = 0; i < 8; i++) {
21
+            String str = uuid.substring(i * 4, i * 4 + 4);
22
+            int x = Integer.parseInt(str, 16);
23
+            shortBuffer.append(chars[x % 0x3E]);
24
+        }
25
+        return shortBuffer.toString();
26
+
27
+    }
28
+}

+ 42 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/vo/TreeNode.java

@@ -0,0 +1,42 @@
1
+package com.yaozhitech.spring5.common.vo;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+/**
7
+ * Created by Ace on 2017/6/12.
8
+ */
9
+public class TreeNode {
10
+    protected int id;
11
+    protected int parentId;
12
+
13
+    public List<TreeNode> getChildren() {
14
+        return children;
15
+    }
16
+
17
+    public void setChildren(List<TreeNode> children) {
18
+        this.children = children;
19
+    }
20
+
21
+    List<TreeNode> children = new ArrayList<TreeNode>();
22
+
23
+    public int getId() {
24
+        return id;
25
+    }
26
+
27
+    public void setId(int id) {
28
+        this.id = id;
29
+    }
30
+
31
+    public int getParentId() {
32
+        return parentId;
33
+    }
34
+
35
+    public void setParentId(int parentId) {
36
+        this.parentId = parentId;
37
+    }
38
+
39
+    public void add(TreeNode node){
40
+        children.add(node);
41
+    }
42
+}

+ 69 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/web/ParameterRequestWrapper.java

@@ -0,0 +1,69 @@
1
+package com.yaozhitech.spring5.common.web;
2
+
3
+
4
+import java.util.Enumeration;
5
+import java.util.Map;
6
+import java.util.Vector;
7
+
8
+import javax.servlet.http.HttpServletRequest;
9
+import javax.servlet.http.HttpServletRequestWrapper;
10
+
11
+/**
12
+ * ${DESCRIPTION}
13
+ *
14
+ * @author wanghaobin
15
+ * @create 2017-07-01 17:04
16
+ */
17
+public class ParameterRequestWrapper extends HttpServletRequestWrapper {
18
+
19
+    private Map params;
20
+
21
+    public ParameterRequestWrapper(HttpServletRequest request, Map newParams) {
22
+        super(request);
23
+        this.params = newParams;
24
+    }
25
+
26
+    @Override
27
+    public Map getParameterMap() {
28
+        return params;
29
+    }
30
+
31
+    @Override
32
+    public Enumeration getParameterNames() {
33
+        Vector l = new Vector(params.keySet());
34
+        return l.elements();
35
+    }
36
+
37
+    @Override
38
+    public String[] getParameterValues(String name) {
39
+        Object v = params.get(name);
40
+        if (v == null) {
41
+            return null;
42
+        } else if (v instanceof String[]) {
43
+            return (String[]) v;
44
+        } else if (v instanceof String) {
45
+            return new String[] { (String) v };
46
+        } else {
47
+            return new String[] { v.toString() };
48
+        }
49
+    }
50
+
51
+    @Override
52
+    public String getParameter(String name) {
53
+        Object v = params.get(name);
54
+        if (v == null) {
55
+            return null;
56
+        } else if (v instanceof String[]) {
57
+            String[] strArr = (String[]) v;
58
+            if (strArr.length > 0) {
59
+                return strArr[0];
60
+            } else {
61
+                return null;
62
+            }
63
+        } else if (v instanceof String) {
64
+            return (String) v;
65
+        } else {
66
+            return v.toString();
67
+        }
68
+    }
69
+}