웹사이트 검색

Tomcat에서 SSL을 구성하고 HTTP에서 HTTPS로 자동 리디렉션을 설정하는 단계


SSL(Secure Socket Layer)은 인터넷을 통해 메시지 보안을 제공하는 암호화 프로토콜입니다. 개인 및 공개 키의 개념에서 작동하며 메시지는 네트워크를 통해 보내기 전에 암호화됩니다. Tomcat에서 SSL을 구성하려면 개발 환경용 Java keytool을 사용하여 생성할 수 있는 디지털 인증서가 필요합니다. 프로덕션 환경의 경우 SSL 인증서 공급자(예: Verisign, Entrust, Lets’ Encrypt)로부터 디지털 인증서를 받아야 합니다.

SSL 인증서 생성

아래 단계에 따라 고유한 디지털 인증서를 만드십시오.

$ keytool -genkey -alias tomcat -keyalg RSA -keystore mycertificate.cert
Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  Pankaj Kumar
What is the name of your organizational unit?
  [Unknown]:  Dev
What is the name of your organization?
  [Unknown]:  JournalDev
What is the name of your City or Locality?
  [Unknown]:  Bangalore
What is the name of your State or Province?
  [Unknown]:  Karnataka
What is the two-letter country code for this unit?
  [Unknown]:  IN
Is CN=Pankaj Kumar, OU=Dev, O=JournalDev, L=Bangalore, ST=Karnataka, C=IN correct?
  [no]:  Yes

Enter key password for <tomcat>
	(RETURN if same as keystore password):
Re-enter new password:
$ ls
mycertificate.cert

Keystore와 key에 "changeit\ 암호를 사용했지만 원하는 것을 사용할 수 있습니다. 이제 디지털 인증서가 준비되었으며 다음 단계는 Tomcat에서 HTTPS 통신 포트를 활성화하고 SSL을 제공하기 위해 디지털 인증서를 사용하도록 설정하는 것입니다. 지원하다.

톰캣 HTTPS

SSL을 활성화하려면 ~Tomcat_Installation/conf/server.xml 파일을 열고 다음 행의 주석을 제거하십시오.

<Connector port="8443" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" scheme="https" secure="true"
               keystoreFile="/Users/Pankaj/tomcat/conf/mycertificate.cert"
	       clientAuth="false" sslProtocol="TLS" />

HTTP를 HTTPS로 Tomcat 리디렉션

따라서 HTTP 및 HTTPS 포트 모두에서 모든 웹 애플리케이션에 액세스할 수 있습니다. 일부 구성을 사용하여 모든 HTTP 요청을 HTTPS 포트로 리디렉션하도록 tomcat을 설정할 수 있습니다.

  1. In ~TomcatInstallation/conf/server.xmlFor HTTP Connector, set the redirect port to the HTTPS connector port. It will look somewhat like this:

    <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
        <Connector port="8090" maxHttpHeaderSize="8192"
                   maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
                   enableLookups="false" redirectPort="8443" acceptCount="100"
                   connectionTimeout="20000" disableUploadTimeout="true" />
    </pre>
    </li>
    <li>In ~TomcatInstallation/conf/web.xml
    
    Add below configuration but make sure to add it after all the servlet-mapping tags.
    
    <pre>
    <!-- added by Pankaj for automatic redirect from HTTP to HTTPS -->
    <security-constraint>
    <web-resource-collection>
    <web-resource-name>Entire Application</web-resource-name>
    <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
    </security-constraint>
    

지금 Tomcat을 다시 시작하면 모든 HTTP 요청이 자동으로 HTTPS(예: https://localhost:8443/axis2)로 리디렉션됩니다. 참고: URL에 포트를 제공하지 않으려면 HTTP에 80을 사용하고 HTTPS에 443을 사용하십시오. 이 경우 기본 포트 443을 자동으로 선택하기 때문에 HTTP 요청을 자동으로 HTTPS로 리디렉션하는 첫 번째 단계를 건너뛸 수 있습니다. 업데이트: Tomcat에서 작업하는 경우 다음 게시물에 관심이 있을 수 있습니다.

  • Java 웹 애플리케이션 자습서
  • 자바 서블릿 자습서