gpt4 book ai didi

java - 带有java配置的spring security AuthenticationFailureHandler

转载 作者:行者123 更新时间:2023-11-30 06:58:40 35 4
gpt4 key购买 nike

我创建了一个像这样的自定义身份验证失败处理程序:

public class TrackerAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler{

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

if(exception.getClass().isAssignableFrom(DisabledException.class)){
setDefaultFailureUrl("/accountRecovery");
}
}
}

然后我创建了一个这样的 bean:

@Bean
public AuthenticationFailureHandler trackerAuthFailureHandler(){
SimpleUrlAuthenticationFailureHandler handler=new SimpleUrlAuthenticationFailureHandler();
return handler;
}

和 Spring 配置是这样的:

    @Autowired
private TrackerAuthFailureHandler trackerAuthFailureHandler;

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.failureHandler(trackerAuthFailureHandler);
}

但是发生了bean not found异常。有什么想法吗?

最佳答案

当您使用注释注入(inject)方式时,您必须在类 T​​rackerAuthFailureHandler 的顶部使用 @Component。像这样:.

@Component
public class TrackerAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler{

关于java - 带有java配置的spring security AuthenticationFailureHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32446570/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com