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.

1 Answer 1

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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

continues to fail, postman throws the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.