|
@@ -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
|
}
|