2012-06-28 3 views
2

내 응용 프로그램에서 스프링 보안 기능을 사용하고 있지만 세션이 만료되면 모든 요청 아약스가 login.jsp 페이지를 반환한다는 것을 알았습니다 (HTTP 응답에서 리디렉션되지 않음, 내 웹 응용 프로그램의 로그인 페이지 인 모든 html 콘텐츠를 넣습니다. 내 앱에서 Ajax 요청을 많이 사용했으며 목표는 로그인 페이지 대신 510과 같은 특정 오류 코드를 반환하는 것입니다. 유효하지 않은 세션-URL 없이스프링 기반 웹 응용 프로그램에서 세션 만료 이벤트 처리

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

나는 잘못된 세션 - URL이 = ""작동하지 않습니다 만들기 위해 노력했다. 많은 감사

답변

3

를 사용하여 사용자 정의 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); 
    } 
    } 
} 

빈을 정의하고 <http> elemententry-point-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> 
관련 문제