RameshMF / registration-login-spring-boot-security-thymeleaf-hibernate

Registration and Login with Spring Boot, Spring Security, Thymeleaf, Hibernate and MySQL
https://youtu.be/Hk70e7KR290
187 stars 148 forks source link

Error when i try to pass this as the parameter for constructor for the user Arrays.asList(new Role("ROLE_USER"))); #3

Open HoodLegend opened 3 years ago

HoodLegend commented 3 years ago

@Override public User save(UserRegistrationDto registrationDto) { User user = new User(registrationDto.getFirstName(), registrationDto.getPassword(), Arrays.asList(new Role("ROLE_USER"))); return userRepository.save(user); }

Gokulviper commented 2 years ago

@Override public User save(UserRegistrationDto registrationDto) { User user = new User(registrationDto.getFirstName(), registrationDto.getLastName(), registrationDto.getEmail(), passwordEncoder.encode(registrationDto.getPassword()), Arrays.asList(new Role("ROLE_USER")));

    return userRepository.save(user);
}

//try like this

atakoutene commented 1 year ago

This error is because internally, spring will automatically add the prefix ROLE_ to the USER role defined so since it's been specified already, it'll throw an error. So remove that prefix from the passed role.