- Ниже приведен мой метод @Bean
Код: Выделить всё
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;
}
}
- Затем я создал класс BCryptPasswordEncoder, как показано ниже:
Код: Выделить всё
package com.naveen.entity;
import org.springframework.stereotype.Component;
@Component
public class BCryptPasswordEncoder extends PasswordEncoder {
}
- Затем я автоматически подключил свой класс Controller и добавил функцию шифрования в свой saveUser(), как показано ниже
Код: Выделить всё
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;
}
}
- следующим образом:
Код: Выделить всё
Service
Код: Выделить всё
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void saveUser(User user) {
userMapper.saveUser(user);
}
}
Код: Выделить всё
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.
Подробнее здесь: https://stackoverflow.com/questions/678 ... pring-boot