|
@@ -3,18 +3,21 @@ package pl.sudra.securityController;
|
|
|
|
|
|
|
|
import jakarta.validation.Valid;
|
|
import jakarta.validation.Valid;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.validation.BindingResult;
|
|
import org.springframework.validation.BindingResult;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+import pl.sudra.domain.LoginDto;
|
|
|
import pl.sudra.domain.User;
|
|
import pl.sudra.domain.User;
|
|
|
import pl.sudra.service.UserService;
|
|
import pl.sudra.service.UserService;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Optional;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
-//import pl.sudra.configuration.SpringConfiguration.PasswordEncoder;
|
|
|
|
|
|
|
+
|
|
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
@@ -33,6 +36,7 @@ public class SecurityController {
|
|
|
method = RequestMethod.POST,
|
|
method = RequestMethod.POST,
|
|
|
produces = MediaType.APPLICATION_JSON_VALUE)
|
|
produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
public ResponseEntity<?> Register(@Valid @RequestBody User user, BindingResult bindingResult) {
|
|
public ResponseEntity<?> Register(@Valid @RequestBody User user, BindingResult bindingResult) {
|
|
|
|
|
+ // validation check
|
|
|
if (bindingResult.hasErrors()) {
|
|
if (bindingResult.hasErrors()) {
|
|
|
List<String> errors = bindingResult.getAllErrors()
|
|
List<String> errors = bindingResult.getAllErrors()
|
|
|
.stream()
|
|
.stream()
|
|
@@ -45,7 +49,7 @@ public class SecurityController {
|
|
|
|
|
|
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ // duplication check
|
|
|
boolean isUsernameNotUnique = this.userService.isUsernameNotUnique(user.getUsername());
|
|
boolean isUsernameNotUnique = this.userService.isUsernameNotUnique(user.getUsername());
|
|
|
boolean isEmailNotUnique = this.userService.isEmailNotUnique(user.getEmail());
|
|
boolean isEmailNotUnique = this.userService.isEmailNotUnique(user.getEmail());
|
|
|
|
|
|
|
@@ -53,8 +57,8 @@ public class SecurityController {
|
|
|
ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
|
errorResponse.setMessage("Duplication Error");
|
|
errorResponse.setMessage("Duplication Error");
|
|
|
List<String> errors = new ArrayList<>();
|
|
List<String> errors = new ArrayList<>();
|
|
|
- if(isUsernameNotUnique) errors.add("Username already exist in database.");
|
|
|
|
|
- if(isEmailNotUnique) errors.add("Email already exist in database.");
|
|
|
|
|
|
|
+ if (isUsernameNotUnique) errors.add("Username already exist in database.");
|
|
|
|
|
+ if (isEmailNotUnique) errors.add("Email already exist in database.");
|
|
|
errorResponse.setErrors(errors);
|
|
errorResponse.setErrors(errors);
|
|
|
|
|
|
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
@@ -66,7 +70,54 @@ public class SecurityController {
|
|
|
|
|
|
|
|
this.userService.registerUser(user);
|
|
this.userService.registerUser(user);
|
|
|
|
|
|
|
|
- return ResponseEntity.ok("Request is valid");
|
|
|
|
|
|
|
+ return ResponseEntity
|
|
|
|
|
+ .status(HttpStatus.OK)
|
|
|
|
|
+ .body("{\"message\": \"Request is valid\"}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @RequestMapping(
|
|
|
|
|
+ value = "/login",
|
|
|
|
|
+ method = RequestMethod.POST,
|
|
|
|
|
+ produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
+ public ResponseEntity<?> Login(@Valid @RequestBody LoginDto loginDto) {
|
|
|
|
|
+ Optional<User> user = this.userService.findByUsername(loginDto.getUsername());
|
|
|
|
|
+ if (user.isEmpty()) {
|
|
|
|
|
+ ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
|
|
|
+ errorResponse.setMessage("Login Error");
|
|
|
|
|
+ List<String> errors = new ArrayList<>();
|
|
|
|
|
+ errors.add("No such username in database.");
|
|
|
|
|
+ errorResponse.setErrors(errors);
|
|
|
|
|
+
|
|
|
|
|
+ return ResponseEntity.badRequest().body(errorResponse);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ if (passwordEncoder.matches(loginDto.getPassword(), user.get().getPassword())) {
|
|
|
|
|
+ System.out.println("Login successful");
|
|
|
|
|
+ String jwtToken = JwtTokenUtil.generateToken(
|
|
|
|
|
+ user.get().getUsername(),
|
|
|
|
|
+ user.get().getId(),
|
|
|
|
|
+ user.get().getRole()
|
|
|
|
|
+ );
|
|
|
|
|
+ System.out.println(jwtToken);
|
|
|
|
|
+
|
|
|
|
|
+// return ResponseEntity.ok()
|
|
|
|
|
+// .header("Authorization", "Bearer " + jwtToken)
|
|
|
|
|
+// .body("Request is valid");
|
|
|
|
|
+
|
|
|
|
|
+ return ResponseEntity
|
|
|
|
|
+ .status(HttpStatus.OK)
|
|
|
|
|
+ .header("Authorization", "Bearer " + jwtToken)
|
|
|
|
|
+ .body("{\"message\": \"Request is valid\"," +
|
|
|
|
|
+ "\"Authorization\": \"Bearer " + jwtToken + "\"}");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
|
|
|
+ errorResponse.setMessage("Login Error");
|
|
|
|
|
+ List<String> errors = new ArrayList<>();
|
|
|
|
|
+ errors.add("Password is not matching.");
|
|
|
|
|
+ errorResponse.setErrors(errors);
|
|
|
|
|
+
|
|
|
|
|
+ return ResponseEntity.badRequest().body(errorResponse);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|