Changeset 84d0fbb for trip-planner/src/main/java/finki/diplomska/tripplanner/web/rest/UserController.java
- Timestamp:
- 12/19/21 19:39:00 (3 years ago)
- Branches:
- master
- Children:
- bdd6491
- Parents:
- 1ad8e64
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.