복붙노트

[SPRING] Spring 기반 웹 애플리케이션에서 만료 된 세션 처리

SPRING

Spring 기반 웹 애플리케이션에서 만료 된 세션 처리

내 응용 프로그램에서 스프링 보안 기능을 사용하고 있지만 세션이 만료되면 모든 요청 아약스가 login.jsp (HTTP 응답에서 리다이렉트하지 않고 모든 html 콘텐츠를 넣음) 페이지를 반환한다는 사실을 알게되었습니다.이 페이지는 로그인 페이지입니다. 내 webapp. 내 애플 리케이션에서 많은 아약스 요청을 사용하고 목표는 로그인 페이지 대신 510과 같은 특정 오류 코드를 반환합니다.

<session-management session-authentication-strategy-ref="example" /> 

invalid-session-url없이 invalid-session-url = ""을 (를) 만들려고했으나 작동하지 않습니다. 많은 감사

해결법

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

    1.사용자 지정 AuthenticationEntryPoint 사용 :

    사용자 지정 AuthenticationEntryPoint 사용 :

    package com.example.spring.security
    // imports here
    
    public class AjaxAwareAuthenticationEntryPoint
         extends LoginUrlAuthenticationEntryPoint {
    
      public AjaxAwareAuthenticationEntryPoint(final String loginFormUrl) {
        super(loginFormUrl);
      }
    
      @Override
      public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException)
          throws IOException, ServletException {
    
        if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
          response.sendError(403, "Forbidden");
        } else {
          super.commence(request, response, authException);
        }
      }
    }
    

    빈을 정의하고 요소에서 entry-ref로 사용합니다.

    <http entry-point-ref="authenticationEntryPoint">
      <!-- more configuration here -->
    </http>
    
    <bean id="authenticationEntryPoint"
       class="com.example.spring.security.AjaxAwareAuthenticationEntryPoint">
     <constructor-arg value="/login.jsp"/>
    </bean>
    
  2. from https://stackoverflow.com/questions/11242174/handle-session-expired-event-in-spring-based-web-application by cc-by-sa and MIT license