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

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

 [[378948]]

本文转载自微信公众号「JAVA日知录」,作者单一色调 。转载本文请联系JAVA日知录公众号。

前言

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

 

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

解决方案

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

  • 使用Kubernetes部署

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

  • 网络隔离

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

  • 应用层拦截

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

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

实现思路

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

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

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

实现过程

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

  1. @Component 
  2. @Order(0) 
  3. public class GatewayRequestFilter implements GlobalFilter { 
  4.  
  5.     @Override 
  6.     public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { 
  7.         byte[] token = Base64Utils.encode((CloudConstant.GATEWAY_TOKEN_VALUE).getBytes()); 
  8.         String[] headerValues = {new String(token)}; 
  9.         ServerHttpRequest build = exchange.getRequest() 
  10.                 .mutate() 
  11.                 .header(CloudConstant.GATEWAY_TOKEN_HEADER, headerValues) 
  12.                 .build(); 
  13.  
  14.         ServerWebExchange newExchange = exchange.mutate().request(build).build(); 
  15.         return chain.filter(newExchange); 
  16.     } 
  17.  

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

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

 

  • 编写配置类,用于灵活控制服务是否允许绕过网关
  1. @Data 
  2. @ConfigurationProperties(prefix = "javadaily.cloud"
  3. public class CloudSecurityProperties { 
  4.  
  5.     /** 
  6.      * 是否只能通过网关获取资源 
  7.      * 默认为True 
  8.      */ 
  9.     private Boolean onlyFetchByGateway = Boolean.TRUE
  10.  
  • 编写拦截器,用于校验请求是否经过网关
  1. public class ServerProtectInterceptor implements HandlerInterceptor { 
  2.  
  3.     private CloudSecurityProperties properties; 
  4.  
  5.     @Override 
  6.     public boolean preHandle(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler){ 
  7.  
  8.         if (!properties.getOnlyFetchByGateway()) { 
  9.             return true
  10.         } 
  11.  
  12.         String token = request.getHeader(CloudConstant.GATEWAY_TOKEN_HEADER); 
  13.  
  14.         String gatewayToken = new String(Base64Utils.encode(CloudConstant.GATEWAY_TOKEN_VALUE.getBytes())); 
  15.  
  16.         if (StringUtils.equals(gatewayToken, token)) { 
  17.             return true
  18.         } else { 
  19.             ResultData<String> resultData = new ResultData<>(); 
  20.             resultData.setSuccess(false); 
  21.             resultData.setStatus(HttpServletResponse.SC_FORBIDDEN); 
  22.             resultData.setMessage("请通过网关访问资源"); 
  23.             WebUtils.writeJson(response,resultData); 
  24.             return false
  25.         } 
  26.     } 
  27.  
  28.     public void setProperties(CloudSecurityProperties properties) { 
  29.         this.properties = properties; 
  30.     } 
  • 配置拦截器
  1. public class CloudSecurityInterceptorConfigure implements WebMvcConfigurer { 
  2.  
  3.     private CloudSecurityProperties properties; 
  4.  
  5.     @Autowired 
  6.     public void setProperties(CloudSecurityProperties properties) { 
  7.         this.properties = properties; 
  8.     } 
  9.  
  10.     @Bean 
  11.     public HandlerInterceptor serverProtectInterceptor() { 
  12.         ServerProtectInterceptor interceptor = new ServerProtectInterceptor(); 
  13.         interceptor.setProperties(properties); 
  14.         return interceptor; 
  15.     } 
  16.  
  17.     @Override 
  18.     public void addInterceptors(InterceptorRegistry registry) { 
  19.         registry.addInterceptor(serverProtectInterceptor()); 
  20.     } 
  • 编写starter装载类
  1. @EnableConfigurationProperties(CloudSecurityProperties.class) 
  2. public class CloudSecurityAutoConfigure{ 
  3.  
  4.     @Bean 
  5.     public CloudSecurityInterceptorConfigure cloudSecurityInterceptorConfigure() { 
  6.         return new CloudSecurityInterceptorConfigure(); 
  7.     } 
  8.  
  • 建立资源文件spring.factories,配置Bean的自动加载
  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 
  2.    com.javadaily.component.security.configure.CloudSecurityAutoConfigure 

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

  1. javadaily: 
  2.   cloud: 
  3.     onlyFetchByGateway: true 

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

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

  1. <dependency> 
  2.  <groupId>com.jianzh5.cloud</groupId> 
  3.  <artifactId>cloud-component-security-starter</artifactId> 
  4. </dependency> 

实现效果

直接访问后端服务接口

http://localhost:8010/account/getByCode/jianzh5

 

返回结果:

  1.   "message""请通过网关访问资源"
  2.   "status": 403, 
  3.   "success"false
  4.   "timestamp": 1611660015830 

 

责任编辑:武晓燕 来源: JAVA日知录
相关推荐

2021-08-02 09:27:02

微服务接口场景

2021-03-09 09:33:42

网关授权微服务

2021-05-14 09:15:32

SpringCloud微服务日志

2021-04-22 09:31:58

服务器微服务配置

2021-06-09 09:42:50

SpringCloud微服务灰度发布

2022-04-09 14:45:02

微服务常见概念Spring

2022-04-27 08:23:34

微服务负载均衡

2016-08-25 20:55:19

微服务架构发布

2016-08-25 21:12:31

微服务架构发布

2021-03-26 06:01:45

日志MongoDB存储

2023-02-07 07:43:27

微服务应用框架

2017-09-05 14:05:11

微服务spring clou路由

2021-02-04 09:18:20

服务器认证自定义

2009-12-28 14:54:48

ADO.NET语句

2021-05-31 11:22:24

微服务开发框架

2022-05-12 07:37:51

单点登录微服务开源

2022-06-24 07:08:24

OHOS自定义服务

2021-07-13 10:00:00

微服务SleuthElasticSear

2017-08-31 09:39:56

微服务架构演进

2018-08-01 14:20:11

微服务架构人工智能
点赞
收藏

51CTO技术栈公众号