java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null". I am not able to solve it

Viewed 29

I am not able to solve the problem --> java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" I have it implemented like this:

    @RequiredArgsConstructor
        @Configuration
        @EnableWebSecurity
        @EnableGlobalMethodSecurity(prePostEnabled = true)
        public class SecurityConfig extends WebSecurityConfigurerAdapter {
        
            private final UserDetailsService userDetailsService;
            private final PersistentTokenRepository persistentTokenRepository;
        
            // needed for use with Spring Data JPA SPeL
            @Bean
            public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
                return new SecurityEvaluationContextExtension();
            }
        
            @Override
            protected void configure(HttpSecurity http) throws Exception {
        
                http
                        .authorizeRequests(authorize -> {
                            authorize
                                    .antMatchers("/", "/webjars/**", "/login", "/resources/**").permitAll();
                        } )
                        .authorizeRequests()
                        .anyRequest().authenticated()
                        .and()
                        .formLogin(loginConfigurer -> {
                            loginConfigurer
                                    .loginProcessingUrl("/login")
                                    .loginPage("/").permitAll()
                                    .successForwardUrl("/")
                                    .defaultSuccessUrl("/")
                                    .failureUrl("/?error");
                        })
                        .logout(logoutConfigurer -> {
                            logoutConfigurer
                                    .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
                                    .logoutSuccessUrl("/?logout")
                                    .permitAll();
                        })
                        .httpBasic()
                        .and().rememberMe()
                        .tokenRepository(persistentTokenRepository)
                        .userDetailsService(userDetailsService);
                http.headers().frameOptions().sameOrigin();
            }
        
            @Bean
            PasswordEncoder passwordEncoder(){
                return SfgPasswordEncoderFactories.createDelegatingPasswordEncoder();
            }
        

Token creation

@Configuration
public class SecurityBeans {
    @Bean
    public PersistentTokenRepository persistentTokenRepository(DataSource dataSource){
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        return tokenRepository;
    }

}

****************************************************************************************************

In the DB insert a password in bcrypt for the user. The error seems to be in return new DelegatingPasswordEncoder(encodingId, encoders); but I think it's fine.

public class SfgPasswordEncoderFactories {

    public static PasswordEncoder createDelegatingPasswordEncoder() {
        String encodingId = "bcrypt";
        Map<String, PasswordEncoder> encoders = new HashMap<>();
        encoders.put(encodingId, new BCryptPasswordEncoder());
        encoders.put("bcrypt", new BCryptPasswordEncoder());
        encoders.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder());
        encoders.put("noop", org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance());
        encoders.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder());

        return new DelegatingPasswordEncoder(encodingId, encoders);
    }

    //don't instantiate class
    private SfgPasswordEncoderFactories() {
    }
}
0 Answers
Related