ssm整合spring security之后登录拦截后跳转到之前登录前的页面

    科技2025-06-19  6

    首先要写一个Handler来处理拦截的时候要跳转的url

    public class LoginSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException { RequestCache requestCache = new HttpSessionRequestCache(); String url = null; SavedRequest savedRequest = requestCache.getRequest(httpServletRequest,httpServletResponse); if(savedRequest != null){ url = savedRequest.getRedirectUrl(); System.out.println("URL:" + url); } if(url == null){ httpServletResponse.sendRedirect("index.jsp"); } else { httpServletResponse.sendRedirect(url); } } }

    将上面的handler交给spring来管理(这里我是写在spring security的配置文件中)

    <bean id="loginSuccessHandler" class="com.zxl.handler.LoginSuccessHandler" />

    然后在spring security的配置文件中的登录跳转的配置中来设置

    <security:form-login login-page="/login.jsp" login-processing-url="/login" default-target-url="/index.jsp" authentication-failure-url="/failer.jsp" authentication-success-forward-url="/index.jsp" authentication-success-handler-ref="loginSuccessHandler" />
    Processed: 0.014, SQL: 8