Changes in / [7d8eeab:dad25fc]
- Location:
- ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo
- Files:
-
- 1 added
- 1 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/config/filter/JWTAuthenticationFilter.java
r7d8eeab rdad25fc 6 6 import jakarta.servlet.http.HttpServletResponse; 7 7 import lombok.NonNull; 8 import mk.ukim.finki.it.reservengo.constants.JWTConstants;9 8 import mk.ukim.finki.it.reservengo.service.intf.JWTService; 10 9 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; … … 33 32 @NonNull HttpServletResponse response, 34 33 @NonNull FilterChain filterChain) throws ServletException, IOException { 35 final String authHeader = request.getHeader( JWTConstants.HEADER);34 final String authHeader = request.getHeader("Authorization"); 36 35 final String jwt; 37 36 final String userEmail; 38 37 39 if (authHeader == null || !authHeader.startsWith( JWTConstants.TOKEN_PREFIX)) {38 if (authHeader == null || !authHeader.startsWith("Bearer ")) { 40 39 filterChain.doFilter(request, response); 41 40 return; 42 41 } 43 42 44 jwt = authHeader.substring( JWTConstants.TOKEN_PREFIX.length());43 jwt = authHeader.substring(7); 45 44 userEmail = jwtService.extractUsername(jwt); 46 45 -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/service/impl/JWTServiceImpl.java
r7d8eeab rdad25fc 6 6 import io.jsonwebtoken.io.Decoders; 7 7 import io.jsonwebtoken.security.Keys; 8 import mk.ukim.finki.it.reservengo.constants.JWTConstants;9 8 import mk.ukim.finki.it.reservengo.model.domain.User; 10 9 import mk.ukim.finki.it.reservengo.service.intf.JWTService; … … 18 17 @Service 19 18 public class JWTServiceImpl implements JWTService { 19 20 private static final String SECRET_KEY = "7bce06d6331e532e8c4c85e6eaae4217711768fe1aa582b4549c2722a8ef0497"; 21 20 22 @Override 21 23 public String extractUsername(String token) { … … 35 37 @Override 36 38 public Key getSignInKey() { 37 byte[] keyBytes = Decoders.BASE64.decode( JWTConstants.SECRET_KEY);39 byte[] keyBytes = Decoders.BASE64.decode(SECRET_KEY); 38 40 return Keys.hmacShaKeyFor(keyBytes); 39 41 } … … 52 54 .claim("id", user.getId()) 53 55 .setIssuedAt(new Date()) 54 .setExpiration(new Date(System.currentTimeMillis() + JWTConstants.EXPIRATION_TIME))56 .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24)) 55 57 .signWith(getSignInKey(), SignatureAlgorithm.HS256) 56 58 .compact(); -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/AuthController.java
r7d8eeab rdad25fc 6 6 import mk.ukim.finki.it.reservengo.service.intf.AuthService; 7 7 import org.springframework.http.HttpStatus; 8 import org.springframework.http.MediaType; 8 9 import org.springframework.http.ResponseEntity; 9 import org.springframework.web.bind.annotation.PostMapping; 10 import org.springframework.web.bind.annotation.RequestBody; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RestController; 10 import org.springframework.web.bind.annotation.*; 13 11 14 12 @RestController … … 21 19 } 22 20 23 @PostMapping(value = "/register/customer" )21 @PostMapping(value = "/register/customer", produces = MediaType.APPLICATION_JSON_VALUE) 24 22 public ResponseEntity<JWTAuthenticationResponseDTO> registerCustomer(@RequestBody JWTAuthenticationRequestDTO jwtAuthenticationRequestDTO) { 25 23 return new ResponseEntity<>(authenticationService.registerCustomer(jwtAuthenticationRequestDTO), HttpStatus.CREATED); -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/GlobalExceptionHandler.java
r7d8eeab rdad25fc 6 6 import org.springframework.security.authentication.BadCredentialsException; 7 7 import org.springframework.security.core.AuthenticationException; 8 import org.springframework.security.core.userdetails.UsernameNotFoundException; 8 9 import org.springframework.web.bind.annotation.ExceptionHandler; 9 10 import org.springframework.web.bind.annotation.RestControllerAdvice; … … 14 15 @ExceptionHandler(BadCredentialsException.class) 15 16 public ResponseEntity<String> handleBadCredentials(BadCredentialsException ex) { 16 return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body( ex.getMessage());17 return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid email or password"); 17 18 } 18 19 19 20 @ExceptionHandler(EmailNotFoundException.class) 20 21 public ResponseEntity<String> handleUserNotFound(EmailNotFoundException ex) { 21 return ResponseEntity.status(HttpStatus.NOT_FOUND).body( ex.getMessage());22 return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); 22 23 } 23 24 24 25 @ExceptionHandler(AuthenticationException.class) 25 26 public ResponseEntity<String> handleUserNotFound(AuthenticationException ex) { 26 return ResponseEntity.status(HttpStatus.NOT_FOUND).body( ex.getMessage());27 return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Authentication failed"); 27 28 } 28 29 } -
ReserveNGo-backend/src/main/java/mk/ukim/finki/it/reservengo/web/UserController.java
r7d8eeab rdad25fc 20 20 21 21 @GetMapping("/info") 22 public ResponseEntity<?> getUserPersonalInfo(@AuthenticationPrincipal User u ser) {23 User u = userService.findUser(user.getId());24 return ResponseEntity.ok(u );22 public ResponseEntity<?> getUserPersonalInfo(@AuthenticationPrincipal User u) { 23 User user = userService.findUser(u.getId()); 24 return ResponseEntity.ok(user); 25 25 } 26 26
Note:
See TracChangeset
for help on using the changeset viewer.