Ignore:
Timestamp:
12/19/21 19:39:00 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
bdd6491
Parents:
1ad8e64
Message:

spring security 2.0

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  
    33import finki.diplomska.tripplanner.models.Location;
    44import finki.diplomska.tripplanner.models.Planner;
     5import finki.diplomska.tripplanner.models.User;
    56import finki.diplomska.tripplanner.models.dto.PlannerDto;
    67import finki.diplomska.tripplanner.service.LocationService;
     
    910import org.springframework.http.MediaType;
    1011import org.springframework.http.ResponseEntity;
     12import org.springframework.security.core.Authentication;
    1113import org.springframework.web.bind.annotation.*;
    1214import org.springframework.web.bind.annotation.RestController;
    1315
     16import javax.validation.Valid;
    1417import java.util.ArrayList;
    1518import java.util.List;
     
    1720
    1821@RestController
    19 @CrossOrigin(origins = "http://localhost:4200")
     22@CrossOrigin(origins = "http://localhost:4200",  exposedHeaders = "token")
    2023@RequestMapping(value = "/api")
    2124public class PlannerRestController {
     
    3134    public List<Planner> getAllPlanners(){
    3235        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());
    3343    }
    3444
     
    5767    @PostMapping(value = "/planner/new", consumes= MediaType.APPLICATION_JSON_VALUE)
    5868    @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())
    6173                .map(planner -> ResponseEntity.ok().body(planner))
    6274                .orElseGet(() -> ResponseEntity.badRequest().build());
     
    6577
    6678    @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())
    6982                .map(planner -> ResponseEntity.ok().body(planner))
    7083                .orElseGet(()-> ResponseEntity.badRequest().build());
  • trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java

    r1ad8e64 r84d0fbb  
    33import finki.diplomska.tripplanner.models.User;
    44import finki.diplomska.tripplanner.models.dto.UserDto;
     5import finki.diplomska.tripplanner.payload.JWTLoginSucessReponse;
     6import finki.diplomska.tripplanner.payload.LoginRequest;
     7import finki.diplomska.tripplanner.security.JwtTokenProvider;
     8import finki.diplomska.tripplanner.security.SecurityConstants;
    59import finki.diplomska.tripplanner.service.UserService;
    610import finki.diplomska.tripplanner.service.impl.MapValidationErrorService;
     
    913import org.springframework.http.HttpStatus;
    1014import org.springframework.http.ResponseEntity;
     15import org.springframework.security.authentication.AuthenticationManager;
     16import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
     17import org.springframework.security.core.context.SecurityContextHolder;
    1118import org.springframework.validation.BindingResult;
    1219import org.springframework.web.bind.annotation.*;
    13 
     20import org.springframework.security.core.Authentication;
    1421import javax.validation.Valid;
    1522
     
    2835    private UserValidator userValidator;
    2936
    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")
    3162    public ResponseEntity<?> registerUser(@Valid @RequestBody User user, BindingResult result){
    3263        // Validate passwords match
     
    3869
    3970        return  new ResponseEntity<User>(newUser, HttpStatus.CREATED);
     71
    4072    }
    4173}
Note: See TracChangeset for help on using the changeset viewer.