본문 바로가기

java

http <-> https 세션 공유

web.xml

<filter>

    <filter-name>httpsFilter</filter-name>

    <filter-class>com.nc.rsd.web.util.https.HttpsFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>httpsFilter</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter>

HttpsFilter 작성

public class HttpsFilter implements Filter {

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

HttpsRequestWrapper httpsRequest = new HttpsRequestWrapper(

(HttpServletRequest) request);

httpsRequest.setResponse((HttpServletResponse) response);

chain.doFilter(httpsRequest, response);

}

public void destroy() {

// TODO Auto-generated method stub

}


public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

}

HttpsRequestWrapper

public class HttpsRequestWrapper extends HttpServletRequestWrapper {

private HttpServletResponse response = null;


public HttpsRequestWrapper(HttpServletRequest request) {

super(request);

}


public void setResponse(HttpServletResponse response) {

this.response = response;

}


public HttpSession getSession() {

HttpSession session = super.getSession();

processSessionCookie(session);

return session;

}


public HttpSession getSession(boolean create) {

HttpSession session = super.getSession(create);

processSessionCookie(session);

return session;

}


private void processSessionCookie(HttpSession session) {

if (null == response || null == session)

return;


Object cookieOverWritten = getAttribute("COOKIE_OVERWRITTEN_FLAG");

if (null == cookieOverWritten && isSecure()

&& isRequestedSessionIdFromCookie() && session.isNew()) {

Cookie cookie = new Cookie("JSESSIONID", session.getId());

cookie.setMaxAge(-1);

String contextPath = getContextPath();

if ((contextPath != null) && (contextPath.length() > 0)) {

cookie.setPath(contextPath);

} else {

cookie.setPath("/");

}

response.addCookie(cookie);

setAttribute("COOKIE_OVERWRITTEN_FLAG", "true");

}

}

}


'java' 카테고리의 다른 글

character to be escaped is missing  (0) 2017.11.27
싱글톤 클래스 생성 팁  (0) 2017.06.15
로컬 서버 네트워크 정보  (0) 2013.10.29
피보나치 수열 간단 알고리즘  (0) 2013.09.26
java 엑셀 함수 NPV 구현  (0) 2013.07.09