본문 바로가기
프로그래밍

tomcat http -> https로 리다이렉트

by 삼굽살 2022. 2. 19.
반응형

http로 접속해도 https 로 리다이렉트 하기

/opt/tomcat/conf/server.xml파일을 열어 아래 Connector태그를 추가한다.

Connector태그가 이미 있어도 새로 추가하면 된다.

<!-- http를 https로 리다이렉트 -->
<Connector URIEncoding="UTF-8" port="80" acceptCount="100" enableLookups="false" maxThreads="150" redirectPort="443" />

프로젝트의 web.xml 파일에 아래 태그를 추가한다.

<security-constraint>
	<web-resource-collection>
		<web-resource-name>Protected Context</web-resource-name>
		<url-pattern>/*</url-pattern>
	</web-resource-collection>
	<user-data-constraint>
		<transport-guarantee>CONFIDENTIAL</transport-guarantee>
	</user-data-constraint>
</security-constraint>

 

tomcat restart 

 

2.

 

server.xml에 SSL설정

$ tomcat/conf/server.xml

주석처리 되어있는 부분을 풀어 주시고

파일에 https 설정을 443 포트로 설정하고, http 설정을 80 포트로 설정해주세요.

그리고, 키 파일과 패스워드를 입력해주세요 !

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<server>
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

... 중간 생략...

<Connector port="80" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="443" />

<Connector port="443" protocol="org.apache.coyote.http11.Http11Protocol"
           maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
           clientAuth="false" sslProtocol="TLS"
           keystoreFile="{keystoreFile}" keystorePass="{keystorePass}" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="443" />

... 중간 생략...

</server>

 

톰캣설정이 끝났으면 자바 프로젝트에 web.xml 변경하러 !

 

web.xml에 리다이렉트 설정 추가한다.

web.xml 에 <security-constraint> 설정을 추가 후

Tomcat 을 재시작하면,

http로 접근을 했을때 https로 리다이렉트 잘 될꺼에요 !

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">

    ... 중간 생략...

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>HTTP</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

</web>

'프로그래밍' 카테고리의 다른 글

tomcat 무중단 배포  (0) 2022.03.05
Git Repository 변경하기  (0) 2022.02.20
letsencrypt ssl인증/갱신까지  (1) 2022.02.19
Oracle 데이터베이스 오류 코드 ORA-06504 설명  (0) 2022.01.12
ORA-00020 Error 오라클 에러  (0) 2021.10.15

댓글