'Simple Password Encryption using Spring Boot
- Below is my Bean class
package com.naveen.entity;
import org.springframework.context.annotation.Bean;
public class PasswordEncoder {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public String encode(String passWord) {
// TODO Auto-generated method stub
return null;
}
}
- Then, I've created BCryptPasswordEncoder. java class as below,
package com.naveen.entity;
import org.springframework.stereotype.Component;
@Component
public class BCryptPasswordEncoder extends PasswordEncoder {
}
- Then, I've autowired my Controller class & I've added encryption function in my saveUser() as below,
public class UserController {
@Autowired
PasswordEncoder passwordEncoder;
@PostMapping("/saveUser")
public int saveUser(@RequestBody User user) throws Exception {
String encryptedPassword =passwordEncoder.encode(user.getPassWord());
user.setPassWord(encryptedPassword);
userService.saveUser(user);
System.out.println("Inserted data with id: "+ user.getId());
return 1;
}
4. Service class as follows,
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void saveUser(User user) {
userMapper.saveUser(user);
} }
When I run the code, I got the error below, Field passwordEncoder in com.naveen.controller.UserController required a bean of type 'com.naveen.entity.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 'com.naveen.entity.PasswordEncoder' in your configuration.
Please help me out to resolve where I went wrong.
Solution 1:[1]
You would need to add @Component annotation to BCryptPasswordEncoder class
import org.springframework.stereotype.Component;
@Component
public class BCryptPasswordEncoder extends PasswordEncoder{
}
It would work then.
Solution 2:[2]
Adding to PumpkinX's answer put your statements in this order:
String encryptedPassword =passwordEncoder.encode(user.getPassWord());
user.setPassWord(encryptedPassword);
userService.saveUser(user);
System.out.println("Inserted data with id: "+ user.getId());
Notice, userService.saveUser(user); after you read and encode the data? Since you had this statement of top it was causing it to have null value.
Solution 3:[3]
I know I am late on this. But you are missing the implementation of the encode function.
Just replace the encode function with the below.
public String encode(String passWord) {
BCryptPasswordEncoder bCryptPasswordEncoder =
new BCryptPasswordEncoder(10, new SecureRandom());
return bCryptPasswordEncoder.encode(passWord);
}
This will encrypt the password instead of returning the null value.
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 | PumpkinX |
| Solution 2 | |
| Solution 3 | fshah |
