Browse Source

jwt编码后校验

yufeng0528 4 years ago
parent
commit
e1f7ebef1f

+ 6 - 1
spring5-admin/src/main/resources/application.yml

@@ -20,5 +20,10 @@ spring:
20 20
     password: huojutech!23
21 21
   
22 22
 password:
23
-  salt: k12829WhsvnEV$#03b2n          
23
+  salt: k12829WhsvnEV$#03b2n     
24
+
25
+auth:
26
+  client:
27
+    secret: jVCIWXeV7BXuxe59
28
+       
24 29
   

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

@@ -3,6 +3,8 @@ package com.yaozhitech.spring5.intercept;
3 3
 import org.springframework.beans.factory.annotation.Value;
4 4
 import org.springframework.context.annotation.Configuration;
5 5
 
6
+import com.yaozhitech.spring5.utils.JwtUtils;
7
+
6 8
 import feign.RequestInterceptor;
7 9
 import feign.RequestTemplate;
8 10
 
@@ -11,10 +13,13 @@ public class OkHttpTokenInterceptor implements RequestInterceptor{
11 13
 
12 14
 	@Value("${spring.application.name}")
13 15
     private String applicationName;
16
+	
17
+	@Value("${auth.client.secret}")
18
+    private String clientSecret;
14 19
 
15 20
 	@Override
16 21
 	public void apply(RequestTemplate template) {
17
-		template.header("x-auth-client", applicationName);
22
+		template.header("x-auth-client", JwtUtils.sign(applicationName + "." + clientSecret, JwtUtils.generateSalt(), 3600));
18 23
 	}
19 24
 
20 25
 }

+ 22 - 9
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/intercept/ServiceAuthRestInterceptor.java

@@ -15,12 +15,16 @@ import com.alibaba.nacos.client.utils.IPUtil;
15 15
 import com.yaozhitech.spring5.common.exception.auth.ClientForbiddenException;
16 16
 import com.yaozhitech.spring5.common.util.ClientUtil;
17 17
 import com.yaozhitech.spring5.config.ServiceAuthConfig;
18
+import com.yaozhitech.spring5.utils.JwtUtils;
19
+
20
+import lombok.extern.slf4j.Slf4j;
18 21
 
19 22
 /**
20 23
  * 微服务之间的认证
21 24
  * @author EDZ
22 25
  *
23 26
  */
27
+@Slf4j
24 28
 public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
25 29
     private Logger logger = LoggerFactory.getLogger(ServiceAuthRestInterceptor.class);
26 30
 
@@ -49,16 +53,25 @@ public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
49 53
 //          return super.preHandle(request, response, handler);
50 54
 //      }
51 55
 
52
-      String token = request.getHeader("x-auth-client");
53
-      logger.info(token);
56
+		String token = request.getHeader("x-auth-client");
57
+		logger.info(token);
58
+		
59
+		if (token.equals("gateway")) {
60
+			return super.preHandle(request, response, handler);
61
+		}
62
+
63
+		try {
64
+			String uniqueName = JwtUtils.getUsername(token);
65
+			for (String client : allowedClient) {
66
+				if (client.equals(uniqueName.split("\\.")[0])) {
67
+					return super.preHandle(request, response, handler);
68
+				}
69
+			}
70
+		} catch (Exception e) {
71
+			log.error(e.getMessage(), e);
72
+			throw new ClientForbiddenException("Client verfy error");
73
+		}
54 74
       
55
-//      IJWTInfo infoFromToken = serviceAuthUtil.getInfoFromToken(token);
56
-      String uniqueName = token;
57
-      for(String client:allowedClient){
58
-          if(client.equals(uniqueName)){
59
-              return super.preHandle(request, response, handler);
60
-          }
61
-      }
62 75
       throw new ClientForbiddenException("Client is Forbidden!");
63 76
   }
64 77
 }

+ 4 - 1
spring5-order/src/main/resources/application.yml

@@ -16,4 +16,7 @@ spring:
16 16
   
17 17
 password:
18 18
   salt: k12829WhsvnEV$#03b2n          
19
-  
19
+  
20
+auth:
21
+  client:
22
+    secret: qx4MXVR9SJm31q2C