Fix type of throws exceptions on methods
This commit is contained in:
parent
b83d0b3faf
commit
ff96ecf8a1
5 changed files with 38 additions and 17 deletions
|
|
@ -3,34 +3,41 @@ package br.com.rayankonecny.authserviceapi.controllers.exceptions;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.authentication.BadCredentialsException;
|
import org.springframework.security.authentication.BadCredentialsException;
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import br.com.rayankonecny.hdcommoslib.models.exceptions.RefreshTokenExpired;
|
import br.com.rayankonecny.hdcommoslib.models.exceptions.RefreshTokenExpired;
|
||||||
import br.com.rayankonecny.hdcommoslib.models.exceptions.StandardError;
|
import br.com.rayankonecny.hdcommoslib.models.exceptions.StandardError;
|
||||||
|
import br.com.rayankonecny.hdcommoslib.models.exceptions.ValidationException;
|
||||||
|
|
||||||
import static java.time.LocalDateTime.now;
|
import static java.time.LocalDateTime.now;
|
||||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||||
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
||||||
|
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class ControllerExceptionHandler {
|
public class ControllerExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(UsernameNotFoundException.class)
|
// @ExceptionHandler(UsernameNotFoundException.class)
|
||||||
ResponseEntity<StandardError> handleNotFoundException(final UsernameNotFoundException ex,final HttpServletRequest request) {
|
// ResponseEntity<StandardError> handleNotAuthorizedException(final UsernameNotFoundException ex,final HttpServletRequest request) {
|
||||||
|
|
||||||
return ResponseEntity.status(NOT_FOUND).body(
|
// return ResponseEntity.status(UNAUTHORIZED).body(
|
||||||
StandardError.builder()
|
// StandardError.builder()
|
||||||
.timestamp(now())
|
// .timestamp(now())
|
||||||
.status(NOT_FOUND.value())
|
// .status(UNAUTHORIZED.value())
|
||||||
.error(NOT_FOUND.getReasonPhrase())
|
// .error(UNAUTHORIZED.getReasonPhrase())
|
||||||
.message(ex.getMessage())
|
// .message(ex.getMessage())
|
||||||
.path(request.getRequestURI())
|
// .path(request.getRequestURI())
|
||||||
.build());
|
// .build());
|
||||||
}
|
// }
|
||||||
|
|
||||||
@ExceptionHandler({BadCredentialsException.class,RefreshTokenExpired.class})
|
@ExceptionHandler({BadCredentialsException.class,RefreshTokenExpired.class,UsernameNotFoundException.class})
|
||||||
ResponseEntity<StandardError> handleBadCredentialsException(final BadCredentialsException ex, final HttpServletRequest request) {
|
ResponseEntity<StandardError> handleBadCredentialsException(final RuntimeException ex, final HttpServletRequest request) {
|
||||||
|
|
||||||
return ResponseEntity.status(UNAUTHORIZED).body(
|
return ResponseEntity.status(UNAUTHORIZED).body(
|
||||||
StandardError.builder()
|
StandardError.builder()
|
||||||
|
|
@ -41,4 +48,18 @@ public class ControllerExceptionHandler {
|
||||||
.path(request.getRequestURI())
|
.path(request.getRequestURI())
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
ResponseEntity<StandardError> handleMethodArgumentNotValidException(final MethodArgumentNotValidException ex,
|
||||||
|
final HttpServletRequest request) {
|
||||||
|
|
||||||
|
var error = ValidationException.builder().timestamp(now()).status(BAD_REQUEST.value()).error("ValidationException")
|
||||||
|
.message("Exception in validation attributes").path(request.getRequestURI()).errors(new ArrayList<>()).build();
|
||||||
|
|
||||||
|
for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
|
||||||
|
error.addError(fieldError.getField(), fieldError.getDefaultMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.badRequest().body(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ public class AuthControllerImpl implements AuthController {
|
||||||
|
|
||||||
return ResponseEntity.ok(authService.authenticate(request));
|
return ResponseEntity.ok(authService.authenticate(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<RefreshTokenResponse> refreshToken(@Valid RefreshTokenRequest request) throws Exception {
|
public ResponseEntity<RefreshTokenResponse> refreshToken(@Valid RefreshTokenRequest request) throws Exception {
|
||||||
return ResponseEntity.ok().body(refreshService.refreshToken(request.refreshToken()));
|
return ResponseEntity.ok().body(refreshService.refreshToken(request.refreshToken()));
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ public class AuthService {
|
||||||
var user = userDetailsService.loadUserByUsername(request.email());
|
var user = userDetailsService.loadUserByUsername(request.email());
|
||||||
|
|
||||||
if (!passwordEncoder.matches(request.password(), user.getPassword())) {
|
if (!passwordEncoder.matches(request.password(), user.getPassword())) {
|
||||||
throw new BadCredentialsException("Invalid credentials");
|
throw new BadCredentialsException("Email or password invalid");
|
||||||
}
|
}
|
||||||
|
|
||||||
String token = jwtUtils.generateToken(user);
|
String token = jwtUtils.generateToken(user);
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
public UserDetailsDTO loadUserByUsername(final String email) throws UsernameNotFoundException {
|
public UserDetailsDTO loadUserByUsername(final String email) throws UsernameNotFoundException {
|
||||||
|
|
||||||
final var entity = repository.findByEmail(email)
|
final var entity = repository.findByEmail(email)
|
||||||
.orElseThrow(() -> new UsernameNotFoundException("User not found: " + email));
|
.orElseThrow(() -> new UsernameNotFoundException("Email or password invalid"));
|
||||||
|
|
||||||
return UserDetailsDTO.builder()
|
return UserDetailsDTO.builder()
|
||||||
.id(entity.getId())
|
.id(entity.getId())
|
||||||
|
|
|
||||||
|
|
@ -9,4 +9,4 @@ spring:
|
||||||
enabled: false
|
enabled: false
|
||||||
jwt.secret: "IHf3Yua/byvtA+iIcGWmkrLvpKEXTb5ClkXaZ0VDmYbr/6b1otCs38x68bidvZLAOB7anUtVQlCid6YDULO5XA=="
|
jwt.secret: "IHf3Yua/byvtA+iIcGWmkrLvpKEXTb5ClkXaZ0VDmYbr/6b1otCs38x68bidvZLAOB7anUtVQlCid6YDULO5XA=="
|
||||||
jwt.expiration: 120000
|
jwt.expiration: 120000
|
||||||
jwt.expiration-sec.refresh-token: 3600
|
jwt.expiration-sec.refresh-token: 5
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue