'Security configuration with spring boot

I'm a beginner, I want to code the password when adding users but there is an error (@Bean).

Error:

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
    

Description:

Field passwordEncoder in com.example.demo.service.UsersService required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' in your configuration.

My code:

@Configuration
@EnableConfigurationProperties
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    UsersService userService;
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // TODO Auto-generated method stub
        http.csrf().disable() //TODO implement csrf
        .cors().and().authorizeRequests().antMatchers(HttpMethod.OPTIONS,"/").permitAll()
        .antMatchers("/login").permitAll()
        .antMatchers("/add-users").permitAll()
        .and().authorizeRequests().anyRequest().authenticated();
    }
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // TODO Auto-generated method stub
        auth.userDetailsService(userService)
        .passwordEncoder(passwordEncoder());
    }
    
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        // TODO Auto-generated method stub
        return super.authenticationManagerBean();
    }
    
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
public class UsersService implements UserDetailsService {

    @Autowired
    UsersRepository repo;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
        
    public Users save(Users u) {
        String encodpass=passwordEncoder.encode(u.getPassword());
        String confpass=passwordEncoder.encode(u.getConfirmepass());
        u.setConfirmpass(confpass);
        u.setPassword(encodpass);
        u.setLock(false);
        u.setEnable(true);
        return repo.save(u);
    }
}

    @RestController  
    public class UsersController {
        @Autowired 
        private UsersService service;
        @PostMapping("/add-users")
        public Users add(@RequestBody Users u) {
            return service.save(u);}



Solution 1:[1]

The problem is that in your SecurityConfiguration you are injecting UsersService, and the UserService bean depends on a bean of type PasswordEncoder that has not been created yet.

I'll simplify your configuration by using a more ellegant way of configuring Spring Security:

@Configuration
//your other annotations
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain app(HttpSecurity http) throws Exception {
        http.csrf().disable().cors().and().authorizeRequests().antMatchers(HttpMethod.OPTIONS,"/").permitAll()
        .antMatchers("/login").permitAll()
        .antMatchers("/add-users").permitAll()
        .and().authorizeRequests().anyRequest().authenticated();
    }

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

}

And that's it, you don't really need the AuthenticationManager bean unless you are actually using it. In case that you need it, you can create it like this:

@Bean
public AuthenticationManager authenticationManager(UsersService usersService) {
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setUserDetailsService(usersService);
    ProviderManager manager = new ProviderManager(daoAuthenticationProvider);
    return manager;
}

I apologize for any compilation error beforehand because I wrote this from a phone.

Solution 2:[2]

add @Service annotation to UserService class. also add @EnableWebSecurity to config class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcus Hert da Coregio
Solution 2 Ayush Dubey