【问题标题】:Redirect insecure requests of one tomcat application to secure port and don't redirect requests of another application将一个 tomcat 应用程序的不安全请求重定向到安全端口,并且不重定向另一个应用程序的请求
【发布时间】:2012-11-18 12:30:26
【问题描述】:

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

  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 实例进行配置?

【问题讨论】:

    标签: tomcat redirect


    【解决方案1】:

    这可以通过在 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) 重定向,在这样的配置中可能有更好的方法,但可以设置第二个“主机”部分,名称为“secure.example.com” Catalina”服务,并部署一些包含简单 servlet 的 Web 应用程序,将任何请求重定向到指定的安全 url。

    例如

    web.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);
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-16
      • 2016-08-19
      • 2016-01-26
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多