资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringCloudAlibaba微服务实战之如何禁止直接访问后端服务

这篇文章主要讲解了“SpringCloud Alibaba微服务实战之如何禁止直接访问后端服务”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringCloud Alibaba微服务实战之如何禁止直接访问后端服务”吧!

成都创新互联公司是一家专业提供梅州企业网站建设,专注与成都网站建设、成都做网站、H5场景定制、小程序制作等业务。10年已为梅州众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

前言

使用SpringCloud架构后我们希望所有的请求都需要经过网关才能访问,在不作任何处理的情况下我们是可以绕过网关直接访问后端服务的。如下,我们绕过网关直接访问后端服务也是可以获取到数据的。

SpringCloud Alibaba微服务实战之如何禁止直接访问后端服务

那我们今天的议题就是 如何防止请求绕过网关直接访问后端服务?

解决方案

我觉得防止绕过网关直接请求后端服务的解决方案主要有三种:

  • 使用Kubernetes部署

在使用Kubernetes部署SpringCloud架构时我们给网关的Service配置NodePort,其他后端服务的Service使用ClusterIp,这样在集群外就只能访问到网关了。

  • 网络隔离

后端普通服务都部署在内网,通过防火墙策略限制只允许网关应用访问后端服务。

  • 应用层拦截

请求后端服务时通过拦截器校验请求是否来自网关,如果不来自网关则提示不允许访问。

这里我们着重关注在应用层拦截这种解决方案。

实现思路

实现思路其实也很简单,在请求经过网关的时候给请求头中增加一个额外的Header,在后端服务中写一个拦截器,判断请求头是否与在网关设置的请求Header一致,如果不一致则不允许访问并给出提示。

当然为了防止在每个后端服务都需要编写这个拦截器,我们可以将其写在一个公共的starter中,让后端服务引用即可。而且为了灵活,可以通过配置决定是否只允许后端服务访问。

接下来我们看看核心代码。(代码中涉及 SpringBoot  编写公共Starter的套路,相信看过我博客的同学肯定是会的,因为之前文章有详细说过。)

实现过程

在网关cloud-gateway模块编写网关过滤器

@Component @Order(0) public class GatewayRequestFilter implements GlobalFilter {      @Override     public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {         byte[] token = Base64Utils.encode((CloudConstant.GATEWAY_TOKEN_VALUE).getBytes());         String[] headerValues = {new String(token)};         ServerHttpRequest build = exchange.getRequest()                 .mutate()                 .header(CloudConstant.GATEWAY_TOKEN_HEADER, headerValues)                 .build();          ServerWebExchange newExchange = exchange.mutate().request(build).build();         return chain.filter(newExchange);     }  }

在请求经过网关时添加额外的Header,为了方便这里直接设置成固定值。

建立公共Starter模块cloud-component-security-starter

SpringCloud Alibaba微服务实战之如何禁止直接访问后端服务

  • 编写配置类,用于灵活控制服务是否允许绕过网关

@Data @ConfigurationProperties(prefix = "javadaily.cloud") public class CloudSecurityProperties {      /**      * 是否只能通过网关获取资源      * 默认为True      */     private Boolean onlyFetchByGateway = Boolean.TRUE;  }
  • 编写拦截器,用于校验请求是否经过网关

public class ServerProtectInterceptor implements HandlerInterceptor {      private CloudSecurityProperties properties;      @Override     public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler){          if (!properties.getOnlyFetchByGateway()) {             return true;         }          String token = request.getHeader(CloudConstant.GATEWAY_TOKEN_HEADER);          String gatewayToken = new String(Base64Utils.encode(CloudConstant.GATEWAY_TOKEN_VALUE.getBytes()));          if (StringUtils.equals(gatewayToken, token)) {             return true;         } else {             ResultData resultData = new ResultData<>();             resultData.setSuccess(false);             resultData.setStatus(HttpServletResponse.SC_FORBIDDEN);             resultData.setMessage("请通过网关访问资源");             WebUtils.writeJson(response,resultData);             return false;         }     }      public void setProperties(CloudSecurityProperties properties) {         this.properties = properties;     } }
  • 配置拦截器

public class CloudSecurityInterceptorConfigure implements WebMvcConfigurer {      private CloudSecurityProperties properties;      @Autowired     public void setProperties(CloudSecurityProperties properties) {         this.properties = properties;     }      @Bean     public HandlerInterceptor serverProtectInterceptor() {         ServerProtectInterceptor interceptor = new ServerProtectInterceptor();         interceptor.setProperties(properties);         return interceptor;     }      @Override     public void addInterceptors(InterceptorRegistry registry) {         registry.addInterceptor(serverProtectInterceptor());     } }
  • 编写starter装载类

@EnableConfigurationProperties(CloudSecurityProperties.class) public class CloudSecurityAutoConfigure{      @Bean     public CloudSecurityInterceptorConfigure cloudSecurityInterceptorConfigure() {         return new CloudSecurityInterceptorConfigure();     }  }
  • 建立资源文件spring.factories,配置Bean的自动加载

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\    com.javadaily.component.security.configure.CloudSecurityAutoConfigure

在后端服务配置文件中添加属性配置,默认只能通过网关访问

javadaily:   cloud:     onlyFetchByGateway: true

经过以上几步,一个公共的Starter模块就构建完成了。

后端服务引用此公共Starter模块即可,以account-service为例

  com.jianzh6.cloud  cloud-component-security-starter 

实现效果

直接访问后端服务接口

http://localhost:8010/account/getByCode/jianzh6

SpringCloud Alibaba微服务实战之如何禁止直接访问后端服务

返回结果:

  "message": "请通过网关访问资源",   "status": 403,   "success": false,   "timestamp": 1611660015830 }

感谢各位的阅读,以上就是“SpringCloud Alibaba微服务实战之如何禁止直接访问后端服务”的内容了,经过本文的学习后,相信大家对SpringCloud Alibaba微服务实战之如何禁止直接访问后端服务这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


文章题目:SpringCloudAlibaba微服务实战之如何禁止直接访问后端服务
地址分享:http://cdkjz.cn/article/peoics.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220