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