|
@@ -10,12 +10,13 @@ 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.LoginDto;
|
|
|
|
|
+import pl.sudra.domain.Role;
|
|
|
import pl.sudra.domain.User;
|
|
import pl.sudra.domain.User;
|
|
|
|
|
+import pl.sudra.service.EmailService;
|
|
|
|
|
+import pl.sudra.service.RoleService;
|
|
|
import pl.sudra.service.UserService;
|
|
import pl.sudra.service.UserService;
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Optional;
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
@@ -23,12 +24,16 @@ import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
@RestController
|
|
@RestController
|
|
|
@CrossOrigin(origins = "http://localhost:1410")
|
|
@CrossOrigin(origins = "http://localhost:1410")
|
|
|
public class SecurityController {
|
|
public class SecurityController {
|
|
|
- private UserService userService;
|
|
|
|
|
- @Autowired
|
|
|
|
|
|
|
+ private final UserService userService;
|
|
|
|
|
+ private final RoleService roleService;
|
|
|
|
|
+// private final EmailService emailService;
|
|
|
|
|
+
|
|
|
private PasswordEncoder passwordEncoder;
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
- public SecurityController(UserService userService) {
|
|
|
|
|
|
|
+ public SecurityController(UserService userService, RoleService roleService) {
|
|
|
this.userService = userService;
|
|
this.userService = userService;
|
|
|
|
|
+ this.roleService = roleService;
|
|
|
|
|
+// this.emailService = emailService;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(
|
|
@RequestMapping(
|
|
@@ -37,6 +42,7 @@ public class SecurityController {
|
|
|
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
|
|
// validation check
|
|
|
|
|
+ System.out.println("register - validation check");
|
|
|
if (bindingResult.hasErrors()) {
|
|
if (bindingResult.hasErrors()) {
|
|
|
List<String> errors = bindingResult.getAllErrors()
|
|
List<String> errors = bindingResult.getAllErrors()
|
|
|
.stream()
|
|
.stream()
|
|
@@ -50,6 +56,8 @@ public class SecurityController {
|
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
|
}
|
|
}
|
|
|
// duplication check
|
|
// duplication check
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("register - 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());
|
|
|
|
|
|
|
@@ -64,11 +72,27 @@ public class SecurityController {
|
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ System.out.println("register - encoding password");
|
|
|
String hashedPassword = passwordEncoder.encode(user.getPassword());
|
|
String hashedPassword = passwordEncoder.encode(user.getPassword());
|
|
|
user.setPassword(hashedPassword);
|
|
user.setPassword(hashedPassword);
|
|
|
|
|
|
|
|
|
|
+ System.out.println("register - setting client role");
|
|
|
|
|
+ Role client_role = roleService.getRole(0L);
|
|
|
|
|
+ System.out.println("CLIENT ROLE" + client_role.toString());
|
|
|
|
|
+ String roles = client_role.getRole();
|
|
|
|
|
+// roles.add(client_role);
|
|
|
|
|
+
|
|
|
|
|
+ user.setRole(roles);
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("hej" + user.toString());
|
|
|
|
|
+
|
|
|
this.userService.registerUser(user);
|
|
this.userService.registerUser(user);
|
|
|
|
|
|
|
|
|
|
+// this.emailService.sendMail(
|
|
|
|
|
+// user.getEmail(),
|
|
|
|
|
+// "Welcome in our app!",
|
|
|
|
|
+// "Account Created");
|
|
|
|
|
+
|
|
|
return ResponseEntity
|
|
return ResponseEntity
|
|
|
.status(HttpStatus.OK)
|
|
.status(HttpStatus.OK)
|
|
|
.body("{\"message\": \"Request is valid\"}");
|
|
.body("{\"message\": \"Request is valid\"}");
|
|
@@ -94,7 +118,7 @@ public class SecurityController {
|
|
|
String jwtToken = JwtTokenUtil.generateToken(
|
|
String jwtToken = JwtTokenUtil.generateToken(
|
|
|
user.get().getUsername(),
|
|
user.get().getUsername(),
|
|
|
user.get().getId(),
|
|
user.get().getId(),
|
|
|
- user.get().getRole()
|
|
|
|
|
|
|
+ user.get().getRolesForToken()
|
|
|
);
|
|
);
|
|
|
System.out.println(jwtToken);
|
|
System.out.println(jwtToken);
|
|
|
|
|
|