Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : Ajout gestion exception sur appel ws authentification Sudoc et … #24

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading