1

我制作了一个 Spring MVC 应用程序,其中提供了注销功能。但是,每当我退出系统并按后退键时,我都会被重定向到上一页,这在理想情况下在退出时不应该发生。

请帮助我如何避免这种情况。

我的注销控制器部分如下:

@RequestMapping( value="/logout",method = RequestMethod.GET)
public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) 
{   
         session=request.getSession(false);
        if(session!=null)
        {
        l.info(""+session.getAttribute("username"));
        session.invalidate();   
        }
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Pragma","no-cache"); //HTTP 1.0    
        response.setHeader("Cache-Control","no-store"); //HTTP 1.1  
        response.setDateHeader ("Expires", 0); //prevents caching at the proxy server  
        String redirect="jsp/index";
        return new ModelAndView(redirect);
    }
4

1 回答 1

3

我已经尝试过了,它对我来说很好用。将此处理程序添加到您的 spring 安全文件

<logout success-handler-ref="logoutSuccessHandler"
        invalidate-session="true" delete-cookies="JSESSIONID" />
<beans:bean id="logoutSuccessHandler"
    class="com.neosyn.security.CustomLogoutSuccessHandler">
    <beans:property name="useReferer" value="true" />
</beans:bean>

定义类注销处理程序

public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {

@Override
public void onLogoutSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException {

    setAlwaysUseDefaultTargetUrl(true);
    setDefaultTargetUrl("/home");
    super.onLogoutSuccess(request, response, authentication);
}

}

于 2013-08-28T10:27:55.000 回答