SpringCloud?中防止繞過(guò)網(wǎng)關(guān)請(qǐng)求直接訪問(wèn)后端服務(wù)的解決方法
SpringCloud 中如何防止繞過(guò)網(wǎng)關(guān)請(qǐng)求后端服務(wù)?感興趣的一起探討探討唄!
前言
使用SpringCloud架構(gòu)后我們希望所有的請(qǐng)求都需要經(jīng)過(guò)網(wǎng)關(guān)才能訪問(wèn),在不作任何處理的情況下我們是可以繞過(guò)網(wǎng)關(guān)直接訪問(wèn)后端服務(wù)的。如下,我們繞過(guò)網(wǎng)關(guān)直接訪問(wèn)后端服務(wù)也是可以獲取到數(shù)據(jù)的。

那我們今天的議題就是 如何防止請(qǐng)求繞過(guò)網(wǎng)關(guān)直接訪問(wèn)后端服務(wù)?
解決方案
我覺(jué)得防止繞過(guò)網(wǎng)關(guān)直接請(qǐng)求后端服務(wù)的解決方案主要有三種:
- 使用Kubernetes部署
在使用Kubernetes部署SpringCloud架構(gòu)時(shí)我們給網(wǎng)關(guān)的Service配置NodePort,其他后端服務(wù)的Service使用ClusterIp,這樣在集群外就只能訪問(wèn)到網(wǎng)關(guān)了。
- 網(wǎng)絡(luò)隔離
后端普通服務(wù)都部署在內(nèi)網(wǎng),通過(guò)防火墻策略限制只允許網(wǎng)關(guān)應(yīng)用訪問(wèn)后端服務(wù)。
- 應(yīng)用層攔截
請(qǐng)求后端服務(wù)時(shí)通過(guò)攔截器校驗(yàn)請(qǐng)求是否來(lái)自網(wǎng)關(guān),如果不來(lái)自網(wǎng)關(guān)則提示不允許訪問(wèn)。
這里我們著重關(guān)注在應(yīng)用層攔截這種解決方案。
實(shí)現(xiàn)思路
實(shí)現(xiàn)思路其實(shí)也很簡(jiǎn)單,在請(qǐng)求經(jīng)過(guò)網(wǎng)關(guān)的時(shí)候給請(qǐng)求頭中增加一個(gè)額外的Header,在后端服務(wù)中寫一個(gè)攔截器,判斷請(qǐng)求頭是否與在網(wǎng)關(guān)設(shè)置的請(qǐng)求Header一致,如果不一致則不允許訪問(wèn)并給出提示。
當(dāng)然為了防止在每個(gè)后端服務(wù)都需要編寫這個(gè)攔截器,我們可以將其寫在一個(gè)公共的starter中,讓后端服務(wù)引用即可。而且為了靈活,可以通過(guò)配置決定是否只允許后端服務(wù)訪問(wèn)。
接下來(lái)我們看看核心代碼。(代碼中涉及 SpringBoot 編寫公共Starter的套路,相信看過(guò)我博客的同學(xué)肯定是會(huì)的,因?yàn)橹拔恼掠性敿?xì)說(shuō)過(guò)。)
實(shí)現(xiàn)過(guò)程
在網(wǎng)關(guān)cloud-gateway模塊編寫網(wǎng)關(guān)過(guò)濾器
@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);
}
}在請(qǐng)求經(jīng)過(guò)網(wǎng)關(guān)時(shí)添加額外的Header,為了方便這里直接設(shè)置成固定值。
建立公共Starter模塊cloud-component-security-starter

