gpt4 book ai didi

java - spring security 没有 AuthenticationSuccessHandler 和 AuthenticationFalureHandler 类型的合格 bean

转载 作者:行者123 更新时间:2023-12-01 09:11:50 24 4
gpt4 key购买 nike

@Component("MyAuthFilter")
public class MyAuthFilter extends UsernamePasswordAuthenticationFilter {

private int errCode = 0;

@Autowired
@Qualifier("authenticationManager")
//@Override
public void setAuthenticationManager(AuthenticationManager authenticationManager, AuthenticationSuccessHandler successHandler, AuthenticationFailureHandler failureHandler) {
super.setAuthenticationManager(authenticationManager);
this.setAuthenticationSuccessHandler(successHandler);
this.setAuthenticationFailureHandler(failureHandler);
}

@Override
public AuthenticationFailureHandler getFailureHandler() {
SimpleUrlAuthenticationFailureHandler handler = new SimpleUrlAuthenticationFailureHandler();
handler.setDefaultFailureUrl("/login?error=" + errCode);
return handler;
}

@Override
public AuthenticationSuccessHandler getSuccessHandler() {
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
handler.setDefaultTargetUrl("/courses");
return handler;
}

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException {

System.out.println("running my own version of UsernmePasswordFilter ... ");

String login = (String) request.getParameter("login");
String password = (String) request.getParameter("password");
errCode = validate(login,password);
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(login, password);
// Allow subclasses to set the "details" property
setDetails(request, authRequest);

return this.getAuthenticationManager().authenticate(authRequest);
}

private int validate(String login,String password){

if (login.isEmpty() && password.isEmpty()){
return 4;
}
if (login.isEmpty() && !password.isEmpty()){
return 2;
}
if (!login.isEmpty() && password.isEmpty()){
return 3;
}

return 1;
}
}

这是 MyAuthFilter。

这是我的 spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.2.xsd">

<http auto-config="false" use-expressions="true">
<intercept-url pattern="/courses*" access="hasRole('ROLE_USER')" />
<custom-filter before="FORM_LOGIN_FILTER" ref="MyAuthFilter" />
<form-login
login-page="/login"
default-target-url="/courses"
authentication-failure-url="/login"
username-parameter="loginField"
password-parameter="passwordField" />
<csrf disabled="true" />
</http>

<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service>
<user name="ars" password="1234" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>

</beans:beans>

当我尝试启动我的应用程序时,出现了异常

没有 AuthenticationSuccessHandler 类型的合格 bean

对于 FailureHandler 也有同样的错误。我将不胜感激任何帮助。

最佳答案

您的 AuthenticationSuccessHandler 未声明为 bean。您应该将其创建为 bean 并通过属性在标记中的 spring-security.xml 中注册 身份验证-success-handler-ref="nameOfYouSuccessHandlerBean"

所以它看起来像:配置java文件中的一些位置:

    @Bean
public AuthenticationSuccessHandler mySuccessHandler() {
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
handler.setDefaultTargetUrl("/courses");
return handler;
}

以及 spring-security.xml

 <form-login
login-page="/login"
default-target-url="/courses"
authentication-failure-url="/login"
username-parameter="loginField"
authentication-success-handler-ref="mySuccessHandler"
password-parameter="passwordField" />

关于java - spring security 没有 AuthenticationSuccessHandler 和 AuthenticationFalureHandler 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40873682/

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