我想知道是否有任何模式或實作允許我通過使用另一個功能標志服務提供的簡單布爾變數來一次啟用/禁用給定 Spring Boot 應用程式的所有控制器。
我正在考慮在每個控制器路徑中進行條件檢查,但這是一種非常糟糕的方法。
提前致謝
uj5u.com熱心網友回復:
您可以定義自定義過濾器并注冊它。示例過濾器代碼位于。
public class RequestAllowDenyFilter implements Filter {
private boolean isAllowed = true;
public RequestAllowDenyFilter(boolean isAllowed) {
this.isAllowed = isAllowed;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (isAllowed) {
chain.doFilter(request, response);
} else {
((HttpServletResponse) response).setStatus(HttpStatus.SERVICE_UNAVAILABLE.value());
}
}
}
然后,當您注冊過濾器時,您需要通過是否允許/拒絕請求。
@Value("${request.enabled:true}")
private boolean isEnabled;
@Bean
public FilterRegistrationBean<RequestAllowDenyFilter> loggingFilter() {
FilterRegistrationBean<RequestAllowDenyFilter> registrationBean
= new FilterRegistrationBean<>();
registrationBean.setFilter(new RequestAllowDenyFilter(isEnabled));
registrationBean.addUrlPatterns("/**");
registrationBean.setOrder(0);
return registrationBean;
}
您需要request.enabled在 application.properties 檔案中定義。根據您想要做的事情是真/假。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/514375.html
標籤:爪哇弹簧靴特征标志
