欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java設計模式之外觀模式

 更新時間:2023年06月14日 11:04:04   作者:啵啵腸  
在Java開發(fā)中,設計模式是一種十分常見的編程思想,它可以幫助我們解決很多實際開發(fā)中的問題,本篇文章將介紹一種常見的設計模式——外觀模式,并結(jié)合實際的開發(fā)場景進行講解,需要的朋友可以參考下

重學 Java 設計模式:實戰(zhàn)外觀模式

在 Java 開發(fā)中,設計模式是一種十分常見的編程思想,它可以幫助我們解決很多實際開發(fā)中的問題。本篇文章將介紹一種常見的設計模式——外觀模式,并結(jié)合實際的開發(fā)場景進行講解。本文將以 SpringBoot 開發(fā)門面模式中間件,實現(xiàn)一個統(tǒng)一控制接口白名單的場景來展示外觀模式的應用。

什么是外觀模式?

外觀模式是一種結(jié)構(gòu)型設計模式,它可以為一組復雜的子系統(tǒng)提供一個簡單的接口。通過使用外觀模式,我們可以將系統(tǒng)中的復雜邏輯隱藏起來,并對外提供簡單的接口。外部調(diào)用者無需關(guān)心具體的執(zhí)行細節(jié),只需要調(diào)用外觀方法即可。外觀模式可以減少系統(tǒng)的耦合度,提高系統(tǒng)的可維護性和可擴展性。

SpringBoot 開發(fā)門面模式中間件

在實際的開發(fā)中,我們可能會遇到這樣的需求:需要對系統(tǒng)中的接口進行控制,只允許某些特定的 IP 地址訪問特定的接口,這時候,外觀模式就可以幫助我們實現(xiàn)這個功能。

我們可以使用 SpringBoot 開發(fā)一個門面模式中間件,讓中間件來完成接口的白名單控制。中間件會檢查訪問的 IP 地址是否在白名單列表中。如果是,則繼續(xù)請求,如果不在,則直接返回錯誤信息。

接下來,我們將一步步地實現(xiàn)這個中間件,以介紹外觀模式的應用。

中間件基本流程

在實現(xiàn)中間件之前,我們需要先確定中間件的基本流程。具體來說,我們需要實現(xiàn)一個方法 handle,這個方法需要根據(jù)配置的白名單列表,對傳入的 HTTP 請求進行判斷,如果 IP 地址在白名單列表中,則正常處理請求。否則,直接返回錯誤信息。代碼如下所示:

public interface GatewayFilter {
?
 ? ?void handle(HttpServletRequest request, HttpServletResponse response, GatewayFilterChain filterChain) throws ServletException, IOException;
?
}

在這個方法中,我們需要調(diào)用 filterChain.doFilter 方法來處理請求,同時需要使用 response 對象來返回錯誤信息。

中間件的具體實現(xiàn)

現(xiàn)在,我們已經(jīng)確定了中間件的基本流程,接下來,我們需要根據(jù)這個流程來實現(xiàn)具體的中間件。在這個例子中,我們使用了 SpringBoot 開發(fā)工具來實現(xiàn)代碼的自動配置功能。

首先,需要創(chuàng)建一個 GatewayProperties 類來保存配置的白名單列表。

@ConfigurationProperties("gateway")
public class GatewayProperties {
 ? ?// 配置白名單列表
 ? ?private List<String> allowList = new ArrayList<>();
?
 ? ?public List<String> getAllowList() {
 ? ? ? ?return allowList;
 ?  }
?
 ? ?public void setAllowList(List<String> allowList) {
 ? ? ? ?this.allowList = allowList;
 ?  }
}

然后,需要創(chuàng)建一個 GatewayFilter 接口,并實現(xiàn)白名單過濾邏輯。

