[JAVA] https로 접근했는데 sendRedirect http로 리다이렉트 되는 현상

[JAVA] https로 접근했는데 sendRedirect http로 리다이렉트 되는 현상

response.sendRedirect(“/test/index.jsp”);

위와 같이 sendRedirect 했을 때 https로 리다이렉트되지 않고 http로 리다이렉트 되는 현상 해결방법.

실제로 백엔드 자바 단(WAS 단)에서 request 객체의 scheme이나 isSecure 값을 가져왔을 때 SSL이 적용되지 않은 상태로 인식되는 현상이다.

확인코드

System.out.println(“protocol: “ + request.getProtocol());
System.out.println(“port : “ + request.getServerPort());
System.out.println(“scheme : “ + request.getScheme());
System.out.println(“isSecure: “ + request.isSecure());

결과

protocol: HTTP/1.1

port : 80

scheme : http

isSecure: false

1. WAS가 톰캣인 경우 설정방법

웹서버(아파치 웹서버) – WAS(Tomcat) 구조에서, 웹서버에는 SSL 인증서가 입혀져 있고, WAS(Tomcat)에는 SSL 인증서가 입혀져 있지 않은 경우.

웹서버를 통해 HTTPS 프로토콜로 접속하더라도 WAS에는 인증서가 입혀져 있지 않으므로, 백엔드 자바 단에서는 HTTP 프로토콜 및 80 포트로 접근했다고 인식되는 문제이다.

톰캣의 server.xml 파일의 Connector 태그에 proxyPort=”443″ scheme=”https” 설정을 추가하면 된다.

<Service name=”Catalina”>

    <Connector proxyName=”example.com” proxyPort=”443″ scheme=”https” URIEncoding=”UTF-8″ port=”8000″ protocol=”HTTP/1.1″ redirectPort=”8443″ />

    <Engine defaultHost=”localhost” name=”Catalina”>

        <Host appBase=”webapps” autoDeploy=”true” name=”localhost” unpackWARs=”true” xmlNamespaceAware=”false” xmlValidation=”false”> <Context docBase=”” path=”/” reloadable=”false” /></Host>

    </Engine>

</Service>

참고사이트 : https://hulint.tistory.com/47

2. WAS가 오라클 웹로직(Weblogic)인 경우 설정방법

웹서버(아파치 웹서버 또는 OHS) – WAS(Weblogic) 구조에서, 웹서버에는 SSL 인증서가 입혀져 있고, WAS(Weblogic)에는 SSL 인증서가 입혀져 있지 않은 경우.

웹서버를 통해 HTTPS 프로토콜로 접속하더라도 WAS에는 인증서가 입혀져 있지 않으므로, 백엔드 자바 단에서는 HTTP 프로토콜 및 80 포트로 접근했다고 인식되는 문제이다.

(1) Weblogic 설정 변경

Weblogic 콘솔에 접속해서 해당 웹 어플리케이션의 Weblogic Plugin Enabled 체크박스를 체크처리하면 된다.

Weblogic 콘솔 접속

도메인명 -> configuration 탭 -> Web Applications 탭 -> Weblogic Plugin Enabled 체크하기

참고사이트 1 : https://doohans.github.io/ohs_weblogic_https_scheme/

참고사이트 2 : https://www.python2.net/questions-1070920.htm

 

(2) 웹서버 설정 변경 검토

만약 위의 Weblogic 설정 변경으로 해결되지 않으면 웹서버의 설정도 확인해봐야 한다.

웹서버가 아파치일 경우 X-Forwarded-Proto 값을 헤더에 설정해야 하고, 웹서버가 OHS일 경우 WLProxySSLPassThrough On 설정이 필요하다.

(2-1) 웹서버가 아파치 웹서버인 경우

웹서버가 아파치일 경우 X-Forwarded-Proto 값을 헤더에 설정해야 한다.

<VirtualHost *:443>

    ServerName www.myapp.org

    ProxyPass / http://127.0.0.1:8080/

    RequestHeader set X-Forwarded-Proto

    https RequestHeader set X-Forwarded-Port 443

    ProxyPreserveHost On

    … (SSL directives omitted for readability)

</VirtualHost>

참고사이트 : https://idkook.tistory.com/75

(2-2) 웹서버가 OHS인 경우

여기서 OHS 란 Oracle HTTP Server의 약자로 아파치처럼 웹서버의 일종이다. 이 문제에 대한 해결책은 두 가지가 있다.

1) obj.conf 파일에 아래의 파라미터를 추가하기

WLProxySSL=”ON”

WLProxySSLPassThrough=”ON”

이어서 BIG IP Load Balancer에서 WL-Proxy-SSL=true 를 설정해야 한다.

2) WLProxySSL=”ON” WLProxySSLPassThrough=”ON” 등 플러그인의 파라미터나 WL-Proxy-SSL = true 헤더를 추가하지 않고도

BIG IP Loadbalancer의 “Rewrite Redirect” 옵션을 통해 문제를 해결할 수도 있다.

참고사이트 : https://support.oracle.com/knowledge/Middleware/2215493_1.html

3. 스프링을 사용하는 경우

스프링(Spring)에서 sendRedirect 시 http로 리다이렉트 되는 경우. redirectHttp10Compatible 관련 설정을 추가하면 된다고 한다.

(1) InternalResourceViewResolver 클래스를 사용하는 경우

<beans:bean p:order=”2″ class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>

    <beans:property name=”viewClass” value=”org.springbyexample.web.servlet.view.tiles2.DynamicTilesView” />

    <beans:property name=”prefix” value=”/WEB-INF/views/” />

    <beans:property name=”suffix” value=”.jsp” />

    <beans:property name=”redirectHttp10Compatible” value=”false” />

</beans:bean>

(2) UrlBasedViewResolver 클래스를 사용하는 경우

<beans:bean p:order=”1″ id=”viewResolver” class=”org.springframework.web.servlet.view.UrlBasedViewResolver”>

    <beans:property name=”viewClass” value=”org.springframework.web.servlet.view.tiles2.TilesView” />

    <beans:property name=”redirectHttp10Compatible” value=”false” />

</beans:bean>

참고사이트 : https://iamfreeman.tistory.com/entry/Spring-mvc%EC%97%90%EC%84%9C-redirect-https-http-redirect-%EB%AC%B8%EC%A0%9C-%ED%95%B4%EA%B2%B0