Spring 上下文中的 Vaadin CustomAuthenticationFailureHandler 不起作用

问题描述 投票:0回答:1

我有一个 Spring Boot 3.2.5 和 Vaadin 24.3.10 项目。我想在 Vaadin 建议的登录过程中处理登录错误。这意味着我定义了一个扩展

SimpleUrlAuthenticationFailureHandler
类的类,并在 SecurityConfig 中设置该类。

@EnableWebSecurity
@Configuration
public class SecurityConfig extends VaadinWebSecurity
{

    private final AppUserRepository userRepository;
    private final CustomAuthenticationFailureHandler failureHandler;

    public SecurityConfig(AppUserRepository appUserRepository, CustomAuthenticationFailureHandler failureHandler) {
        this.userRepository = appUserRepository;
        this.failureHandler = failureHandler;
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeHttpRequests(auth -> {
                    auth.requestMatchers(
                            AntPathRequestMatcher.antMatcher(HttpMethod.GET, "/images/*.png")).permitAll();
                })
                .formLogin(form -> form
                    .loginPage("/login")
                    .failureHandler(failureHandler)  
                    .failureUrl("/login?error")
                    .permitAll())
                .logout(logout -> logout.permitAll())
                .exceptionHandling(e -> e.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login")))
                .headers(httpSecurityHeadersConfigurer -> httpSecurityHeadersConfigurer.frameOptions(HeadersConfigurer.FrameOptionsConfig::disable))
        ;

        super.configure(httpSecurity);
        setLoginView(httpSecurity, LoginView.class);
    }

CustomAuthenticationFailureHandler
班:

@Service
@Slf4j
public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler {

    @Autowired
    private LogService logService;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws
        IOException, ServletException
    {
        String username = request.getParameter("username");
        log.error("Wrong login: {}", username);
        logService.auditLog("UNKNOWN", LogTypes.SEC_ERR, "Wrong login: " + username);
        super.onAuthenticationFailure(request, response, exception);
    }
}

我需要 Spring 上下文,因为

LogService
中的
CustomAuthenticationFailureHandler
使用 JPA。但这个配置不起作用。如果我不使用
LogService
并通过 new 创建
CustomAuthenticationFailureHandler
,那么它可以正常工作。

...
         .failureHandler(new CustomAuthenticationFailureHandler())
...

我尝试寻找解决方案,因为我需要持久的审核日志,但我不知道。我需要一个解决方案来帮助我在此设置中使用 Spring 上下文。

java spring-boot authentication spring-security vaadin
1个回答
0
投票

所以解决方案就是参数的顺序。 这是原版:

                .formLogin(form -> form
                .loginPage("/login")
                .failureHandler(failureHandler)  
                .failureUrl("/login?error")
                .permitAll())

工作版本:

                .formLogin(form -> form
                .failureUrl("/login?error")
                .loginPage("/login")
                .failureHandler(failureHandler)  
                .permitAll())

如您所见,如果

失败处理程序

不是最后一个,那么它不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.