Browse Source

两服务之间通过auth-server校验才能进行

yufeng0528 4 years ago
parent
commit
739cc02d7c

+ 2 - 0
spring5-admin/src/main/java/com/yaozhitech/spring5/AdminApplication.java

@@ -4,7 +4,9 @@ import org.mybatis.spring.annotation.MapperScan;
4 4
 import org.springframework.boot.SpringApplication;
5 5
 import org.springframework.boot.autoconfigure.SpringBootApplication;
6 6
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7
+import org.springframework.cloud.openfeign.EnableFeignClients;
7 8
 
9
+@EnableFeignClients
8 10
 @EnableDiscoveryClient
9 11
 @SpringBootApplication
10 12
 @MapperScan("com.yaozhitech.spring5.mapper")

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

@@ -20,6 +20,7 @@ public class OkHttpTokenInterceptor implements RequestInterceptor{
20 20
 	@Override
21 21
 	public void apply(RequestTemplate template) {
22 22
 		template.header("x-auth-client", JwtUtils.sign(applicationName + "." + clientSecret, JwtUtils.generateSalt(), 3600));
23
+		template.header("x-auth-token", "gateway");
23 24
 	}
24 25
 
25 26
 }

+ 15 - 17
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/intercept/ServiceAuthRestInterceptor.java

@@ -1,19 +1,18 @@
1 1
 package com.yaozhitech.spring5.intercept;
2 2
 
3
-import java.util.Arrays;
4
-import java.util.List;
5
-
6 3
 import javax.servlet.http.HttpServletRequest;
7 4
 import javax.servlet.http.HttpServletResponse;
8 5
 
9 6
 import org.slf4j.Logger;
10 7
 import org.slf4j.LoggerFactory;
8
+import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.beans.factory.annotation.Value;
11 10
 import org.springframework.web.method.HandlerMethod;
12 11
 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
13 12
 
14 13
 import com.yaozhitech.spring5.annotation.IgnoreClientToken;
15 14
 import com.yaozhitech.spring5.common.exception.auth.ClientForbiddenException;
16
-import com.yaozhitech.spring5.config.ServiceAuthConfig;
15
+import com.yaozhitech.spring5.provider.AuthServerProvider;
17 16
 import com.yaozhitech.spring5.utils.JwtUtils;
18 17
 
19 18
 import lombok.extern.slf4j.Slf4j;
@@ -27,13 +26,11 @@ import lombok.extern.slf4j.Slf4j;
27 26
 public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
28 27
     private Logger logger = LoggerFactory.getLogger(ServiceAuthRestInterceptor.class);
29 28
 
30
-//  @Autowired
31
-//  private ServiceAuthUtil serviceAuthUtil;
32
-//
33
-//  @Autowired
34
-  private ServiceAuthConfig serviceAuthConfig;
35
-
36
-  private List<String> allowedClient = Arrays.asList("admin", "order", "gateway");
29
+  @Autowired
30
+  private AuthServerProvider clientAuthProvider;
31
+  
32
+  @Value("${spring.application.name}")
33
+  private String applicationName;
37 34
 
38 35
   @Override
39 36
   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
@@ -52,20 +49,21 @@ public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
52 49
           return super.preHandle(request, response, handler);
53 50
       }
54 51
 
55
-		String token = request.getHeader("x-auth-client");
52
+		String token = request.getHeader("x-auth-token");
56 53
 		logger.info(token);
57 54
 		
55
+		String client = request.getHeader("x-auth-client");
56
+		
58 57
 		if (token.equals("gateway")) {
59 58
 			return super.preHandle(request, response, handler);
60 59
 		}
61 60
 
