На беке пока типо всё. Но это только пока...
This commit is contained in:
parent
92c7edb8f2
commit
1820b567a6
@ -40,7 +40,7 @@ public class SecurityConfiguration {
|
||||
final String admin = "admin";
|
||||
if (userService.findByLogin(admin) == null) {
|
||||
log.info("Admin user successfully created");
|
||||
userService.addClient(admin, "admin@gmail.com", admin, admin, UserRole.ADMIN);
|
||||
userService.addClient(admin, "admin@gmail.com", admin, 0, admin, UserRole.ADMIN);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,19 +1,27 @@
|
||||
package premium_store.controller.controller;
|
||||
|
||||
import org.springframework.security.access.annotation.Secured;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import premium_store.configuration.OpenAPI30Configuration;
|
||||
import premium_store.configuration.WebConfiguration;
|
||||
import premium_store.controller.DTO.ClientDTO;
|
||||
import premium_store.controller.DTO.UserSignupDto;
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.model.UserRole;
|
||||
import premium_store.service.GameClientService;
|
||||
import premium_store.service.TankService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.ValidationException;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@CrossOrigin
|
||||
@RequestMapping(OpenAPI30Configuration.API_PREFIX + "/client")
|
||||
@RequestMapping("/client")
|
||||
public class GameClientController {
|
||||
public static final String URL_LOGIN = "/jwt/login";
|
||||
public static final String URL_SING_UP = "/sing_up";
|
||||
public static final String URL_WHO_AM_I = "/who_am_i";
|
||||
|
||||
private final GameClientService gameClientService;
|
||||
private final TankService tankService;
|
||||
|
||||
@ -22,21 +30,39 @@ public class GameClientController {
|
||||
this.tankService = tankService;
|
||||
}
|
||||
|
||||
@PostMapping(URL_LOGIN)
|
||||
public String login(@RequestBody @Valid ClientDTO userDto) {
|
||||
return gameClientService.loginAndGetToken(userDto);
|
||||
}
|
||||
|
||||
@PostMapping(URL_SING_UP)
|
||||
public String singUp(@RequestBody @Valid UserSignupDto userSignupDto) {
|
||||
try {
|
||||
final GameClient user = gameClientService.addClient(userSignupDto.getLogin(), userSignupDto.getEmail(), userSignupDto.getPassword(),
|
||||
Integer.parseInt(userSignupDto.getBalance()), userSignupDto.getPasswordConfirm(), UserRole.USER);
|
||||
|
||||
return "created " + user.getLogin();
|
||||
} catch (ValidationException e) {
|
||||
return e.getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
//аннотация PathVariable связывает значения id из URL и Long id
|
||||
@GetMapping("/{id}")
|
||||
@GetMapping(OpenAPI30Configuration.API_PREFIX + "/{id}")
|
||||
public ClientDTO getClient(@PathVariable Long id) {
|
||||
return new ClientDTO(gameClientService.findClient(id));
|
||||
}
|
||||
|
||||
//с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto
|
||||
@GetMapping("/")
|
||||
@GetMapping(OpenAPI30Configuration.API_PREFIX + "/")
|
||||
@Secured({UserRole.AsString.ADMIN})
|
||||
public List<ClientDTO> getClients() {
|
||||
return gameClientService.findAllClients().stream()
|
||||
.map(ClientDTO::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
@PostMapping(OpenAPI30Configuration.API_PREFIX + "/")
|
||||
public ClientDTO createClient(@RequestParam("login") String login,
|
||||
@RequestParam("password") String password,
|
||||
@RequestParam("email") String email,
|
||||
@ -44,7 +70,8 @@ public class GameClientController {
|
||||
return new ClientDTO(gameClientService.addClient(login, email, password, balance, password, UserRole.USER));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@PutMapping(OpenAPI30Configuration.API_PREFIX + "/{id}")
|
||||
@Secured({UserRole.AsString.USER})
|
||||
public ClientDTO updateClient(@PathVariable Long id,
|
||||
@RequestParam("login") String login,
|
||||
@RequestParam("password") String password,
|
||||
@ -54,7 +81,8 @@ public class GameClientController {
|
||||
return new ClientDTO(gameClientService.updateClient(id, login, password, email, balance, tankService.findTank(tankId)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@DeleteMapping(OpenAPI30Configuration.API_PREFIX + "/{id}")
|
||||
@Secured({UserRole.AsString.USER})
|
||||
public ClientDTO deleteClient(@PathVariable Long id) {
|
||||
return new ClientDTO(gameClientService.deleteClient(id));
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.configuration.jwt.JwtException;
|
||||
import premium_store.configuration.jwt.JwtProvider;
|
||||
import premium_store.controller.DTO.ClientDTO;
|
||||
import premium_store.controller.DTO.SupportClientDTO;
|
||||
import premium_store.controller.DTO.UserSignupDto;
|
||||
@ -33,15 +35,18 @@ public class GameClientService implements UserDetailsService {
|
||||
private final TankRepository tankRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtProvider jwtProvider;
|
||||
|
||||
public GameClientService(GameClientRepository gameClientRepository,
|
||||
TankRepository tankRepository,
|
||||
ValidatorUtil validatorUtil,
|
||||
PasswordEncoder passwordEncoder) {
|
||||
PasswordEncoder passwordEncoder,
|
||||
JwtProvider jwtProvider) {
|
||||
this.gameClientRepository = gameClientRepository;
|
||||
this.tankRepository = tankRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.jwtProvider = jwtProvider;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -62,11 +67,6 @@ public class GameClientService implements UserDetailsService {
|
||||
return gameClientRepository.save(gameClient);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public GameClient addClient(String login, String email, String password, Integer ballance, String passwordConfirm) {
|
||||
return addClient(login, email, password, ballance, passwordConfirm, UserRole.USER);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public GameClient findClient(Long id) {
|
||||
final Optional<GameClient> client = gameClientRepository.findById(id);
|
||||
@ -158,6 +158,31 @@ public class GameClientService implements UserDetailsService {
|
||||
gameClientRepository.deleteAll();
|
||||
}
|
||||
|
||||
public String loginAndGetToken(ClientDTO userDto) {
|
||||
final GameClient user = findByLogin(userDto.getLogin());
|
||||
|
||||
if (user == null) {
|
||||
throw new ClientNotFoundException(userDto.getLogin());
|
||||
}
|
||||
|
||||
if (!passwordEncoder.matches(userDto.getPassword(), user.getPassword())) {
|
||||
throw new ValidationException("Incorrect password");
|
||||
}
|
||||
|
||||
return jwtProvider.generateToken(user.getLogin());
|
||||
}
|
||||
|
||||
public UserDetails loadUserByToken(String token) throws UsernameNotFoundException {
|
||||
if (!jwtProvider.isTokenValid(token)) {
|
||||
throw new JwtException("Bad token");
|
||||
}
|
||||
|
||||
final String userLogin = jwtProvider.getLoginFromToken(token)
|
||||
.orElseThrow(() -> new JwtException("Token is not contain Login"));
|
||||
|
||||
return loadUserByUsername(userLogin);
|
||||
}
|
||||
|
||||
//метод загрузки пользователя по его логину
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
|
@ -4,4 +4,8 @@ public class ClientNotFoundException extends RuntimeException {
|
||||
public ClientNotFoundException(Long id) {
|
||||
super(String.format("Client with id [%s] is not found", id));
|
||||
}
|
||||
|
||||
public ClientNotFoundException(String login) {
|
||||
super(String.format("User not found '%s'", login));
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package premium_store.service.exception;
|
||||
|
||||
public class UserExistsException extends RuntimeException {
|
||||
public UserExistsException(String login) {
|
||||
super(String.format("User '%s' already exists", login));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user