spring security忽略authentication-failure-url

时间:2016-03-29 14:16:46

标签: java spring spring-mvc spring-security

我有以下spring安全配置:

<security:http auto-config="true" use-expressions="false" entry-point-ref="httpStatusEntryPoint">
        <security:custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrentSessionFilter"/>

        <security:form-login
                authentication-success-handler-ref="authenticationSuccessHandler"
                authentication-failure-url="/api/loginFailed"
        />

        <security:intercept-url pattern="/api/**"/>
        <security:anonymous enabled="false"/>
        <security:logout logout-url="/logout" delete-cookies="JSESSIONID,sessionId"
                         success-handler-ref="logoutSuccessHandler"
        />
        <security:csrf disabled="true"/>

        <security:session-management session-authentication-strategy-ref="sessionAuthenticationStrategy"/>

我输入了错误的密码,执行后没有输入

@RequestMapping(value = "/loginFailed", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public String loginError(HttpServletRequest httpServletRequest) {
    return getErrorMessage(httpServletRequest, SPRING_SECURITY_LAST_EXCEPTION_KEY);
}

我错了什么?

1 个答案:

答案 0 :(得分:1)

以下是问题和解决方案:

根据您的配置:

<security:intercept-url pattern="/api/**"/>

所有路径都位于api路径下方。根据您当前的配置,这还包括authenticationSuccessHandler配置:。

<security:form-login
                authentication-success-handler-ref="authenticationSuccessHandler"
                authentication-failure-url="/api/loginFailed"
        />
事实上,Spring Security在失败的登录尝试时重定向回/api/loginFailed,但由于这也是受保护的资源,因此另一个重定向将发生在身份验证入口点。您可以使用网络活动选项卡上的浏览器开发人员工具对此进行验证。

您需要做的是确保authentication-failure-url位于不安全的端点上。例如,/loginFailed

所以这个配置会起作用:

<security:intercept-url pattern="/api/**"/>

<security:form-login
                authentication-success-handler-ref="authenticationSuccessHandler"
                authentication-failure-url="/loginFailed"
        />