SpringCloud Alibaba微服務實戰二十七 - 禁止直接訪問後端服務

前言

使用SpringCloud架構後我們希望所有的請求都需要經過網關才能訪問,在不作任何處理的情況下我們是可以繞過網關直接訪問後端服務的。如下,我們繞過網關直接訪問後端服務也是可以獲取到數據的。

那我們今天的議題就是 如何防止請求繞過網關直接訪問後端服務?

解決方案

我覺得防止繞過網關直接請求後端服務的解決方案主要有三種:

  • 使用Kubernetes部署

    在使用Kubernetes部署SpringCloud架構時我們給網關的Service配置NodePort,其他後端服務的Service使用ClusterIp,這樣在集羣外就只能訪問到網關了。

  • 網絡隔離

    後端普通服務都部署在內網,通過防火牆策略限制只允許網關應用訪問後端服務。

  • 應用層攔截

    請求後端服務時通過攔截器校驗請求是否來自網關,如果不來自網關則提示不允許訪問。

這裏我們着重關注在應用層攔截這種解決方案。

實現思路

實現思路其實也很簡單,在請求經過網關的時候給請求頭中增加一個額外的Header,在後端服務中寫一個攔截器,判斷請求頭是否與在網關設置的請求Header一致,如果不一致則不允許訪問並給出提示。

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

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

實現過程

  • 在網關 cloud-gateway模塊編寫網關過濾器
@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);
    }

}

在請求經過網關時添加額外的Header,爲了方便這裏直接設置成固定值。

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


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

    /**
     * 是否只能通過網關獲取資源
     * 默認爲True
     */

    private Boolean onlyFetchByGateway = Boolean.TRUE;

}


  • 編寫攔截器,用於校驗請求是否經過網關
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("請通過網關訪問資源");
            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


  • 在後端服務配置文件中添加屬性配置,默認只能通過網關訪問
javadaily:
  cloud:
    onlyFetchByGateway: true

經過以上幾步,一個公共的Starter模塊就構建完成了。

  • 後端服務引用此公共Starter模塊即可,以 account-service爲例
<dependency>
 <groupId>com.jianzh5.cloud</groupId>
 <artifactId>cloud-component-security-starter</artifactId>
</dependency>

實現效果

直接訪問後端服務接口  
http://localhost:8010/account/getByCode/jianzh5

返回結果:

{
  "message""請通過網關訪問資源",
  "status"403,
  "success"false,
  "timestamp"1611660015830
}


以上,希望對你有所幫助!



End




乾貨分享



這裏爲大家準備了一份小小的禮物,關注公衆號,輸入如下代碼,即可獲得百度網盤地址,無套路領取!

001:《程序員必讀書籍》
002:《從無到有搭建中小型互聯網公司後臺服務架構與運維架構》
003:《互聯網企業高併發解決方案》
004:《互聯網架構教學視頻》
006:《SpringBoot實現點餐系統》
007:《SpringSecurity實戰視頻》
008:《Hadoop實戰教學視頻》
009:《騰訊2019Techo開發者大會PPT》

010: 微信交流羣






近期熱文top



1、關於JWT Token 自動續期的解決方案

2、SpringBoot開發祕籍-事件異步處理

3、架構師之路-服務器硬件掃盲

4、基於Prometheus和Grafana的監控平臺 - 環境搭建

5、RocketMQ進階 - 事務消息



我就知道你“在看”





本文分享自微信公衆號 - JAVA日知錄(javadaily)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章