tomcat - 将一个 tomcat 应用程序的不安全请求重定向到安全端口,并且不重定向另一个应用程序的请求

标签 tomcat redirect

我有两个 web 应用程序在 tomcat 7.0 下运行

  1. https://secure.example.com:8443
  2. http://insecure.example.com:8080

它们在 server.xml 中有两个独立的“主机”记录(不同的域,不同的位置)。

我需要第一个只能通过 HTTPS 访问。换句话说,我需要不安全的请求来保护要重定向到安全端口的应用程序。但不安全的应用程序仍然必须通过 HTTP 可用。

  • http://insecure.example.com:8080 - 好的
  • https://secure.example.com:8443 - 确定
  • http://secure.example.com:8080 --> https://secure.example.com:8443

我知道可以在不安全的连接器 (server.xml) 中指定“redirectPort”,但随后对任何应用程序(域)的 HTTP 请求将被重定向到安全端口。

是否可以使用单个 tomcat 实例进行配置?

最佳答案

这可以通过在 conf/server.xml 中设置单独的“服务”元素来实现。

例如你有

<Service name="Catalina">
  <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" />
  <Engine name="Catalina" defaultHost="insecure.example.com">
      <Host name="insecure.example.com"  appBase="insecure" unpackWARs="true" autoDeploy="true">
      </Host>
  </Engine>
</Service>

现在添加额外的服务部分

<Service name="SecureApps">
  <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
           maxThreads="150" scheme="https" secure="true"
            keystoreFile="/usr/local/tomcat/keys/keystore.p12" keystorePass="mySecret" keystoreType="pkcs12"
           clientAuth="false" sslProtocol="TLS" />
  <Engine name="SecureEngine" defaultHost="secure.example.com">
      <Host name="secure.example.com"  appBase="secure" unpackWARs="true" autoDeploy="true">
      </Host>
  </Engine>
</Service>

因此,安全应用程序将无法通过不安全的连接使用,因为 HTTP 端口由另一项服务提供服务。

关于 HTTP(8080)->HTTPS(8443) 重定向,在这样的配置中可能有更好的方法,但是可以设置第二个“Host”部分,其中 name="secure.example.com"inside "Catalina”服务,并部署一些包含简单 servlet 的 Web 应用程序,将任何请求重定向到指定的安全 url。

例如

网络.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>Redirect to secure port</display-name>
    <description>
        This is a simple web application which redirects you to secure port
    </description>

    <servlet>
        <servlet-name>RedirectServlet</servlet-name>
        <servlet-class>com.mycompany.RedirectServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>RedirectServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

RedirectServlet.java

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RedirectServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException
    {
        String url = "https://secure.example.com:8443/";

        response.sendRedirect(url);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException
    {
        String url = "https://secure.example.com:8443/";

        response.sendRedirect(url);

    }
}

关于tomcat - 将一个 tomcat 应用程序的不安全请求重定向到安全端口,并且不重定向另一个应用程序的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13642631/

相关文章:

apache - 将 HTTPS 重定向与 WWW 重定向合并

php - 在 .htaccess 中的重定向链接中添加问号

file - 使用 HTA 应用程序将 shell 应用程序输出重定向到文件

tomcat - Apache shiro - 我怎么知道缓存正在为授权工作?

tomcat - spring hessian 客户端套接字连接重置

tomcat - 如何在CentOS服务器上升级Tomcat 6到Tomcat 7

javascript - 无法使用 react-router 4 以编程方式重定向

php - Magento - URL 重写或重定向问题

java.lang.ClassNotFoundException : org. apache.catalina.core.ThreadLocalLeakPreventionListener 问题

tomcat - 避免Tomcat删除已部署的webapp文件夹