0

我刚刚从 Spring Security 3.2.0.RC1 升级到 3.2.0.RC2。在 RC1 下一切正常。在 RC2 下,我的自定义登录页面不再有效。单击登录按钮后,登录页面将重新显示。如果提交了无效的凭据(或没有凭据),它还会重新显示而没有任何错误消息。如果凭据不正确,它将正确显示错误消息。

有趣的是,如果我改变:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    // @formatter:off

    httpSecurity
        .authorizeRequests()
            .antMatchers("/restricted/**").hasRole("admin"))
            // all requests must be authenticated
            .anyRequest().authenticated()
    .and()
        .formLogin()
            .loginPage("/myLoginUrl.request")
            .failureUrl("/myLoginUrl.request?error")
            .permitAll()
    .and()
        .logout()
            .permitAll()
            .logoutSuccessUrl("/myLoginUrl.request")
    ;
    // @formatter:on
}

至:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
    // @formatter:off

    httpSecurity
        .authorizeRequests()
            .antMatchers("/restricted/**").hasRole("admin"))
            // all requests must be authenticated
            .anyRequest().authenticated()
    .and()
        .formLogin()
            .permitAll()
    .and()
        .logout()
            .permitAll()
            .logoutSuccessUrl("/myLoginUrl.request")
    ;
    // @formatter:on
}

显示默认的 Spring Security 登录页面并正常工作。我查看了默认页面的来源并将其与我的自定义页面进行了比较,它似乎对具有相同名称的字段调用了相同的操作。

如果我单步调试调试器,我会发现在 AntPathMatcher.java 中,公共布尔匹配(HttpServletRequest 请求):

String url = getRequestPath(request)

使用我的自定义登录页面时返回的 url 是“/error”。getRequestPath() 只返回附加到 request.getPathInfo() 的 request.getServletPath()。我不确定为什么升级到 RC2 会导致它返回“/error”。

4

1 回答 1

1

我改变了三件事,使这项工作有效。

1) 在表单中添加了一个 CSRF 隐藏字段:

<input type="hidden" name="${_csrf.parameterName}"
    value="${_csrf.token}" /> 

2)表单方法的大写POST:

<form action="login" method="POST">

3)在配置中显式添加loginProcessingUrl:

.formLogin()
        .loginPage("/myLoginUrl.request")
        .loginProcessingUrl("/login")
        .failureUrl("/myLoginUrl.request?error")
        .permitAll()
于 2013-11-05T18:18:49.390 回答