欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)

這篇文章主要講解了“如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)”吧!

創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、陽朔網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價格優(yōu)惠性價比高,為陽朔等各大城市提供網(wǎng)站開發(fā)制作服務(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ù)時通過攔截器校驗(yàn)請求是否來自網(wǎng)關(guān),如果不來自網(wǎng)關(guān)則提示不允許訪問。

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

實(shí)現(xiàn)思路

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

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

接下來我們看看核心代碼。(代碼中涉及 SpringBoot 編寫公共Starter的套路,相信看過我博客的同學(xué)肯定是會的,因?yàn)橹拔恼掠性敿?xì)說過。)     

實(shí)現(xiàn)過程

  • 在網(wǎng)關(guān)         cloud-gateway模塊編寫網(wǎng)關(guān)過濾器
@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)關(guān)時添加額外的Header,為了方便這里直接設(shè)置成固定值。

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

如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)

  • 編寫配置類,用于靈活控制服務(wù)是否允許繞過網(wǎng)關(guān)
@Data
@ConfigurationProperties(prefix = "javadaily.cloud")
public class CloudSecurityProperties {

    /**
     * 是否只能通過網(wǎng)關(guān)獲取資源
     * 默認(rèn)為True
     */
    private Boolean onlyFetchByGateway = Boolean.TRUE;

}
     
  • 編寫攔截器,用于校驗(yàn)請求是否經(jīng)過網(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("請通過網(wǎng)關(guā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());
    }
}
       
  • 編寫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
     
  • 在后端服務(wù)配置文件中添加屬性配置,默認(rèn)只能通過網(wǎng)關(guān)訪問
javadaily:
  cloud:
    onlyFetchByGateway: true
     

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

  • 后端服務(wù)引用此公共Starter模塊即可,以         account-service為例
<dependency>
 <groupId>com.jianzh6.cloud</groupId>
 <artifactId>cloud-component-security-starter</artifactId>
</dependency>
           

實(shí)現(xiàn)效果

直接訪問后端服務(wù)接口  
http://localhost:8010/account/getByCode/jianzh6

如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)

返回結(jié)果:

{
  "message": "請通過網(wǎng)關(guān)訪問資源",
  "status": 403,
  "success": false,
  "timestamp": 1611660015830
}

感謝各位的閱讀,以上就是“如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

網(wǎng)站標(biāo)題:如何防止請求繞過網(wǎng)關(guān)直接訪問后端服務(wù)
本文路徑:http://aaarwkj.com/article24/gjcsce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、、定制網(wǎng)站、App設(shè)計(jì)、網(wǎng)站維護(hù)品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
色哟哟网站在线精品视频 | 国产黄片免费高清观看| 欧美日韩国产激情高清| 高清国语对白刺激av在线播放| 国产丝袜在线精品丝袜不卡| 日韩欧美亚洲国产每日更新| 中文字幕二区三区av| av免费在线不卡一区| 91在线国产手机视频| 国产精品国产不卡在线| 久久av天堂在线观看| 国产高清自拍视频在线一区| 国产91黑丝视频在线观看| 伊人亚洲一区二区三区| 黄色午夜福利在线观看| 日本一欧美一欧美一亚洲| 可以免费看的欧美黄片| 日本加勒比一道本东京热| 欧美十八一区二区三区| 亚洲黄片在线免费播放观看| 亚洲av色香蕉一区二区| av亚洲天堂免费观看| 亚洲 欧美 日韩一区| 国产成人免费视频大全| 97成人在线免费视频| 蜜臀久久精品亚洲一区| 尤物在线观看精品视频| 日韩欧美中文字幕区| 成人影院视频在线播放| 少妇高潮时会抱紧男人脖子| 国产精品白丝一区二区三区| 欧美成人免费做真爱高潮| 欧美一区二区日韩国产| 午夜最新福利在线视频| 欧洲亚洲精品免费二区| 国产一区二区欧美久久| 天堂av在线网址观看| 手机在线观看av网站| 99热精品免费在线观看| 日韩精品亚洲一级在线观看| 国产精品播放一区二区三区|