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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.