gpt4 book ai didi

java - customAuthenticationProvider 身份验证被调用两次

转载 作者:行者123 更新时间:2023-12-02 02:11:33 25 4
gpt4 key购买 nike

我已经对此进行了研究,我认为评论(在该线程中的实际答案下方: ProviderManager.authenticate called twice for BadCredentialsException )将是我的解决方案......但我仍然收到双重提交/调用来进行身份验证。第二个每次都是空密码。第一次调用有凭据。

下面是 Java Config 类和 CustomAuthProvider 类..

@Configuration
@EnableWebSecurity
public class UserWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//@formatter:off
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/home**", "/login**","/create_user")
.permitAll().anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login")
.failureUrl("/login?error")
.and()
.logout()
.logoutSuccessUrl("/login?logout")
.permitAll()
.and()
.exceptionHandling().accessDeniedPage("/login?denied") //in this simple case usually due to a InvalidCsrfTokenException after session timeout
.and()
.csrf()
.ignoringAntMatchers("/rest/**")
.and()
.sessionManagement().enableSessionUrlRewriting(false)
.and()
.headers().frameOptions().deny();
}

...然后是 customAuthProvider ...

@Component
public class CustomAuthProvider implements AuthenticationProvider {

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (authentication.getName() == null) {
logger.warn("empty userName");
return null;
}

if (authentication.getCredentials() == null) {
logger.warn("empty password");
return null;
}

// code to check credentials etc ...

if (!(user != null && userHash.equals(storedHash))) {
System.out.println("fail");
return null;
}

return new UsernamePasswordAuthenticationToken(user,password);

}

最佳答案

修改了身份验证提供程序代码,以空的授权列表结束并返回authenticationToken ...现在它可以工作了。

// ...        
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user,password, new ArrayList<>());
return authenticationToken;

关于java - customAuthenticationProvider 身份验证被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49933343/

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