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

标签 java authentication spring-boot custom-authentication

我已经对此进行了研究,我认为评论(在该线程中的实际答案下方: 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/

相关文章:

java - Spring Boot 上的 Jersey - ApplicationPath

python - 为什么在 Python 的 http 登录响应中没有 session ID?

java - 如何在 spring boot 应用程序中检查嵌入式 tomcat 的健康状况?

java - spring boot中如何读写文件

java - 使用 GraalPython 作为 Jython 替代品

java - GraphicsConfiguration.getDevice() 返回错误的监视器 ID

java - iText 页面颜色或黑/白

java - 销毁 JSF 中的特定用户 session

c# - 在 Azure 中运行时使用自定义范围时 DefaultAzureCredential().GetTokenAsync 失败

spring-boot - 在 REST 微服务上使用 Zuul Proxy、Oauth2 实现身份验证和授权