Fix type of throws exceptions
This commit is contained in:
parent
4efd4685ce
commit
b83d0b3faf
4 changed files with 28 additions and 42 deletions
|
|
@ -13,8 +13,12 @@ public class SecurityConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
|
|
||||||
http.authorizeHttpRequests(auth -> auth.requestMatchers("/auth/login", "/v3/api-docs/**", "/swagger-ui/**",
|
http.authorizeHttpRequests(auth -> auth.requestMatchers(
|
||||||
"/swagger-ui.html","/auth/refreshtoken", "/swagger-ui/index.html").permitAll().anyRequest().authenticated())
|
"/auth/**",
|
||||||
|
"/v3/api-docs/**",
|
||||||
|
"/swagger-ui/**",
|
||||||
|
"/swagger-ui.html",
|
||||||
|
"/swagger-ui/index.html").permitAll().anyRequest().authenticated())
|
||||||
|
|
||||||
// API pura → sem Basic
|
// API pura → sem Basic
|
||||||
.httpBasic(httpBasic -> httpBasic.disable())
|
.httpBasic(httpBasic -> httpBasic.disable())
|
||||||
|
|
|
||||||
|
|
@ -1,61 +1,44 @@
|
||||||
package br.com.rayankonecny.authserviceapi.controllers.exceptions;
|
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.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.ValidationException;
|
|
||||||
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 static java.time.LocalDateTime.now;
|
import static java.time.LocalDateTime.now;
|
||||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
|
||||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||||
import static org.springframework.http.HttpStatus.NOT_ACCEPTABLE;
|
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class ControllerExceptionHandler {
|
public class ControllerExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(UsernameNotFoundException.class)
|
@ExceptionHandler(UsernameNotFoundException.class)
|
||||||
ResponseEntity<StandardError> handleNotFoundException(final UsernameNotFoundException ex,
|
ResponseEntity<StandardError> handleNotFoundException(final UsernameNotFoundException ex,final HttpServletRequest request) {
|
||||||
final HttpServletRequest request) {
|
|
||||||
|
|
||||||
return ResponseEntity.status(NOT_FOUND).body(
|
return ResponseEntity.status(NOT_FOUND).body(
|
||||||
|
StandardError.builder()
|
||||||
StandardError.builder().timestamp(now()).status(NOT_FOUND.value()).error(NOT_FOUND.getReasonPhrase())
|
.timestamp(now())
|
||||||
.message(ex.getMessage()).path(request.getRequestURI()).build());
|
.status(NOT_FOUND.value())
|
||||||
|
.error(NOT_FOUND.getReasonPhrase())
|
||||||
|
.message(ex.getMessage())
|
||||||
|
.path(request.getRequestURI())
|
||||||
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler({BadCredentialsException.class,RefreshTokenExpired.class})
|
||||||
ResponseEntity<StandardError> handleMethodArgumentNotValidException(final MethodArgumentNotValidException ex,
|
ResponseEntity<StandardError> handleBadCredentialsException(final BadCredentialsException ex, final HttpServletRequest request) {
|
||||||
final HttpServletRequest request) {
|
|
||||||
|
|
||||||
var error = ValidationException.builder().timestamp(now()).status(BAD_REQUEST.value()).error("Validation Exception")
|
return ResponseEntity.status(UNAUTHORIZED).body(
|
||||||
.message("Exception in validation attributes").path(request.getRequestURI()).errors(new ArrayList<>()).build();
|
StandardError.builder()
|
||||||
|
.timestamp(now())
|
||||||
for (FieldError fieldError : ex.getBindingResult().getFieldErrors()) {
|
.status(UNAUTHORIZED.value())
|
||||||
error.addError(fieldError.getField(), fieldError.getDefaultMessage());
|
.error(UNAUTHORIZED.getReasonPhrase())
|
||||||
}
|
.message(ex.getMessage())
|
||||||
|
.path(request.getRequestURI())
|
||||||
return ResponseEntity.badRequest().body(error);
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(RefreshTokenExpired.class)
|
|
||||||
ResponseEntity<StandardError> handleRefreshTokenExpired(final RefreshTokenExpired ex,
|
|
||||||
final HttpServletRequest request) {
|
|
||||||
|
|
||||||
var error = ValidationException.builder().timestamp(now()).status(NOT_ACCEPTABLE.value())
|
|
||||||
.error("Validation Exception").message("Refresh Token Expired").path(request.getRequestURI())
|
|
||||||
.errors(new ArrayList<>()).build();
|
|
||||||
|
|
||||||
return ResponseEntity.badRequest().body(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ public class AuthControllerImpl implements AuthController {
|
||||||
private final RefreshTokenService refreshService;
|
private final RefreshTokenService refreshService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResponseEntity<AuthenticationResponse> authenticate(@Valid
|
public ResponseEntity<AuthenticationResponse> authenticate(@Valid AuthenticateRequest request) {
|
||||||
AuthenticateRequest request) {
|
|
||||||
|
|
||||||
return ResponseEntity.ok(authService.authenticate(request));
|
return ResponseEntity.ok(authService.authenticate(request));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,6 @@ public class AuthService {
|
||||||
String token = jwtUtils.generateToken(user);
|
String token = jwtUtils.generateToken(user);
|
||||||
RefreshToken refresh = refreshTokenService.save(user.getUsername());
|
RefreshToken refresh = refreshTokenService.save(user.getUsername());
|
||||||
|
|
||||||
return new AuthenticationResponse(token, refresh.getId(), user.getUsername());
|
return new AuthenticationResponse(token, refresh.getId(), "Bearer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue