javaspring-bootspring-mvcspring-security

bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found


Structure of the project is attached as image

this is WebSecurity class java package com.demo.springsecurity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
public class WebSecurity {

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

}

This is Service class

package com.demo.springsecurity.service;

import com.demo.springsecurity.entity.User;
import com.demo.springsecurity.entity.VerificationToken;
import com.demo.springsecurity.model.UserModel;
import com.demo.springsecurity.repository.UserRepository;
import com.demo.springsecurity.repository.VerificationTokenRepository;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class UserServiceImpl implements UserService{

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Autowired
    private VerificationTokenRepository verificationTokenRepository;



    @Override
    public User registerUser(UserModel userModel) {

        User user = User.builder()
                .firstName(userModel.getFirstName())
                .lastName(userModel.getLastName())
                .role("User")
                .build();
        user.setPassword(passwordEncoder.encode(userModel.getPassword()));
        user = userRepository.save(user);
        log.info("User saved in db");
        return user;
    }

    @Override
    public void saveVerificationTokenForUser(String token, User user) {

        VerificationToken verificationToken = new VerificationToken(user,token);

        verificationTokenRepository.save(verificationToken);

    }
}

Do let me know if u want any other class file code to see to help me to resolve this issue.

Error : Field passwordEncoder in com.demo.springsecurity.service.UserServiceImpl 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.

-- i have defined that method as @Bean but still not autowired, Please tell me what wrong i am doing in this code. Thanks you.

I have annotated that method as @Bean so that i can autowired it in service class but still not working.


Solution

  • Your WebSecurity class is missing the @Configuration annotation, so it's ignored by Spring's component scanning. This is why the PasswordEncoder bean isn't created, and thus it cannot be autowired either.

    @Configuration // Add this
    @EnableWebSecurity
    public class WebSecurity {
    
        @Bean
        public PasswordEncoder passwordEncoder(){
            return new BCryptPasswordEncoder(11);
        }
    
    }