public class GatewayFilterImpl implements GatewayFilter {
?
 ? ?public static final String X_REAL_IP = "X-Real-IP";
 ? ?private List<String> allowList;
?
 ? ?public GatewayFilterImpl(List<String> allowList) {
 ? ? ? ?this.allowList = allowList;
 ?  }
?
 ? ?@Override
 ? ?public void handle(HttpServletRequest request, HttpServletResponse response, GatewayFilterChain filterChain) throws ServletException, IOException {
 ? ? ? ?String remoteAddr = request.getHeader(X_REAL_IP);
 ? ? ? ?if (validateIp(remoteAddr)) {
 ? ? ? ? ? ?filterChain.doFilter(request, response);
 ? ? ?  } else {
 ? ? ? ? ? ?response.sendError(HttpStatus.UNAUTHORIZED.value(), "Your IP address is not allowed to access the API.");
 ? ? ?  }
 ?  }
?
 ? ?// 驗證當前 IP 是否在白名單列表中
 ? ?private boolean validateIp(String remoteAddr) {
 ? ? ? ?if (allowList == null || allowList.isEmpty()) {
 ? ? ? ? ? ?return true;
 ? ? ?  }
?
 ? ? ? ?return allowList.contains(remoteAddr);
 ?  }
}

在這里,我們從請求頭中獲取了客戶端的 IP 地址,并使用 validateIp 方法來判斷這個 IP 是否在白名單列表中。如果符合要求,則調(diào)用 filterChain.doFilter 方法進行下一步處理。否則,直接返回錯誤信息。

最后,需要創(chuàng)建一個 GatewayAutoConfiguration 類,用于自動配置當前的中間件。

@Configuration
@ConditionalOnClass(GatewayFilter.class)
@ConditionalOnProperty(prefix = "gateway", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(GatewayProperties.class)
public class GatewayAutoConfiguration {
?
 ? ?@Bean
 ? ?public GatewayFilter gatewayFilter(GatewayProperties gatewayProperties) {
 ? ? ? ?return new GatewayFilterImpl(gatewayProperties.getAllowList());
 ?  }
?
 ? ?@Bean
 ? ?public FilterRegistrationBean registration(GatewayFilter gatewayFilter) {
 ? ? ? ?FilterRegistrationBean registration = new FilterRegistrationBean();
 ? ? ? ?registration.setFilter(gatewayFilter);
 ? ? ? ?registration.addUrlPatterns("/*");
 ? ? ? ?registration.setName("gatewayFilter");
 ? ? ? ?registration.setOrder(Ordered.LOWEST_PRECEDENCE);
 ? ? ? ?return registration;
 ?  }
?
}

在這個類中,我們使用 @ConditionalOnProperty 來控制中間件是否開啟,使用 @ConfigurationProperties 來讀取配置的白名單配置信息,并使用 GatewayFilterImpl 類來處理請求。

使用中間件

使用該中間件很簡單,只需要在 application.properties 中配置白名單列表即可。例如:

# application.properties
?
gateway.enabled=true
gateway.allowList=127.0.0.1,192.168.1.2

在這里,我們配置了兩個 IP 地址,分別是 127.0.0.1192.168.1.2。如果請求的 IP 地址不在這個列表中,則無法訪問 API。

總結(jié)

外觀模式是一種常見的設計模式,它可以為一組復雜的子系統(tǒng)提供一個簡單的接口。通過使用外觀模式,我們可以將系統(tǒng)中的復雜邏輯隱藏起來,并對外提供簡單的接口,從而減少系統(tǒng)的耦合度,提高系統(tǒng)的可維護性和可擴展性。

在本文中,我們以 SpringBoot 開發(fā)門面模式中間件為例,展示了外觀模式的應用。通過對請求進行白名單過濾,來限制客戶端對接口的訪問。通過合理地使用外觀模式,我們可以減少開發(fā)工作量,提高代碼的可重用性和可維護性。

以上就是詳解Java設計模式之外觀模式的詳細內(nèi)容,更多關(guān)于Java 外觀模式的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論