0

This is the error that appears when I try to create a new user "message": "Cannot invoke \"org.springframework.security.crypto.password.PasswordEncoder.encode(java.lang.CharSequence)\" because \"this.passwordEncoder\" is null", "path": "/auth/nuevo"

this is my code MainSecurity:

`

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class MainSecurity {

    @Autowired
    UserDetailsImpl userDetailsServiceImpl;

    @Autowired
    JwtEntryPoint jwtEntryPoint;

    @Bean
    public JwtTokenFilter jwtTokenFilter() {
        return new JwtTokenFilter();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsServiceImpl).passwordEncoder(passwordEncoder());
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
            throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .exceptionHandling().authenticationEntryPoint(jwtEntryPoint).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeHttpRequests()
                .requestMatchers("/**").permitAll()
                .anyRequest().authenticated();
            http
.addFilterBefore(jwtTokenFilter()
, UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }
}

`

I can't find how to solve the error.I am using spring-boot version 3.0.6, if you need more information, please let me know.

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37

1 Answers1

0

Try this

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
...

private final BCryptPasswordEncoder bCryptPasswordEncoder;
...

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsServiceImpl).passwordEncoder(bCryptPasswordEncoder);
    }
dannyRouge
  • 61
  • 5
  • continues to fail, postman throws the same error. – Programmer-G May 11 '23 at 22:32
  • try to see some of this answer: https://stackoverflow.com/questions/73400220/cannot-invoke-org-springframework-security-crypto-bcrypt-bcryptpasswordencoder https://stackoverflow.com/questions/73205915/i-have-a-problem-with-testing-adding-user-with-password-encoder – dannyRouge May 12 '23 at 06:38