- Timestamp:
- 12/19/21 19:39:00 (3 years ago)
- Branches:
- master
- Children:
- bdd6491
- Parents:
- 1ad8e64
- Location:
- trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/PlannerRestController.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.Location; 4 4 import finki.diplomska.tripplanner.models.Planner; 5 import finki.diplomska.tripplanner.models.User; 5 6 import finki.diplomska.tripplanner.models.dto.PlannerDto; 6 7 import finki.diplomska.tripplanner.service.LocationService; … … 9 10 import org.springframework.http.MediaType; 10 11 import org.springframework.http.ResponseEntity; 12 import org.springframework.security.core.Authentication; 11 13 import org.springframework.web.bind.annotation.*; 12 14 import org.springframework.web.bind.annotation.RestController; 13 15 16 import javax.validation.Valid; 14 17 import java.util.ArrayList; 15 18 import java.util.List; … … 17 20 18 21 @RestController 19 @CrossOrigin(origins = "http://localhost:4200" )22 @CrossOrigin(origins = "http://localhost:4200", exposedHeaders = "token") 20 23 @RequestMapping(value = "/api") 21 24 public class PlannerRestController { … … 31 34 public List<Planner> getAllPlanners(){ 32 35 return this.plannerService.getAllPlaners(); 36 } 37 38 39 @GetMapping(value = "/planners/user") 40 public List<Planner> getPlannersByUser(Authentication authentication){ 41 User user = (User) authentication.getPrincipal(); 42 return this.plannerService.getPlannersByUser(user.getUsername()); 33 43 } 34 44 … … 57 67 @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE) 58 68 @ResponseStatus(HttpStatus.CREATED) 59 public ResponseEntity<Planner> newPlanner(@RequestBody PlannerDto plannerDto) { 60 return this.plannerService.newPlanner(plannerDto) 69 public ResponseEntity<?> newPlanner(@Valid @RequestBody PlannerDto plannerDto, Authentication authentication) { 70 71 User user = (User) authentication.getPrincipal(); 72 return this.plannerService.newPlanner(plannerDto, user.getUsername()) 61 73 .map(planner -> ResponseEntity.ok().body(planner)) 62 74 .orElseGet(() -> ResponseEntity.badRequest().build()); … … 65 77 66 78 @PutMapping(value ="edit/planner/{id}", consumes= MediaType.APPLICATION_JSON_VALUE) 67 public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @RequestBody PlannerDto plannerDto){ 68 return this.plannerService.editPlanner(id, plannerDto) 79 public ResponseEntity<Planner> editPlanner(@PathVariable Long id, @Valid @RequestBody PlannerDto plannerDto, Authentication authentication){ 80 User user = (User) authentication.getPrincipal(); 81 return this.plannerService.editPlanner(id, plannerDto, user.getUsername()) 69 82 .map(planner -> ResponseEntity.ok().body(planner)) 70 83 .orElseGet(()-> ResponseEntity.badRequest().build()); -
trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java
r1ad8e64 r84d0fbb 3 3 import finki.diplomska.tripplanner.models.User; 4 4 import finki.diplomska.tripplanner.models.dto.UserDto; 5 import finki.diplomska.tripplanner.payload.JWTLoginSucessReponse; 6 import finki.diplomska.tripplanner.payload.LoginRequest; 7 import finki.diplomska.tripplanner.security.JwtTokenProvider; 8 import finki.diplomska.tripplanner.security.SecurityConstants; 5 9 import finki.diplomska.tripplanner.service.UserService; 6 10 import finki.diplomska.tripplanner.service.impl.MapValidationErrorService; … … 9 13 import org.springframework.http.HttpStatus; 10 14 import org.springframework.http.ResponseEntity; 15 import org.springframework.security.authentication.AuthenticationManager; 16 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 17 import org.springframework.security.core.context.SecurityContextHolder; 11 18 import org.springframework.validation.BindingResult; 12 19 import org.springframework.web.bind.annotation.*; 13 20 import org.springframework.security.core.Authentication; 14 21 import javax.validation.Valid; 15 22 … … 28 35 private UserValidator userValidator; 29 36 30 @PostMapping("/register") 37 @Autowired 38 private JwtTokenProvider tokenProvider; 39 40 @Autowired 41 private AuthenticationManager authenticationManager; 42 43 @PostMapping(value = "/login") 44 public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest, BindingResult result){ 45 ResponseEntity<?> errorMap = mapValidationErrorService.MapValidationService(result); 46 if(errorMap != null) return errorMap; 47 48 Authentication authentication = authenticationManager.authenticate( 49 new UsernamePasswordAuthenticationToken( 50 loginRequest.getUsername(), 51 loginRequest.getPassword() 52 ) 53 ); 54 55 SecurityContextHolder.getContext().setAuthentication(authentication); 56 String jwt = SecurityConstants.TOKEN_PREFIX + tokenProvider.generateToken(authentication); 57 58 return ResponseEntity.ok(new JWTLoginSucessReponse(true, jwt)); 59 } 60 61 @PostMapping(value = "/register") 31 62 public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){ 32 63 // Validate passwords match … … 38 69 39 70 return new ResponseEntity<User>(newUser, HttpStatus.CREATED); 71 40 72 } 41 73 }
Note:
See TracChangeset
for help on using the changeset viewer.