1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package com.yaozhitech.spring5.intercept;
- import java.util.Arrays;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.web.method.HandlerMethod;
- import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
- import com.alibaba.nacos.client.utils.IPUtil;
- import com.yaozhitech.spring5.common.exception.auth.ClientForbiddenException;
- import com.yaozhitech.spring5.common.util.ClientUtil;
- import com.yaozhitech.spring5.config.ServiceAuthConfig;
- /**
- * 微服务之间的认证
- * @author EDZ
- *
- */
- public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
- private Logger logger = LoggerFactory.getLogger(ServiceAuthRestInterceptor.class);
- // @Autowired
- // private ServiceAuthUtil serviceAuthUtil;
- //
- // @Autowired
- private ServiceAuthConfig serviceAuthConfig;
- private List<String> allowedClient = Arrays.asList("admin", "order", "gateway");
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- HandlerMethod handlerMethod = (HandlerMethod) handler;
- // ip
- // if (ClientUtil.isLocalhost(request)) {
- // return super.preHandle(request, response, handler);
- // }
-
- // // 配置该注解,说明不进行服务拦截
- // IgnoreClientToken annotation = handlerMethod.getBeanType().getAnnotation(IgnoreClientToken.class);
- // if (annotation == null) {
- // annotation = handlerMethod.getMethodAnnotation(IgnoreClientToken.class);
- // }
- // if(annotation!=null) {
- // return super.preHandle(request, response, handler);
- // }
- String token = request.getHeader("x-auth-client");
- logger.info(token);
-
- // IJWTInfo infoFromToken = serviceAuthUtil.getInfoFromToken(token);
- String uniqueName = token;
- for(String client:allowedClient){
- if(client.equals(uniqueName)){
- return super.preHandle(request, response, handler);
- }
- }
- throw new ClientForbiddenException("Client is Forbidden!");
- }
- }
|