我正在使用 Spring security 4.0. 我有一个基本的 Web 表单,通过 POST 提交到/SaveForm。为此,您必须是经过身份验证的用户。

<security:intercept-url pattern="/SaveForm" access="isAuthenticated()" />
<security:intercept-url pattern="/login" access="permitAll" />

我的 form-login 如下所示,其中 LoginAuthenticationSuccessHandler 实现了 AuthenticationSuccessHandler。

<security:form-login login-page="/login"
        authentication-failure-url="/login" username-parameter="email"
        password-parameter="password"
        authentication-success-handler ref="LoginAuthenticationSuccessHandler" />

不确定它是否相关,但我也使用 CustomAuthenticationProvider bean 实现 AuthenticationProvider(只返回 true 或 false)。

<security:authentication-manager>
    <security:authentication-provider
        ref="CustomAuthenticationProvider">
    </security:authentication-provider>
</security:authentication-manager>

进入登录页面后,我将保存标题“Referer”,以便我可以重定向到该页面之后。见下面的控制器

@RequestMapping("/login")
public String login(HttpServletRequest request, HttpSession session) {

    String referringURL = request.getHeader("Referer");
    session.setAttribute("referring_url", referringURL);

然后在我的 LoginAuthenticationSuccessHandler 类(实现 AuthenticationSuccessHandler)中,我重定向回原始 Web 表单

redirectURL = (String) session.getAttribute("referring_url");
response.sendRedirect(redirectURL);

这个“黑客”有效,但似乎在 Spring 中你可以自动重定向回原来的页面而没有这个。但我似乎无法配置它。我的问题是原始的表单值丢失了,我无法弄清楚在哪里保存它们。一旦我按预期提交/SaveForm Spring 安全拦截,但是我如何保存表单值?我希望能够在进行身份验证之前使用他们键入的值来填充表单。