Browse Source

IgnoreClientToken注解

yufeng0528 4 years ago
parent
commit
d003f8fde4

+ 15 - 0
spring5-auth/spring5-auth-client/src/main/java/com/yaozhitech/spring5/annotation/IgnoreClientToken.java

@@ -0,0 +1,15 @@
1
+package com.yaozhitech.spring5.annotation;
2
+
3
+import java.lang.annotation.ElementType;
4
+import java.lang.annotation.Retention;
5
+import java.lang.annotation.RetentionPolicy;
6
+import java.lang.annotation.Target;
7
+
8
+/**
9
+ * 忽略服务鉴权
10
+ * Created by ace on 2017/9/27.
11
+ */
12
+@Retention(RetentionPolicy.RUNTIME)
13
+@Target(value={ElementType.METHOD,ElementType.TYPE})
14
+public @interface IgnoreClientToken {
15
+}

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

@@ -11,9 +11,8 @@ import org.slf4j.LoggerFactory;
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;
14
+import com.yaozhitech.spring5.annotation.IgnoreClientToken;
15 15
 import com.yaozhitech.spring5.common.exception.auth.ClientForbiddenException;
16
-import com.yaozhitech.spring5.common.util.ClientUtil;
17 16
 import com.yaozhitech.spring5.config.ServiceAuthConfig;
18 17
 import com.yaozhitech.spring5.utils.JwtUtils;
19 18
 
@@ -44,14 +43,14 @@ public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
44 43
 //			return super.preHandle(request, response, handler);
45 44
 //		}
46 45
 		
47
-//      // 配置该注解,说明不进行服务拦截
48
-//      IgnoreClientToken annotation = handlerMethod.getBeanType().getAnnotation(IgnoreClientToken.class);
49
-//      if (annotation == null) {
50
-//          annotation = handlerMethod.getMethodAnnotation(IgnoreClientToken.class);
51
-//      }
52
-//      if(annotation!=null) {
53
-//          return super.preHandle(request, response, handler);
54
-//      }
46
+      // 配置该注解,说明不进行服务拦截
47
+      IgnoreClientToken annotation = handlerMethod.getBeanType().getAnnotation(IgnoreClientToken.class);
48
+      if (annotation == null) {
49
+          annotation = handlerMethod.getMethodAnnotation(IgnoreClientToken.class);
50
+      }
51
+      if(annotation != null) {
52
+          return super.preHandle(request, response, handler);
53
+      }
55 54
 
56 55
 		String token = request.getHeader("x-auth-client");
57 56
 		logger.info(token);

+ 15 - 0
spring5-order/src/main/java/com/yaozhitech/spring5/controller/OrderController.java

@@ -1,13 +1,16 @@
1 1
 package com.yaozhitech.spring5.controller;
2 2
 
3 3
 import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.beans.factory.annotation.Value;
4 5
 import org.springframework.http.ResponseEntity;
5 6
 import org.springframework.web.bind.annotation.GetMapping;
6 7
 import org.springframework.web.bind.annotation.PathVariable;
7 8
 import org.springframework.web.bind.annotation.RequestMapping;
8 9
 import org.springframework.web.bind.annotation.RestController;
9 10
 
11
+import com.yaozhitech.spring5.annotation.IgnoreClientToken;
10 12
 import com.yaozhitech.spring5.provider.AdminProvider;
13
+import com.yaozhitech.spring5.utils.JwtUtils;
11 14
 
12 15
 @RestController
13 16
 @RequestMapping("/order")
@@ -15,9 +18,21 @@ public class OrderController {
15 18
 	
16 19
 	@Autowired
17 20
 	private AdminProvider adminProvider;
21
+	
22
+	@Value("${spring.application.name}")
23
+    private String applicationName;
24
+	
25
+	@Value("${auth.client.secret}")
26
+    private String clientSecret;
18 27
 
19 28
 	@GetMapping("/{id}")
20 29
 	public ResponseEntity<String> read(@PathVariable Long id) {
21 30
 		return ResponseEntity.ok(adminProvider.admin(id));
22 31
 	}
32
+	
33
+	@IgnoreClientToken
34
+	@GetMapping("/token")
35
+	public ResponseEntity<String> token() {
36
+		return ResponseEntity.ok(JwtUtils.sign(applicationName + "." + clientSecret, JwtUtils.generateSalt(), 3600));
37
+	}
23 38
 }