package pl.dmcs.springbootjsp_iwa.controllers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.bind.annotation.*; import pl.dmcs.springbootjsp_iwa.message.request.LoginForm; import pl.dmcs.springbootjsp_iwa.message.request.SignUpForm; import pl.dmcs.springbootjsp_iwa.message.response.JwtResponse; import pl.dmcs.springbootjsp_iwa.message.response.ResponseMessage; import pl.dmcs.springbootjsp_iwa.model.Role; import pl.dmcs.springbootjsp_iwa.model.RoleName; import pl.dmcs.springbootjsp_iwa.model.User; import pl.dmcs.springbootjsp_iwa.repository.RoleRepository; import pl.dmcs.springbootjsp_iwa.repository.UserRepository; import pl.dmcs.springbootjsp_iwa.security.jwt.JwtProvider; import javax.validation.Valid; import java.util.HashSet; import java.util.Set; @RestController @CrossOrigin(origins = "http://localhost:4200", maxAge = 3600) @RequestMapping("/auth") public class AuthRESTController { @Autowired AuthenticationManager authenticationManager; @Autowired UserRepository userRepository; @Autowired RoleRepository roleRepository; @Autowired PasswordEncoder passwordEncoder; @Autowired JwtProvider jwtProvider; @PostMapping("/signin") public ResponseEntity authenticateUser(@Valid @RequestBody LoginForm loginRequest) { Authentication authentication = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword())); SecurityContextHolder.getContext().setAuthentication(authentication); String jwt = jwtProvider.generateJwtToken(authentication); UserDetails userDetails = (UserDetails) authentication.getPrincipal(); return ResponseEntity.ok(new JwtResponse(jwt,userDetails.getUsername(), userDetails.getAuthorities())); } @PostMapping("/signup") public ResponseEntity registerUser(@Valid @RequestBody SignUpForm signUpRequest) { if (userRepository.existsByUsername(signUpRequest.getUsername())) { return new ResponseEntity<>(new ResponseMessage("Fail -> Username is already taken."), HttpStatus.BAD_REQUEST); } // Create user account User user = new User(signUpRequest.getUsername(), passwordEncoder.encode(signUpRequest.getPassword())); Set strRoles = signUpRequest.getRole(); Set roles = new HashSet<>(); strRoles.forEach(role -> { switch (role) { case "admin": Role adminRole = roleRepository.findByName(RoleName.ROLE_ADMIN) .orElseThrow(() -> new RuntimeException("Fail -> Cause: Admin Role not found.")); roles.add(adminRole); break; default: Role userRole = roleRepository.findByName(RoleName.ROLE_USER) .orElseThrow(() -> new RuntimeException("Fail -> Cause: User Role not found.")); roles.add(userRole); } }); user.setRoles(roles); userRepository.save(user); return new ResponseEntity<>(new ResponseMessage("User registered successfully."), HttpStatus.OK); } }