Last change
on this file since 84d0fbb was 84d0fbb, checked in by Ema <ema_spirova@…>, 3 years ago |
spring security 2.0
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[1ad8e64] | 1 | package finki.diplomska.tripplanner.service.impl;
|
---|
| 2 |
|
---|
| 3 | import finki.diplomska.tripplanner.models.User;
|
---|
| 4 | import finki.diplomska.tripplanner.models.dto.UserDto;
|
---|
| 5 | import finki.diplomska.tripplanner.models.exceptions.UsernameAlreadyExistsException;
|
---|
| 6 | import finki.diplomska.tripplanner.repository.jpa.JpaUserRepository;
|
---|
| 7 | import finki.diplomska.tripplanner.service.UserService;
|
---|
| 8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
| 10 | import org.springframework.stereotype.Service;
|
---|
| 11 |
|
---|
[84d0fbb] | 12 | import java.util.Optional;
|
---|
| 13 |
|
---|
[1ad8e64] | 14 |
|
---|
| 15 | @Service
|
---|
| 16 | public class UserServiceImpl implements UserService {
|
---|
| 17 |
|
---|
| 18 | @Autowired
|
---|
| 19 | private JpaUserRepository userRepository;
|
---|
| 20 |
|
---|
| 21 | @Autowired
|
---|
| 22 | private BCryptPasswordEncoder bCryptPasswordEncoder;
|
---|
| 23 |
|
---|
| 24 | public User saveUser (User newUser){
|
---|
| 25 | try{
|
---|
| 26 | newUser.setPassword(bCryptPasswordEncoder.encode(newUser.getPassword()));
|
---|
| 27 | //Username has to be unique (exception)
|
---|
| 28 | newUser.setUsername(newUser.getUsername());
|
---|
| 29 | // Make sure that password and confirmPassword match
|
---|
| 30 | // We don't persist or show the confirmPassword
|
---|
| 31 | newUser.setConfirmPassword("");
|
---|
[84d0fbb] | 32 | return this.userRepository.save(newUser);
|
---|
[1ad8e64] | 33 | }catch(Exception e){
|
---|
| 34 | throw new UsernameAlreadyExistsException("Username '"+newUser.getUsername()+ "' already exists");
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.