|
@@ -11,32 +11,39 @@ import org.springframework.validation.BindingResult;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import pl.dmcs.jwoszczyk.domain.AppUser;
|
|
import pl.dmcs.jwoszczyk.domain.AppUser;
|
|
|
import pl.dmcs.jwoszczyk.domain.AppUserRole;
|
|
import pl.dmcs.jwoszczyk.domain.AppUserRole;
|
|
|
-import pl.dmcs.jwoszczyk.domain.LoginEntity;
|
|
|
|
|
|
|
+import pl.dmcs.jwoszczyk.domain.LoginDto;
|
|
|
import pl.dmcs.jwoszczyk.security.JwtTokenUtil;
|
|
import pl.dmcs.jwoszczyk.security.JwtTokenUtil;
|
|
|
import pl.dmcs.jwoszczyk.security.ValidationErrorResponse;
|
|
import pl.dmcs.jwoszczyk.security.ValidationErrorResponse;
|
|
|
|
|
+import pl.dmcs.jwoszczyk.service.IAppUserRoleService;
|
|
|
import pl.dmcs.jwoszczyk.service.IAppUserService;
|
|
import pl.dmcs.jwoszczyk.service.IAppUserService;
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.Optional;
|
|
|
|
|
-import java.util.Set;
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
|
@CrossOrigin(origins = "http://localhost:4200")
|
|
@CrossOrigin(origins = "http://localhost:4200")
|
|
|
public class SecurityController {
|
|
public class SecurityController {
|
|
|
|
|
+
|
|
|
|
|
+ //##############################################################_DEPENDENCY INJECTION
|
|
|
private IAppUserService userService;
|
|
private IAppUserService userService;
|
|
|
|
|
+ private IAppUserRoleService userRoleService;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private PasswordEncoder passwordEncoder;
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
- public SecurityController(IAppUserService userService) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public SecurityController(IAppUserService userService, IAppUserRoleService userRoleService) {
|
|
|
this.userService = userService;
|
|
this.userService = userService;
|
|
|
|
|
+ this.userRoleService = userRoleService;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+//########################################################################################################################_REGISTER
|
|
|
|
|
+
|
|
|
@RequestMapping(value = "/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
@RequestMapping(value = "/register", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
public ResponseEntity<?> Register(@Valid @RequestBody AppUser user, BindingResult bindingResult)
|
|
public ResponseEntity<?> Register(@Valid @RequestBody AppUser user, BindingResult bindingResult)
|
|
|
{
|
|
{
|
|
|
- // validation check
|
|
|
|
|
|
|
+ // --------------------------------------------------------------------------------- validation check
|
|
|
|
|
+
|
|
|
if (bindingResult.hasErrors()) {
|
|
if (bindingResult.hasErrors()) {
|
|
|
List<String> errors = bindingResult.getAllErrors()
|
|
List<String> errors = bindingResult.getAllErrors()
|
|
|
.stream()
|
|
.stream()
|
|
@@ -49,7 +56,9 @@ public class SecurityController {
|
|
|
|
|
|
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
|
}
|
|
}
|
|
|
- // duplication check
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // --------------------------------------------------------------------------------- duplication check
|
|
|
|
|
+
|
|
|
boolean usernameUnique = this.userService.isUsernameUnique(user.getLogin());
|
|
boolean usernameUnique = this.userService.isUsernameUnique(user.getLogin());
|
|
|
boolean emailUnique = this.userService.isEmailUnique(user.getEmail());
|
|
boolean emailUnique = this.userService.isEmailUnique(user.getEmail());
|
|
|
|
|
|
|
@@ -64,10 +73,21 @@ public class SecurityController {
|
|
|
|
|
|
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
return ResponseEntity.badRequest().body(errorResponse);
|
|
|
}
|
|
}
|
|
|
|
|
+ // --------------------------------------------------------------------------------- creating account
|
|
|
|
|
|
|
|
String hashedPassword = passwordEncoder.encode(user.getPassword());
|
|
String hashedPassword = passwordEncoder.encode(user.getPassword());
|
|
|
user.setPassword(hashedPassword);
|
|
user.setPassword(hashedPassword);
|
|
|
|
|
|
|
|
|
|
+ Set<AppUserRole> userRoles = new HashSet<>();
|
|
|
|
|
+ userRoles.add(userRoleService.getAppUserRoleByRoleName("ROLE_USER"));
|
|
|
|
|
+ user.setAppUserRole(userRoles);
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println("\n\ncreating account\n" +
|
|
|
|
|
+ "userRoleService.getAppUserRoleByRoleName(\"USER\") = " + userRoleService.getAppUserRoleByRoleName("ROLE_USER").getRoleName() +
|
|
|
|
|
+ "\nuserRoles (set) = " + userRoles.stream().findFirst() + "\n\n"
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
this.userService.registerUser(user);
|
|
this.userService.registerUser(user);
|
|
|
|
|
|
|
|
return ResponseEntity
|
|
return ResponseEntity
|
|
@@ -75,10 +95,12 @@ public class SecurityController {
|
|
|
.body("{\"message\": \"Request is valid\"}");
|
|
.body("{\"message\": \"Request is valid\"}");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- @RequestMapping(value = "/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
- public ResponseEntity<?> Login(@Valid @RequestBody LoginEntity loginEntity)
|
|
|
|
|
|
|
+//########################################################################################################################_LOGIN
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "riffmaster/login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
+ public ResponseEntity<?> Login(@Valid @RequestBody LoginDto loginDto)
|
|
|
{
|
|
{
|
|
|
- Optional<AppUser> user = Optional.ofNullable(this.userService.findByLogin(loginEntity.getUsername()));
|
|
|
|
|
|
|
+ Optional<AppUser> user = Optional.ofNullable(this.userService.findByLogin(loginDto.getUsername()));
|
|
|
|
|
|
|
|
if (user.isEmpty())
|
|
if (user.isEmpty())
|
|
|
{
|
|
{
|
|
@@ -92,25 +114,27 @@ public class SecurityController {
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if (passwordEncoder.matches(loginEntity.getPassword(), user.get().getPassword()))
|
|
|
|
|
|
|
+ if (passwordEncoder.matches(loginDto.getPassword(), user.get().getPassword()))
|
|
|
{
|
|
{
|
|
|
System.out.println("Login successful");
|
|
System.out.println("Login successful");
|
|
|
- String jwtToken = JwtTokenUtil.generateToken(
|
|
|
|
|
- user.get().getLogin(),
|
|
|
|
|
- user.get().getAppUserRoleStringArray()
|
|
|
|
|
- );
|
|
|
|
|
- System.out.println(jwtToken);
|
|
|
|
|
|
|
|
|
|
-// return ResponseEntity.ok()
|
|
|
|
|
-// .header("Authorization", "Bearer " + jwtToken)
|
|
|
|
|
-// .body("Request is valid");
|
|
|
|
|
|
|
+ // Get the role names as a HashSet of strings
|
|
|
|
|
+ Set<String> roleNames = user.get().getAppUserRole().stream()
|
|
|
|
|
+ .map(AppUserRole::getRoleName)
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+
|
|
|
|
|
+ String jwtToken = JwtTokenUtil.generateToken(user.get().getLogin(), roleNames);
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println(jwtToken);
|
|
|
|
|
|
|
|
return ResponseEntity
|
|
return ResponseEntity
|
|
|
.status(HttpStatus.OK)
|
|
.status(HttpStatus.OK)
|
|
|
.header("Authorization", "Bearer " + jwtToken)
|
|
.header("Authorization", "Bearer " + jwtToken)
|
|
|
.body("{\"message\": \"Request is valid\"," +
|
|
.body("{\"message\": \"Request is valid\"," +
|
|
|
"\"Authorization\": \"Bearer " + jwtToken + "\"}");
|
|
"\"Authorization\": \"Bearer " + jwtToken + "\"}");
|
|
|
- } else {
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
|
errorResponse.setMessage("Login Error");
|
|
errorResponse.setMessage("Login Error");
|
|
|
List<String> errors = new ArrayList<>();
|
|
List<String> errors = new ArrayList<>();
|