๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๋ชฉ๋ก์ด ์—†์Šต๋‹ˆ๋‹ค.

Spring framework ๋ฐ Tomcat ์—์„œ SSL ์ธ์ฆ์„œ ์ž๋™ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ(HTTP to HTTPS Redirect)

๐Ÿ”‘ Basic Concept

    Spring framework ๊ทธ๋ฆฌ๊ณ  Tomcat์„ ํ†ตํ•ด์„œ ์›น ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ „์ œํ•˜์— ํฌ์ŠคํŒ…์„ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    ์Šค๋งˆํŠธํฐ์ด ๋ณด๊ธ‰๋˜๊ณ  ์ธํ„ฐ๋„ท์ด ํ™œ๋ฐœํ•ด์ง€๊ณ  ์›น ๋˜ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ๋งŽ์ด ๋Š˜์–ด๋‚˜๋ฉด์„œ ์›น ๋ณด์•ˆ์— ๋Œ€ํ•ด ์ค‘์š”์‹œํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ์š”. ์ด ๋•Œ๋ฌธ์— ์š”์ฆ˜ ์›น ์‚ฌ์ดํŠธ์—์„œ SSL ์ธ์ฆ์„œ๊ฐ€ ๋ถ™์ง€ ์•Š์€ ์‚ฌ์ดํŠธ๋Š” ์ตœ์‹  ๋ธŒ๋ผ์šฐ์ €๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ ๋ฌด์„ญ๊ฒŒ ๊ฒฝ๊ณ ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค.

     

     

    SSL ์ธ์ฆ์„œ ์—†๋Š” ์‚ฌ์ดํŠธ๋ฅผ ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ ‘์† ์‹œ ๋‚˜ํƒ€๋‚˜๋Š” ํ™”๋ฉด

     

    ๊ทธ๋ฆฌ๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์›น์„œ๋ฒ„์™€ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ฃผ๊ณ  ๋ฐ›์œผ๋ฉด์„œ ์›น ์„œ๋ฒ„์ชฝ์— ์ธ์ฆ์„œ๊ฐ€ ๋ถ™์–ด์žˆ๋Š” ๊ฒฝ์šฐ ์•”ํ˜ธํ™”๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ์ข€ ๋” ์•ˆ์ „ํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธ€์—์„œ๋Š” ์ธ์ฆ์„œ๋ฅผ ๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•˜์ง€ ์•Š๊ณ  ์ธ์ฆ์„œ๋ฅผ ๋ถ™์ธ ํ›„์— ์‚ฌ์šฉ์ž(ํด๋ผ์ด์–ธํŠธ)๊ฐ€ http ์ฃผ์†Œ๋กœ ์ ‘์†ํ–ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ https๋กœ ์ ‘์†๋˜๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

     

     

    ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ธ์ •ํ•˜์ง€ ์•Š๋Š” ์ธ์ฆ์„œ ํ‘œ์‹œ

    ์ฐธ๊ณ ๋กœ ์ œ๋Œ€๋กœ ์ธ์ฆ๋˜์ง€ ์•Š์€ ์ธ์ฆ์„œ๋ฅผ ๋ถ™์ธ ๊ฒฝ์šฐ ์˜ˆ๋ฅผ ๋“ค์–ด, ์ž์ฒด์ ์œผ๋กœ ์ƒ์‚ฐํ•œ ์ธ์ฆ์„œ๋ฅผ ์„œ๋ฒ„์— ์ ์šฉํ•˜๋ฉด ์œ„์˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ๋ธŒ๋ผ์šฐ์ €์—์„œ URI๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๋ถ€๋ถ„์— ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค๊ณ  ํ‘œ์‹œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

     

    ์„œ๋ฒ„ํ™˜๊ฒฝ

    Tomcat 9.0.52
    Spring framework 4.3.20.RELEASE

     

     

    ์ž‘์—…

    1. Tomcat server.xml ์ธ์ฆ์„œ ์ ์šฉ
    2. web.xml ์ž๋™ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์„ค์ •

     

    1. Tomcat server.xml ์ธ์ฆ์„œ ์ ์šฉ

    ์šฐ์„  tomcat ์„œ๋ฒ„์˜ conf ๋””๋ ‰ํ† ๋ฆฌ ์œ„์น˜์— ์ ์šฉํ•  ์ธ์ฆ์„œ ํŒŒ์ผ์„ ์—…๋กœ๋“œ ํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” jksํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ์ธ์ฆ์„œ ํŒŒ์ผ ์—…๋กœ๋“œ

    drwx------  3 apptm apptm   4096 10์›” 28 19:03 ./
    drwxrwxr-x 10 apptm apptm   4096  9์›”  1 10:07 ../
    drwxr-x---  6 apptm apptm   4096  8์›” 25 16:46 Catalina/
    -rw-rw-r--  1 apptm apptm   6917  8์›” 25 17:01 ์ธ์ฆ์„œ.jks
    -rw-------  1 apptm apptm  13052  7์›” 31 13:12 catalina.policy
    -rw-------  1 apptm apptm   7262  7์›” 31 13:12 catalina.properties
    -rw-------  1 apptm apptm   1400  7์›” 31 13:12 context.xml
    -rw-------  1 apptm apptm   1149  7์›” 31 13:12 jaspic-providers.xml
    -rw-------  1 apptm apptm   2313  7์›” 31 13:12 jaspic-providers.xsd
    -rw-------  1 apptm apptm   4144  7์›” 31 13:12 logging.properties
    -rw-------  1 apptm apptm   9806 10์›” 27 14:36 server.xml
    -rw-------  1 apptm apptm   2756  7์›” 31 13:12 tomcat-users.xml
    -rw-------  1 apptm apptm   2558  7์›” 31 13:12 tomcat-users.xsd
    -rw-------  1 apptm apptm 172359  7์›” 31 13:12 web.xml

    ์ธ์ฆ์„œ ์ด๋ฆ„์€ ์–ด๋–ค ์ด๋ฆ„์ด์–ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. ์‚ฌ์‹ค ์ธ์ฆ์„œ ์œ„์น˜๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค. server.xml์— ์„ค์ •์„ ์ž˜ ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์„ค๋ช…์„ ์œ„ํ•ด์„œ ํŒŒ์ผ์ด๋ฆ„์„ ์ธ์ฆ์„œ.jks๋กœ ์—…๋กœ๋“œ ํ–ˆ์Šต๋‹ˆ๋‹ค.

     

     

    server.xml ์ˆ˜์ •

    ์„ค์น˜๋œ ํ†ฐ์บฃ ๋ฒ„์ „์—๋Š” SSL ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด ์˜ˆ์‹œ๋กœ ๋“ค์–ด์žˆ๊ณ  ์ฃผ์„์ฒ˜๋ฆฌ๊ฐ€ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฃผ์„์„ ํ•ด์ œํ•˜๊ณ  ์•Œ๋งž๊ฒŒ ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

        <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="443"/>
        <!-- A "Connector" using the shared thread pool-->
        <!--
        <Connector executor="tomcatThreadPool"
                   port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" />
        -->
        <!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
             This connector uses the NIO implementation. The default
             SSLImplementation will depend on the presence of the APR/native
             library and the useOpenSSL attribute of the
             AprLifecycleListener.
             Either JSSE or OpenSSL style configuration may be used regardless of
             the SSLImplementation selected. JSSE style configuration is used below.
        -->
        <Connector port="8443"
                   protocol="org.apache.coyote.http11.Http11NioProtocol"
                   maxThreads="150"
                   SSLEnabled="true"
                   defaultSSLHostConfigName="๊ธฐ๋ณธํ˜ธ์ŠคํŠธ๋ช…">
    
            <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    
            <SSLHostConfig hostName="ํ˜ธ์ŠคํŠธ๋ช…">
                <Certificate certificateKeystoreFile="conf/์ธ์ฆ์„œ.jks"
                             certificateKeystorePassword="์ธ์ฆ์„œ๋น„๋ฐ€๋ฒˆํ˜ธ"
                             type="RSA" />
            </SSLHostConfig>
        </Connector>

    ์ธ์ฆ์„œ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ž…๋ ฅํ•œ ํ˜ธ์ŠคํŠธ๋ช…๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ทธ๋ฆฌ๊ณ  ์ธ์ฆ์„œ ํŒŒ์ผ์˜ ์œ„์น˜๋ฅผ ์•Œ๋งž์€ ์œ„์น˜์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

    ๊ทธ๋Ÿฌ๋ฉด ์ธ์ฆ์„œ ์ ์šฉ์ด ๋๋‚ฉ๋‹ˆ๋‹ค! ๋งŒ์•ฝ 443ํฌํŠธ ๋˜๋Š” 8443ํฌํŠธ(์ง€์ •ํ•œํฌํŠธ)๋กœ ์›น์„œ๋ฒ„์— ์ ‘์†์ด ์•ˆ๋œ๋‹ค๋ฉด ๋ฐฉํ™”๋ฒฝ ๋˜๋Š” iptable ์„ค์ • ๋“ฑ ๋„คํŠธ์›Œํฌ ์„ค์ •์„ ํ™•์ธํ•ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

     

    2. web.xml ์„ค์ •

    Tomcat๋งŒ ์„ค์ •ํ•˜๊ณ  ๋งˆ๋ฌด๋ฆฌํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ์ž๊ฐ€ https ๊ฐ€ ์•„๋‹Œ http ํ”„๋กœํ† ์ฝœ๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ๋˜๋ฉด ์ธ์ฆ์„œ๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์€์ฒด ์‚ฌ์ดํŠธ์— ์ ‘๊ทผ์ด ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ํ˜„์ƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ http ์ ‘์† ์‹œ ์ž๋™์œผ๋กœ https๋กœ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ํ•ด์ฃผ๋„๋ก web.xml์„ ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

     

      ... 
    
      <security-constraint>
          <web-resource-collection>
              <web-resource-name>HTTPS or HTTP</web-resource-name>
              <url-pattern>/web/*</url-pattern>
          </web-resource-collection>
          <user-data-constraint>
              <transport-guarantee>NONE</transport-guarantee>
          </user-data-constraint>
      </security-constraint>
    
      <security-constraint>
              <web-resource-collection>
                      <web-resource-name>SSL Forward</web-resource-name>
                      <url-pattern>/*</url-pattern>
              </web-resource-collection>
              <user-data-constraint>
                      <transport-guarantee>CONFIDENTIAL</transport-guarantee>
              </user-data-constraint>
      </security-constraint>
    </web-app>

    ์œ„์˜ ์„ค์ •์„ web-app ํƒœ๊ทธ ์•ˆ์— ๋งจ ์•„๋ž˜์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

     

    ์ด๋ ‡๊ฒŒ Tomcat, Spring framework ๊ธฐ๋ฐ˜ ์›น ์„œ๋ฒ„ SSL ์ธ์ฆ์„œ ์ ์šฉ ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

    ๋ถ€์กฑํ•œ ๊ธ€ ์ฐพ์•„์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

     

    ์ž˜๋ชป๋œ ๋ถ€๋ถ„์ด ์žˆ์œผ๋ฉด ์ง€์ ํ•ด์ฃผ์‹œ๊ณ  ์–ด๋–ค ํ”ผ๋“œ๋ฐฑ๋„ ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.