2,936
yesterday 1,791
visitor 24,291,866
9

https http 세션 공유 필터 방법(쿠키 복사)

조회 수 63253 추천 수 0 2018.09.20 11:26:50

https http 세션 공유가 안된다....

인터넷에 검색된 필터도 적용하는데 안된다...

왜그런지 모르지만 로그인후 세션 생성하기전 request.getSession().invalidate(); 을 실행해주니 정상적으로 필터에서

새로운 쿠키가 적용 되었다...

아래는 필터


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;

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
 }
}



import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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("/");
   }
   //cookie.setSecure(false);
   response.addCookie(cookie);

   setAttribute("COOKIE_OVERWRITTEN_FLAG", "true");
  }
 }
}


web.xml -> 최상위

 <filter>
  <filter-name>httpsFilter</filter-name>
  <filter-class>com.project.common.filter.HttpsFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>httpsFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>



2일정도 개삽질...

 

 

'헬로마켓'과 함께하는 스마트한 중고 아이템 거래

https://www.hellomarket.com

문서 첨부 제한 : 0Byte/ 2.00MB
파일 제한 크기 : 2.00MB (허용 확장자 : *.*)
List of Articles
번호 제목 글쓴이 날짜 조회 수

Program Note 로그인 :)