編寫配置類,用于靈活控制服務(wù)是否允許繞過(guò)網(wǎng)關(guān)
@Data
@ConfigurationProperties(prefix = "javadaily.cloud")
public class CloudSecurityProperties {
/**
* 是否只能通過(guò)網(wǎng)關(guān)獲取資源
* 默認(rèn)為True
*/
private Boolean onlyFetchByGateway = Boolean.TRUE;
}編寫攔截器,用于校驗(yàn)請(qǐng)求是否經(jīng)過(guò)網(wǎng)關(guān)
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("請(qǐng)通過(guò)網(wǎng)關(guān)訪問(wèn)資源");
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的自動(dòng)加載
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.javadaily.component.security.configure.CloudSecurityAutoConfigure
在后端服務(wù)配置文件中添加屬性配置,默認(rèn)只能通過(guò)網(wǎng)關(guān)訪問(wèn)
javadaily:
cloud:
onlyFetchByGateway: true經(jīng)過(guò)以上幾步,一個(gè)公共的Starter模塊就構(gòu)建完成了。
后端服務(wù)引用此公共Starter模塊即可,以account-service為例
<dependency> <groupId>com.jianzh5.cloud</groupId> <artifactId>cloud-component-security-starter</artifactId> </dependency>
實(shí)現(xiàn)效果
直接訪問(wèn)后端服務(wù)接口 http://localhost:8010/account/getByCode/jianzh5

返回結(jié)果:
{
"message": "請(qǐng)通過(guò)網(wǎng)關(guān)訪問(wèn)資源",
"status": 403,
"success": false,
"timestamp": 1611660015830
}
到此這篇關(guān)于SpringCloud 中如何防止繞過(guò)網(wǎng)關(guān)請(qǐng)求后端服務(wù)的文章就介紹到這了,更多相關(guān)SpringCloud 網(wǎng)關(guān)請(qǐng)求后端服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于ClasspathResource路徑問(wèn)題的解決
這篇文章主要介紹了ClasspathResource路徑問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot打war包部署到外置tomcat容器的方法
這篇文章主要介紹了springboot]打war包部署到外置tomcat容器,在這需要注意的是在boot-launch.war在tomcat?webapps目錄里面解壓到boot-launch文件夾,感興趣的朋友跟隨小編一起看看吧2022-04-04
快速解決IDEA運(yùn)行控制臺(tái)中文亂碼的問(wèn)題
這段文章詳細(xì)介紹了如何解決中文亂碼問(wèn)題,通過(guò)配置TomcatServer、IntelliJIDEA全局設(shè)置、項(xiàng)目編碼及屬性文件編碼等步驟確保所有設(shè)置都正確無(wú)誤,配置完成后記得重啟IDEA以使更改生效2026-05-05
SpringBoot項(xiàng)目啟動(dòng)健康檢查的實(shí)現(xiàn)示例
健康檢查是確保容器內(nèi)應(yīng)用運(yùn)行正常的一種機(jī)制,它可以幫助我們及時(shí)發(fā)現(xiàn)并處理應(yīng)用中的問(wèn)題,比如服務(wù)崩潰、資源耗盡等,下面就來(lái)介紹一下SpringBoot項(xiàng)目啟動(dòng)健康檢查的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2025-07-07
K8S環(huán)境下如何驗(yàn)證RocketMQ擴(kuò)縮容
文章主要內(nèi)容驗(yàn)證了K8S環(huán)境下RocketMQ的擴(kuò)縮容特性,包括序號(hào)變化、命名規(guī)則以及節(jié)點(diǎn)重建后序號(hào)保持不變,StatefulSet確保Pod序號(hào)在重建后保持穩(wěn)定,而Deployment創(chuàng)建的Pod名稱是隨機(jī)的2025-01-01
Java中throws?與?throw?的區(qū)別與用法示例詳解
本文給大家介紹Java中throws與throw的區(qū)別與用法示例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2025-09-09
java實(shí)現(xiàn)fibonacci數(shù)列學(xué)習(xí)示例分享(斐波那契數(shù)列)
這篇文章主要介紹了fibonacci數(shù)列(斐波那契數(shù)列)示例,大家參考使用吧2014-01-01

