diff --git a/core/src/main/java/fr/abes/item/core/constant/Constant.java b/core/src/main/java/fr/abes/item/core/constant/Constant.java index 842fa375..a54ba81c 100644 --- a/core/src/main/java/fr/abes/item/core/constant/Constant.java +++ b/core/src/main/java/fr/abes/item/core/constant/Constant.java @@ -228,7 +228,7 @@ public class Constant implements Serializable { public static final String ENTER_CREATION_DEMANDE_BY_USERNUM = "entree dans autoriserCreationDemandeParUserNum.........."; public static final String ENTER_AUTORISER_MAJ_UTILISATEUR_BY_USERNUM = "entree dans autoriserMajUtilisateurParUserNum.........."; public static final String ENTER_AUTHENTICATE = "entree dans authenticate..."; - public static final String ERROR_SUDOC_WS_AUTHENTICATION = "rejet du service web d'authentification Sudoc = "; + public static final String ERROR_SUDOC_WS_AUTHENTICATION = "rejet du service web d'authentification Sudoc"; public static final String ERROR_AUTHENTICATION_IN_SECURITY_CONTEXT = "Could not set user authentication in security context"; public static final String ERROR_BLOCKED_IP = "dans isblocked IP, attemptsCache.get(key) = "; public static final String NUMBER_IP_TENTATIVES = "NB de tentatives pour ip ="; diff --git a/web/src/main/java/fr/abes/item/exception/RestResponseEntityExceptionHandler.java b/web/src/main/java/fr/abes/item/exception/RestResponseEntityExceptionHandler.java index 28644f87..08dc692e 100644 --- a/web/src/main/java/fr/abes/item/exception/RestResponseEntityExceptionHandler.java +++ b/web/src/main/java/fr/abes/item/exception/RestResponseEntityExceptionHandler.java @@ -45,6 +45,9 @@ public ResponseEntity<?> handleMiscFailures(Throwable t) { @ExceptionHandler({StorageFileNotFoundException.class}) public ResponseEntity<?> handleStorageFileNotFound(Throwable t) { return errorResponse(t, HttpStatus.NOT_FOUND);} + @ExceptionHandler({WsAuthException.class}) + public ResponseEntity<?> handleWsAuthException(Throwable t) { return errorResponse(t, HttpStatus.BAD_GATEWAY);} + protected ResponseEntity<ExceptionMessage> errorResponse(Throwable throwable, HttpStatus status) { if (null != throwable) { log.error(Constant.ERROR_CAUGHT + throwable.getMessage()); diff --git a/web/src/main/java/fr/abes/item/exception/WsAuthException.java b/web/src/main/java/fr/abes/item/exception/WsAuthException.java new file mode 100644 index 00000000..63c247ed --- /dev/null +++ b/web/src/main/java/fr/abes/item/exception/WsAuthException.java @@ -0,0 +1,7 @@ +package fr.abes.item.exception; + +public class WsAuthException extends RuntimeException { + public WsAuthException(String message) { + super(message); + } +} diff --git a/web/src/main/java/fr/abes/item/security/CustomAuthenticationManager.java b/web/src/main/java/fr/abes/item/security/CustomAuthenticationManager.java index 1eaf153c..18e74b98 100644 --- a/web/src/main/java/fr/abes/item/security/CustomAuthenticationManager.java +++ b/web/src/main/java/fr/abes/item/security/CustomAuthenticationManager.java @@ -2,6 +2,7 @@ import fr.abes.item.core.constant.Constant; import fr.abes.item.core.service.UtilisateurService; +import fr.abes.item.exception.WsAuthException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.EmptyResultDataAccessException; @@ -19,7 +20,6 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; -import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; @@ -53,7 +53,7 @@ public Authentication authenticate(Authentication authentication) String name = authentication.getName(); String password = authentication.getCredentials().toString(); - User u = this.estAuthentifie(name, password); + User u = this.callWsAuth(name, password); if (u != null) { @@ -78,8 +78,7 @@ public Authentication authenticate(Authentication authentication) } - private User estAuthentifie(String userKey, String password) { - + private User callWsAuth(String userKey, String password) { try { RestTemplate restTemplate = new RestTemplate(); String requestJson = "{\n" + @@ -94,9 +93,9 @@ private User estAuthentifie(String userKey, String password) { HttpEntity<String> entity = new HttpEntity<>(requestJson, headers); return restTemplate.postForObject(this.urlWsAuthSudoc, entity, User.class); } - catch (HttpClientErrorException e) { - log.info(Constant.ERROR_SUDOC_WS_AUTHENTICATION + e); - return null; + catch (Exception e) { + log.error(Constant.ERROR_SUDOC_WS_AUTHENTICATION + e); + throw new WsAuthException(e.getMessage()); } } public String getEmail(Integer userNum) {