SpringCloud?中防止繞過網(wǎng)關請求直接訪問后端服務的解決方法
SpringCloud 中如何防止繞過網(wǎng)關請求后端服務?感興趣的一起探討探討唄!
前言
使用SpringCloud架構(gòu)后我們希望所有的請求都需要經(jīng)過網(wǎng)關才能訪問,在不作任何處理的情況下我們是可以繞過網(wǎng)關直接訪問后端服務的。如下,我們繞過網(wǎng)關直接訪問后端服務也是可以獲取到數(shù)據(jù)的。
那我們今天的議題就是 如何防止請求繞過網(wǎng)關直接訪問后端服務?
解決方案
我覺得防止繞過網(wǎng)關直接請求后端服務的解決方案主要有三種:
- 使用Kubernetes部署
在使用Kubernetes部署SpringCloud架構(gòu)時我們給網(wǎng)關的Service配置NodePort,其他后端服務的Service使用ClusterIp,這樣在集群外就只能訪問到網(wǎng)關了。
- 網(wǎng)絡隔離
后端普通服務都部署在內(nèi)網(wǎng),通過防火墻策略限制只允許網(wǎng)關應用訪問后端服務。
- 應用層攔截
請求后端服務時通過攔截器校驗請求是否來自網(wǎng)關,如果不來自網(wǎng)關則提示不允許訪問。
這里我們著重關注在應用層攔截這種解決方案。
實現(xiàn)思路
實現(xiàn)思路其實也很簡單,在請求經(jīng)過網(wǎng)關的時候給請求頭中增加一個額外的Header,在后端服務中寫一個攔截器,判斷請求頭是否與在網(wǎng)關設置的請求Header一致,如果不一致則不允許訪問并給出提示。
當然為了防止在每個后端服務都需要編寫這個攔截器,我們可以將其寫在一個公共的starter中,讓后端服務引用即可。而且為了靈活,可以通過配置決定是否只允許后端服務訪問。
接下來我們看看核心代碼。(代碼中涉及 SpringBoot 編寫公共Starter的套路,相信看過我博客的同學肯定是會的,因為之前文章有詳細說過。)
實現(xiàn)過程
在網(wǎng)關cloud-gateway
模塊編寫網(wǎng)關過濾器
@Component @Order(0) public class GatewayRequestFilter implements GlobalFilter { @Override public Mono<Void> 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); } }
在請求經(jīng)過網(wǎng)關時添加額外的Header,為了方便這里直接設置成固定值。
建立公共Starter模塊cloud-component-security-starter
編寫配置類,用于靈活控制服務是否允許繞過網(wǎng)關
@Data @ConfigurationProperties(prefix = "javadaily.cloud") public class CloudSecurityProperties { /** * 是否只能通過網(wǎng)關獲取資源 * 默認為True */ private Boolean onlyFetchByGateway = Boolean.TRUE; }
編寫攔截器,用于校驗請求是否經(jīng)過網(wǎng)關
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<String> resultData = new ResultData<>(); resultData.setSuccess(false); resultData.setStatus(HttpServletResponse.SC_FORBIDDEN); resultData.setMessage("請通過網(wǎng)關訪問資源"); 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()); } }
編寫stater裝載類
@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
在后端服務配置文件中添加屬性配置,默認只能通過網(wǎng)關訪問
javadaily: cloud: onlyFetchByGateway: true
經(jīng)過以上幾步,一個公共的Starter模塊就構(gòu)建完成了。
后端服務引用此公共Starter模塊即可,以account-service
為例
<dependency> <groupId>com.jianzh5.cloud</groupId> <artifactId>cloud-component-security-starter</artifactId> </dependency>
實現(xiàn)效果
直接訪問后端服務接口 http://localhost:8010/account/getByCode/jianzh5
返回結(jié)果:
{
"message": "請通過網(wǎng)關訪問資源",
"status": 403,
"success": false,
"timestamp": 1611660015830
}
到此這篇關于SpringCloud 中如何防止繞過網(wǎng)關請求后端服務的文章就介紹到這了,更多相關SpringCloud 網(wǎng)關請求后端服務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot微服務項目集成html頁面的實現(xiàn)
本文主要介紹了springboot微服務項目集成html頁面的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04SpringBoot框架實現(xiàn)切換啟動開發(fā)環(huán)境和測試環(huán)境
這篇文章主要介紹了SpringBoot框架實現(xiàn)切換啟動開發(fā)環(huán)境和測試環(huán)境,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot解決同名類導致的bean名沖突bean name conflicts問題
這篇文章主要介紹了SpringBoot解決同名類導致的bean名沖突bean name conflicts問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06詳解Java基礎篇--面向?qū)ο?(構(gòu)造方法,static、this關鍵字)
這篇文章主要介紹了Java基礎篇--面向?qū)ο?(構(gòu)造方法,static、this關鍵字),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04