62 61
 		try {
63
-			String uniqueName = JwtUtils.getUsername(token);
64
-			for (String client : allowedClient) {
65
-				if (client.equals(uniqueName.split("\\.")[0])) {
66
-					return super.preHandle(request, response, handler);
67
-				}
62
+			String uniqueName = JwtUtils.getUsername(client);
63
+			if (Boolean.TRUE.equals(clientAuthProvider.verify(applicationName, uniqueName.split("\\.")[0], uniqueName.split("\\.")[1]))) {
64
+				return super.preHandle(request, response, handler);
68 65
 			}
66
+			
69 67
 		} catch (Exception e) {
70 68
 			log.error(e.getMessage(), e);
71 69
 			throw new ClientForbiddenException("Client verfy error");

+ 12 - 0
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/provider/AuthServerProvider.java

@@ -0,0 +1,12 @@
1
+package com.yaozhitech.spring5.provider;
2
+
3
+import org.springframework.cloud.openfeign.FeignClient;
4
+import org.springframework.web.bind.annotation.GetMapping;
5
+import org.springframework.web.bind.annotation.RequestParam;
6
+
7
+@FeignClient(name = "auth-server")
8
+public interface AuthServerProvider {
9
+
10
+	@GetMapping(value = "/clientAuth/verify/")
11
+    Boolean verify(@RequestParam("service") String service, @RequestParam("allowClient") String allowClient, @RequestParam("secret") String secret);
12
+}

+ 0 - 36
spring5-auth/spring5-auth-client/src/main/resources/application.yml

@@ -1,36 +0,0 @@
1
-server:
2
-  port: 8080
3
-
4
-logging:
5
-  level:
6
-    root: INFO
7
-    org.springframework.web: INFO
8
-    org.springframework.security: INFO
9
-#    org.springframework.boot.autoconfigure: DEBUG
10
-
11
-spring:
12
-  thymeleaf:
13
-    cache: false
14
-  security:
15
-    oauth2:
16
-      client:
17
-        registration:
18
-          github:
19
-            client-id: 7b9c752378a3d95a4529
20
-            client-secret: f635d8c7d44a50bdf12f2055e7e44b2bbc9c1043
21
-#           google:
22
-#             client-id: your-app-client-id
23
-#             client-secret: your-app-client-secret
24
-#           okta:
25
-#             client-id: fooClientIdPassword
26
-#             client-secret: secret
27
-#             scopes: read,foo
28
-#             authorization-grant-type: authorization_code
29
-#             redirect-uri-template: http://localhost:8080/login/oauth2/code/custom
30
-#         provider:
31
-#           okta:
32
-#             authorization-uri: http://localhost:8081/spring-security-oauth-server/oauth/authorize
33
-#             token-uri: http://localhost:8081/spring-security-oauth-server/oauth/token
34
-#             user-info-uri: http://localhost:8088/spring-security-oauth-resource/users/extra
35
-#             user-name-attribute: user_name
36
-            

+ 1 - 1
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/config/ShiroConfiguration.java

@@ -112,7 +112,7 @@ public class ShiroConfiguration {
112 112
         chainDefinition.addPathDefinition("/admin/**", "noSessionCreation,authcToken,anyRole[admin,manager]"); //只允许admin或manager角色的用户访问
113 113
         chainDefinition.addPathDefinition("/article/list", "noSessionCreation,authcToken");
114 114
         chainDefinition.addPathDefinition("/article/*", "noSessionCreation,authcToken[permissive]");
115
-        chainDefinition.addPathDefinition("/**", "noSessionCreation,anon"); // 默认进行用户鉴权
115
+        chainDefinition.addPathDefinition("/**", "noSessionCreation,authcToken"); // 默认进行用户鉴权
116 116
         return chainDefinition;
117 117
     }
118 118
 

+ 0 - 3
spring5-auth/spring5-auth-server/src/main/resources/application.yml

@@ -1,6 +1,3 @@
1
-server:
2
-  port: 8751
3
-
4 1
 logging:
5 2
   level:
6 3
     root: INFO

+ 12 - 0
spring5-auth/spring5-auth-server/src/main/resources/bootstrap.yml

@@ -0,0 +1,12 @@
1
+server:
2
+  port: ${SERVER_PORT:8751}
3
+spring:
4
+  application:
5
+    name: auth-server
6
+  cloud:
7
+    nacos:
8
+      discovery:
9
+        server-addr: ${REGISTER_HOST:192.168.99.100}:${REGISTER_PORT:8848}
10
+      config:
11
+        server-addr: ${REGISTER_HOST:192.168.99.100}:${REGISTER_PORT:8848}
12
+        file-extension: yml