![](/img/trans.png)
[英]Jersey 1.x and Tomcat: how to register a ContainerRequestFilter programmatically, i.e. without web.xml?
[英]Is there a way to programmatically set SSL as mandatory in Tomcat (e.g. not via web.xml)?
我們有一個使用Tomcat 7部署在許多不同客戶端的java Web應用程序。我們的一個客戶希望我們強制使用SSL,以便強制執行https。 由於沒有SSL證書,因此其他部署應保留不使用SSL。
我們知道如何通過web.xml中的安全約束標記來強制執行它,但是我們想避免必須對單個客戶端進行特殊構建的麻煩,並且寧願能夠將其設置為數據庫參數,將由應用程序讀取,然后以編程方式強制執行SSL。
這可能嗎? 我們怎么做?
非常感謝提前。
Jonathas的
更新:我們需要的是,在這個特定的環境中,應用程序自動將por 80重定向到端口443,禁用端口80不是一個可行的選擇。
您可以通過ServletRequest.isSecure
定義一個驗證連接是否安全的過濾器。 如果不安全,並且上下文中的應用程序需要安全連接,則重定向到您的“https”端點。 否則,繼續。
偽代碼:
public class SecureConnectionFilter implements Filter {
private boolean requireSecure;
@Override
public void destroy() {
return;
}
@Override
public void doFilter(final ServletRequest request,
final ServletResponse response, final FilterChain filterChain)
throws IOException, ServletException {
if(requireSecure && ! (request.isSecure())) {
// Redirect to secure endpoint
} else {
filterChain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// Determine wether the application in context is required
// to be secure or not. If this information is not available
// at startup time then defer this logic to `doFilter`
}
}
如果這僅適用於一個客戶端,則只注釋server.xml中的普通http連接器(和相應的端口),並僅保留安全連接器和端口可用。
簡單的解決方案,不會影響您的代碼或其他部署,您的客戶端可以獲得他想要的東西
更新:
您可以重定向到https。 嘗試:
<Connector port="80" protocol="HTTP/1.1"
redirectPort="443"/>
也看到了這一點
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.