1

i have a problem with PasswordEncoder,

my code:

@Service
public class UserService {

    private static final String DEFAULT_ROLE = "ROLE_USER";
    private UserRepository userRepository;
    private UserRoleRepository roleRepository;
    public PasswordEncoder passwordEncoder;


    @Autowired
    public UserService(PasswordEncoder passwordEncoder){
        this.passwordEncoder = passwordEncoder;
    }

    @Autowired
    public void setUserRepository(UserRepository userRepository){
        this.userRepository = userRepository;
    }

    @Autowired
    public void setUserRoleRepository(UserRoleRepository roleRepository){
        this.roleRepository = roleRepository;
    }

    public void addWithDefaultRole(User user){
        UserRole defaultRole = roleRepository.findByRole(DEFAULT_ROLE);
        user.getRoles().add(defaultRole);
        String passwordHash = passwordEncoder.encode(user.getPassword());
        user.setPassword(passwordHash);
        userRepository.save(user);
    }

}

error:


APPLICATION FAILED TO START


Description:

Parameter 0 of constructor in org.spring.service.UserService required a bean of type 'org.springframework.security.crypto.password.PasswordEncoder' that could not be found.

Action:

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

Process finished with exit code 1

I don't know how to fix it.

2 Answers 2

2

Try this way ( One of two bean, not both):

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfigAuthentication extends WebSecurityConfigurerAdapter {

        //   For BCrypt Encoded password

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


         OR


        //   For no Encoder, plain text password

        @Bean
        public static NoOpPasswordEncoder passwordEncoder() {
           return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
        }



    }
Sign up to request clarification or add additional context in comments.

Comments

0

For dependency injection to work, you need to acually declare a bean of the appropriate type, otherwise there is nothing to be injected. Exactly that is telling you the exception.

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder(); // or any other password encoder
}

3 Comments

I add your code to UserService, but i have another problem
Description: The dependencies of some of the beans in the application context form a cycle: ┌─────┐ | userService defined in file [C:\Users\...\UserService.class] └─────┘ Process finished with exit code 1
Put the bean to a separate @Component. Right now, you want to inject a PasswordEncoder in your UserService, but that is excatly the component who declares it.

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.