成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

SpringCloud Alibaba微服務(wù)實戰(zhàn)之 禁止直接訪問后端服務(wù)

開發(fā) 前端
使用SpringCloud架構(gòu)后我們希望所有的請求都需要經(jīng)過網(wǎng)關(guān)才能訪問,在不作任何處理的情況下我們是可以繞過網(wǎng)關(guān)直接訪問后端服務(wù)的。如下,我們繞過網(wǎng)關(guān)直接訪問后端服務(wù)也是可以獲取到數(shù)據(jù)的。

 [[378948]]

本文轉(zhuǎn)載自微信公眾號「JAVA日知錄」,作者單一色調(diào) 。轉(zhuǎn)載本文請聯(lián)系JAVA日知錄公眾號。

前言

使用SpringCloud架構(gòu)后我們希望所有的請求都需要經(jīng)過網(wǎng)關(guān)才能訪問,在不作任何處理的情況下我們是可以繞過網(wǎng)關(guān)直接訪問后端服務(wù)的。如下,我們繞過網(wǎng)關(guān)直接訪問后端服務(wù)也是可以獲取到數(shù)據(jù)的。

 

那我們今天的議題就是 如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)?

解決方案

我覺得防止繞過網(wǎng)關(guān)直接請求后端服務(wù)的解決方案主要有三種:

  • 使用Kubernetes部署

在使用Kubernetes部署SpringCloud架構(gòu)時我們給網(wǎng)關(guān)的Service配置NodePort,其他后端服務(wù)的Service使用ClusterIp,這樣在集群外就只能訪問到網(wǎng)關(guān)了。

  • 網(wǎng)絡(luò)隔離

后端普通服務(wù)都部署在內(nèi)網(wǎng),通過防火墻策略限制只允許網(wǎng)關(guān)應(yīng)用訪問后端服務(wù)。

  • 應(yīng)用層攔截

請求后端服務(wù)時通過攔截器校驗請求是否來自網(wǎng)關(guān),如果不來自網(wǎng)關(guān)則提示不允許訪問。

這里我們著重關(guān)注在應(yīng)用層攔截這種解決方案。

實現(xiàn)思路

實現(xiàn)思路其實也很簡單,在請求經(jīng)過網(wǎng)關(guān)的時候給請求頭中增加一個額外的Header,在后端服務(wù)中寫一個攔截器,判斷請求頭是否與在網(wǎng)關(guān)設(shè)置的請求Header一致,如果不一致則不允許訪問并給出提示。

當(dāng)然為了防止在每個后端服務(wù)都需要編寫這個攔截器,我們可以將其寫在一個公共的starter中,讓后端服務(wù)引用即可。而且為了靈活,可以通過配置決定是否只允許后端服務(wù)訪問。

接下來我們看看核心代碼。(代碼中涉及 SpringBoot 編寫公共Starter的套路,相信看過我博客的同學(xué)肯定是會的,因為之前文章有詳細(xì)說過。)

實現(xiàn)過程

在網(wǎng)關(guān)cloud-gateway模塊編寫網(wǎng)關(guān)過濾器

  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.  

在請求經(jīng)過網(wǎng)關(guān)時添加額外的Header,為了方便這里直接設(shè)置成固定值。

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

 

  • 編寫配置類,用于靈活控制服務(wù)是否允許繞過網(wǎng)關(guān)
  1. @Data 
  2. @ConfigurationProperties(prefix = "javadaily.cloud"
  3. public class CloudSecurityProperties { 
  4.  
  5.     /** 
  6.      * 是否只能通過網(wǎng)關(guān)獲取資源 
  7.      * 默認(rèn)為True 
  8.      */ 
  9.     private Boolean onlyFetchByGateway = Boolean.TRUE
  10.  
  • 編寫攔截器,用于校驗請求是否經(jīng)過網(wǎng)關(guān)
  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("請通過網(wǎng)關(guān)訪問資源"); 
  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 

在后端服務(wù)配置文件中添加屬性配置,默認(rèn)只能通過網(wǎng)關(guān)訪問

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

經(jīng)過以上幾步,一個公共的Starter模塊就構(gòu)建完成了。

后端服務(wù)引用此公共Starter模塊即可,以account-service為例

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

實現(xiàn)效果

直接訪問后端服務(wù)接口

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

 

返回結(jié)果:

  1.   "message""請通過網(wǎng)關(guān)訪問資源"
  2.   "status": 403, 
  3.   "success"false
  4.   "timestamp": 1611660015830 

 

責(zé)任編輯:武曉燕 來源: JAVA日知錄
相關(guān)推薦

2021-08-02 09:27:02

微服務(wù)接口場景

2021-05-14 09:15:32

SpringCloud微服務(wù)日志

2021-03-09 09:33:42

網(wǎng)關(guān)授權(quán)微服務(wù)

2021-04-22 09:31:58

服務(wù)器微服務(wù)配置

2021-06-09 09:42:50

SpringCloud微服務(wù)灰度發(fā)布

2022-04-09 14:45:02

微服務(wù)常見概念Spring

2022-04-27 08:23:34

微服務(wù)負(fù)載均衡

2016-08-25 20:55:19

微服務(wù)架構(gòu)發(fā)布

2016-08-25 21:12:31

微服務(wù)架構(gòu)發(fā)布

2021-03-26 06:01:45

日志MongoDB存儲

2025-03-13 00:55:00

微服務(wù)架構(gòu)系統(tǒng)

2023-02-07 07:43:27

微服務(wù)應(yīng)用框架

2017-09-05 14:05:11

微服務(wù)spring clou路由

2021-02-04 09:18:20

服務(wù)器認(rèn)證自定義

2025-04-17 02:00:00

2025-03-07 08:57:46

HTTP客戶端框架

2009-12-28 14:54:48

ADO.NET語句

2024-07-02 10:58:53

2021-05-31 11:22:24

微服務(wù)開發(fā)框架

2022-05-12 07:37:51

單點登錄微服務(wù)開源
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 精品一二三 | 日日夜夜精品 | 午夜影晥 | 91国语清晰打电话对白 | 欧美日韩免费一区二区三区 | 羞羞的视频免费在线观看 | 激情网站 | 中文字幕国产精品 | 一区二区三区播放 | 国产精品资源在线 | 一区二区在线免费观看视频 | 狠狠操狠狠操 | 日韩av一区二区在线观看 | 国产精品www | 国产精久久久久久久妇剪断 | 免费在线视频一区二区 | 综合久久av | 成人在线观看黄 | 亚洲一区二区日韩 | 午夜精品| 日本天天操| 亚洲精品第一页 | 男女在线网站 | 国产在线中文字幕 | 小川阿佐美pgd-606在线 | 一区二区在线免费观看 | 中文字幕一区二区三区不卡在线 | 美女国产| 最新国产精品视频 | 2019天天干夜夜操 | 一级片网址 | 国产h视频| 欧美激情亚洲激情 | 黄视频免费观看 | 久久久久国产 | 精品视频久久久久久 | 亚洲国产精品久久 | 日本淫视频 | 五月天婷婷综合 | 男女视频在线观看 | 视频一二区 |