스프링/eGov

[eGov] http 로 온 것을 https 로 리다이렉트 하기. (HTTPS UrlRewrite filter (egovurlrewritefilter) 사용하기)

배고파요 2025. 7. 4. 17:45
728x90

전자정부프레임워크 사용하면  공통 컴포넌트에 있음. 

egovframework.com.cmm.util.egovurlrewritefilter

 

 

 

 

📍 나는 eGov에서 제공해주는 것 대로 쓸 수가 없어서 수정해서 사용했음!!

  • 1. 내가 web.xml 에 설정한 ip ( 127.0.0.1 ) 로 접근하면 
  • 2.  protocol이 http 인 지 확인해서
  • 3. https://localhost:8181/test/test.do. 로 보내고 싶다! 

 

 

 

 

 

 

📍 이건 자바단!

package egovframework.com.cmm.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.AntPathMatcher;

public class MyUrlRewriteFilter implements Filter {

	@SuppressWarnings("unused")
	private FilterConfig config;

	private String targetIp;
	private String redirctDomain;
    	private String httpsPort;
	private String[] targetIpPatterns;

    @Override
    public void init(FilterConfig config) throws ServletException {
    	System.out.println("MyUrlRewriteFilter--------------init");
    	String delimiter = ",";
		this.config = config;

		this.targetIp = config.getInitParameter("targetIp");
		this.redirctDomain = config.getInitParameter("redirctDomain");
		this.httpsPort = config.getInitParameter("httpsPort");
		this.targetIpPatterns = targetIp.split(delimiter);
    }

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
		System.out.println("MyUrlRewriteFilter--------------doFilter");
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		String uri = req.getRequestURI();
		String getProtocol = req.getScheme();
		String getDomain = req.getServerName();
		System.out.println(uri +"---" + getProtocol  +"---" + getDomain);
		
		AntPathMatcher pm = new AntPathMatcher();
		for (String ipPattern : targetIpPatterns) {
			if(pm.match(ipPattern.trim(), getDomain)) {
				System.out.println("MyUrlRewriteFilter--------------uriPattern-- " + ipPattern + "---" + uri + "---"+ (pm.match(ipPattern.trim(), getDomain)) );
				
				if(getProtocol.toLowerCase().equals("http")){
					 response.setContentType("text/html");
					 String httpsPath = "https" + "://" + redirctDomain + ":" + httpsPort + uri; // 개발서버 테스트
					 
					 String site = new String(httpsPath);
					 res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
					 res.setHeader("Location", site);
				}
			}
			/* 이건 원래 전자정부프레임워크의 소스
			if (pm.match(uriPattern.trim(), uri)) {

				 if (getProtocol.toLowerCase().equals("http")) {

					 response.setContentType("text/html");

					 String httpsPath = "https" + "://" + getDomain + ":" + httpsPort + uri;
					 String site = new String(httpsPath);
					 res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
					 res.setHeader("Location", site);

				 }

			}else if(getProtocol.toLowerCase().equals("https")){

				response.setContentType("text/html");

				String httpPath = "http" + "://" + getDomain + ":" + httpPort + uri;

				String site = new String(httpPath);
				res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
				res.setHeader("Location", site);

			}
			*/
		}

		chain.doFilter(req, res);

	}

    @Override
    public void destroy() {
    	this.targetIp = null;
    	this.redirctDomain = null;
        this.httpsPort = null;
		this.targetIpPatterns = null;
    }

}

 

 

 

 

 

 

 

📍 web.xml 에 작성!

<!-- web.xml -->

<filter>
	<filter-name>MyUrlRewriteFilter</filter-name>
	<filter-class>egovframework.com.cmm.util.MyUrlRewriteFilter</filter-class>
	<init-param>
		<param-name>targetIp</param-name>
		<param-value>127.0.0.1</param-value>
	</init-param>	
	<init-param>
		<param-name>redirectDomain</param-name>
		<param-value>localhost</param-value>
	</init-param>	
    <init-param>
		<param-name>httpsPort</param-name>
		<param-value>8181</param-value>
	</init-param>	
</filter>
<filter-mapping>
	<filter-name>MyUrlRewriteFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

 

 


출처 : 

https://www.egovframe.go.kr/wiki/doku.php?id=egovframework:com:v3:cmm:urlrewrite

 

 


개발 공부를 위한 블로그 입니다. 

오류가 있다면 댓글로 알려주세요! 

감사합니다.

728x90