3

Tomcat 7.0 で 2 つの Web アプリケーションを実行しています。

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

server.xml に 2 つの別個の「ホスト」レコードがあります (異なるドメイン、別個の場所)。

最初の 1 つは HTTPS 経由でのみ利用できるようにする必要があります。言い換えれば、アプリケーションを安全なポートにリダイレクトするには、安全でない要求が必要です。ただし、安全でないアプリケーションは HTTP 経由で利用できる必要があります。

  • http://insecure.example.com:8080 - OK
  • https://secure.example.com:8443 - OK
  • http://secure.example.com:8080 --> https://secure.example.com:8443

安全でないコネクタ (server.xml) で「redirectPort」を指定できることはわかっていますが、アプリケーション (ドメイン) への HTTP 要求は安全なポートにリダイレクトされます。

単一の tomcat インスタンスでそれを構成することは可能ですか?

4

1 に答える 1

1

これは、conf/server.xml で別の「Service」要素を設定することで可能になります。

たとえば、あなたが持っている

<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) リダイレクトに関しては、おそらくそのような構成にもっと良い方法がありますが、「Catalina」サービス内に name="secure.example.com" で 2 番目の「Host」セクションをセットアップすることは可能です。を作成し、送信されたリクエストを指定された安全な URL にリダイレクトする単純なサーブレットを含む Web アプリケーションをデプロイします。

例えば

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);

    }
}
于 2012-12-13T23:26:08.253 に答える