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
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
|
||||
http.authorizeHttpRequests(auth -> auth.requestMatchers("/auth/login", "/v3/api-docs/**", "/swagger-ui/**",
|
||||
"/swagger-ui.html","/auth/refreshtoken", "/swagger-ui/index.html").permitAll().anyRequest().authenticated())
|
||||
http.authorizeHttpRequests(auth -> auth.requestMatchers(
|
||||
"/auth/**",
|
||||
"/v3/api-docs/**",
|
||||
"/swagger-ui/**",
|
||||
"/swagger-ui.html",
|
||||
"/swagger-ui/index.html").permitAll().anyRequest().authenticated())
|
||||
|
||||
// API pura → sem Basic
|
||||
.httpBasic(httpBasic -> httpBasic.disable())
|
||||
|
|
|
|||
|
|
@ -1,61 +1,44 @@
|
|||
package br.com.rayankonecny.authserviceapi.controllers.exceptions;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
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.ExceptionHandler;
|
||||
|
||||
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.StandardError;
|
||||
|
||||
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_ACCEPTABLE;
|
||||
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ControllerAdvice
|
||||
public class ControllerExceptionHandler {
|
||||
|
||||
@ExceptionHandler(UsernameNotFoundException.class)
|
||||
ResponseEntity<StandardError> handleNotFoundException(final UsernameNotFoundException ex,
|
||||
final HttpServletRequest request) {
|
||||
ResponseEntity<StandardError> handleNotFoundException(final UsernameNotFoundException ex,final HttpServletRequest request) {
|
||||
|
||||
return ResponseEntity.status(NOT_FOUND).body(
|
||||
|
||||
StandardError.builder().timestamp(now()).status(NOT_FOUND.value()).error(NOT_FOUND.getReasonPhrase())
|
||||
.message(ex.getMessage()).path(request.getRequestURI()).build());
|
||||
StandardError.builder()
|
||||
.timestamp(now())
|
||||
.status(NOT_FOUND.value())
|
||||
.error(NOT_FOUND.getReasonPhrase())
|
||||
.message(ex.getMessage())
|
||||
.path(request.getRequestURI())
|
||||
.build());
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
ResponseEntity<StandardError> handleMethodArgumentNotValidException(final MethodArgumentNotValidException ex,
|
||||
final HttpServletRequest request) {
|
||||
@ExceptionHandler({BadCredentialsException.class,RefreshTokenExpired.class})
|
||||
ResponseEntity<StandardError> handleBadCredentialsException(final BadCredentialsException ex, final HttpServletRequest request) {
|
||||
|
||||
var error = ValidationException.builder().timestamp(now()).status(BAD_REQUEST.value()).error("Validation Exception")
|
||||
.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.status(UNAUTHORIZED).body(
|
||||
StandardError.builder()
|
||||
.timestamp(now())
|
||||
.status(UNAUTHORIZED.value())
|
||||
.error(UNAUTHORIZED.getReasonPhrase())
|
||||
.message(ex.getMessage())
|
||||
.path(request.getRequestURI())
|
||||
.build());
|
||||
}
|
||||
|
||||
return ResponseEntity.badRequest().body(error);
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
@Override
|
||||
public ResponseEntity<AuthenticationResponse> authenticate(@Valid
|
||||
AuthenticateRequest request) {
|
||||
public ResponseEntity<AuthenticationResponse> authenticate(@Valid AuthenticateRequest request) {
|
||||
|
||||
return ResponseEntity.ok(authService.authenticate(request));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,6 @@ public class AuthService {
|
|||
String token = jwtUtils.generateToken(user);
|
||||
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