웹 개발

spring boot] URL에서 jsessionid 제거

노루아부지 2020. 12. 20. 00:34

jsessionid는 새 세션이 만들어지면 클라이언트가 쿠키를 지원하는지 여부를 서버가 알 수 없으므로, 쿠키와 URL에 모두 jsessionid를 만들어 주는 것을 의미하며 url에 붙이거나 헤더에 붙여서 나오게 됩니다.

클라이언트가 두 번째 요청부터 세션 쿠키를 보내오면 URL에 jsessionid를 붙이지 않고, 쿠키가 없으면 계속 URL에 jsessionid를 붙이게 됩니다.

 

그런데 문제는, jsessionid를 탈취당하면 사용자 ID, 비밀번호를 몰라도 접근이 가능하게 됩니다.

그래서 웹 보안취약점 점검에서는 jsessionid를 꼭 제거하게 되어있습니다.

 

방법 1. application.properties에 아래와 같은 옵션을 추가합니다.

server.session.tracking-modes=cookie

 

방법2.

@Bean
public ServletContextInitializer servletContextInitializer() {
  return new ServletContextInitializer() {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
      servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
      SessionCookieConfig sessionCookieConfig=servletContext.getSessionCookieConfig();
      sessionCookieConfig.setHttpOnly(true);
    }
  };
}

 

 

방법 3.

import java.util.Collections;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // This can be done here or as the last step in the method
        // Doing it in this order will initialize the Spring
        // Framework first, doing it as last step will initialize
        // the Spring Framework after the Servlet configuration is 
        // established
        super.onStartup(servletContext);

        // This will set to use COOKIE only
        servletContext
            .setSessionTrackingModes(
                Collections.singleton(SessionTrackingMode.COOKIE)
        );
        // This will prevent any JS on the page from accessing the
        // cookie - it will only be used/accessed by the HTTP transport
        // mechanism in use
        SessionCookieConfig sessionCookieConfig=
                servletContext.getSessionCookieConfig();
        sessionCookieConfig.setHttpOnly(true);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootrApplication.class);
    }
}
728x90
loading