首页 > 解决方案 > 管理错误凭据的 SpringSecurity failureHandler 或 failureUrl 被忽略

问题描述

我正在将 SpringBoot 与 SpringSecurity 一起使用。我集成了 LDAP 身份验证过程。我没有使用 REST 方法,我只是使用 Thymeleaf 库显示页面。一切正常。

现在,我尝试在凭据错误的情况下显示相同的登录页面和身份验证错误消息。

我以这种方式配置了安全配置:

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.cors();
    http
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .csrf().disable()
            .formLogin()
                .loginPage("/")
                //.failureUrl("/authError")
                //.failureHandler(getAuthenticationFailureHandler())
                .permitAll()
            .and()
            .httpBasic().disable()
            .authorizeRequests()
            .antMatchers("/",
                    "/error",
                    "/favicon.ico",
                    "/**/*.png",
                    "/**/*.gif",
                    "/**/*.svg",
                    "/**/*.jpg",
                    "/**/*.html",
                    "/**/*.css",
                    "/webjar/**",
                    "/**/*.js").permitAll()
            .antMatchers("/login**").permitAll()
            .anyRequest().authenticated();

    http.addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}

@Bean
public AuthenticationFailureHandler getAuthenticationFailureHandler() {
    return new LdapAuthenticationFailureHandler();
}

public class LdapAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request,
                                        HttpServletResponse response, AuthenticationException exception)
            throws IOException, ServletException {


        //mylogic
    }

我想使用自定义处理程序来管理这个场景,但是如果我尝试在AuthenticationFailure 方法上放置一个断点,它就永远不会被调用。

如果我尝试使用自定义控制器配置 .failureUrl("/login-error") 也会出现同样的问题。

我该如何解决这个问题?

标签: javaspring-bootspring-securityspring-ldap

解决方案


推荐阅读