Home > Net >  How to encpyt the password using Springboot?
How to encpyt the password using Springboot?

Time:08-26

I have already done user register and login. But I want to encrypt the password when create a profile.

This is my current configuration

MongoDB Connection

spring.data.mongodb.uri= mongodb://127.0.0.1:27017/Student
server.port=8080

Model Class: @Document @AllArgsConstructor @NoArgsConstructor @Data public class User { @Id @Indexed private String id; @Indexed private String address; @Indexed private String name; @Indexed private String email; @Indexed private String password; @Indexed private String role; }

Repository Class:

public interface userReporsitory extends MongoRepository<User,String> {
    Optional<User> findByEmail(String email);
    List<User> findAllByRole(String role);
}

Service Class:

@AllArgsConstructor
@Service
public class userService {
    private userReporsitory userReporsitory;
    public User saveUser(User user){
        return userReporsitory.save(user);    
    }
    public User login(User user){
          User response = userReporsitory.findByEmail(user.getEmail()).orElseThrow(()->new RuntimeException("User Not Found"));
          
          if(!response.getPassword().equals(user.getPassword())){
            throw new RuntimeException("Bad Credincials");
          }

          return response;
    }

    public List<User> findAllUsers(){
      return userReporsitory.findAllByRole("user");
    }
}

Controller Class:

    @CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("api/v1/user")
public class userController {
   private userService userService;

    @PostMapping("/create")
    public ResponseEntity<User> save(@RequestBody User user){
        HttpStatus status = HttpStatus.EXPECTATION_FAILED;
        User response = userService.saveUser(user);

        if(response != null){
           status = HttpStatus.CREATED;
        }

        return new ResponseEntity<>(response, status);
        
    }
    @PostMapping("/login")
    public ResponseEntity<User> login(@RequestBody User user){
        return new ResponseEntity<>(userService.login(user),HttpStatus.ACCEPTED);
        
    }
    @GetMapping("/userList")
    public ResponseEntity<List<User>> userList(){
        return new ResponseEntity<>(userService.findAllUsers(),HttpStatus.ACCEPTED);
    }
        

}

CodePudding user response:

I think this is about encrypt. It is only a mistake. Code is fine no wrong

CodePudding user response:

Use

BCryptPasswordEncoder

Class while saving the Password in DataBase.it will convert the normal text to RandomValue.

Define the BCryptPasswordEncoder In config Class.

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

Repository Class :

   @Autowired
private PasswordEncoder passwordEncoder;

public User newUserAccount(UserDto accountDto) {
        User user = new User();
    user.setFirstName(accountDto.getFirstName());
    user.setLastName(accountDto.getLastName());
   
    user.setPassword(passwordEncoder.encode(accountDto.getPassword()));
    
    
    return repository.save(user);
}
  • Related