Skip to content

Commit

Permalink
Merge pull request #24 from abes-esr/ITEM-39-catcher-lerreur-sur-le-w…
Browse files Browse the repository at this point in the history
…s-dauthentification-1

Fix : Ajout gestion exception sur appel ws authentification Sudoc et …
  • Loading branch information
SamuelQuetin authored Jun 25, 2024
2 parents 9f8e1b5 + 7f74181 commit 35f8f97
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
7 changes: 7 additions & 0 deletions web/src/main/java/fr/abes/item/exception/WsAuthException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.abes.item.exception;

public class WsAuthException extends RuntimeException {
public WsAuthException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {

Expand All @@ -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" +
Expand All @@ -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) {
Expand Down

0 comments on commit 35f8f97

Please sign in to comment.