복붙노트

[SPRING] 봄 부팅 : URL에서 jsessionid 제거

SPRING

봄 부팅 : URL에서 jsessionid 제거

내 URL에서 jsessionid를 제거하려면 어떻게해야합니까?

나는 Spring Security MVC (Spring Security없이; Tomcat embedded)를 사용하고있다.

나는 disableUrlRewriting을 "true"로 설정하여이 작업을 수행 할 수 있음을 읽었습니다. 하지만이 방법은 사용하지 않는 스프링 보안 솔루션처럼 보입니다 (로그인이없는 간단한 프로젝트, 페이지 만, 세션 컨트롤러가 있어야하며 세션 컨트롤러 여야합니다).

GoogleBot이 ID가 포함 된 URL을 작성 중이므로 요청하는 중입니다.

편집하다: https://randomcoder.org/articles/jsessionid-considered-harmful에 설명 된 솔루션으로 문제를 해결했습니다.

해결법

  1. ==============================

    1.나는 빠르고 더러운 스프링 부팅 응용 프로그램을 만들었고 여기서 내가 생각해 냈다.

    나는 빠르고 더러운 스프링 부팅 응용 프로그램을 만들었고 여기서 내가 생각해 냈다.

    생성 된 ServletInitializer는 다음과 같이 변경할 수 있습니다.

    package com.division6.bootr;
    
    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);
        }
    
    }
    

    작성자 노트

    나는 이것이 도입되었을 때 100 % 확실하지는 않지만 다음과 같은 매개 변수를 도입함으로써 코드를 작성하지 않고도 동일한 결과를 얻을 수 있습니다.

  2. ==============================

    2.이 질문은 스프링 부트 컨텍스트에서 나에게 쉬운 해결책은 다음과 같습니다.

    이 질문은 스프링 부트 컨텍스트에서 나에게 쉬운 해결책은 다음과 같습니다.

    server:
      session:
        tracking-modes: cookie
    

    appication.yml에 추가되어 임베디드 tomcat 설정을 수정합니다. ll 봄 부팅 속성 목록에서 : https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

  3. ==============================

    3.당신도 이것을 시도 할 수 있습니다.

    당신도 이것을 시도 할 수 있습니다.

            @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);
                        }
                    };
    
            }
    
  4. from https://stackoverflow.com/questions/31791587/spring-boot-remove-jsessionid-from-url by cc-by-sa and MIT license