diff --git a/src/main/java/com/aolda/itda/config/AuthInterceptor.java b/src/main/java/com/aolda/itda/config/AuthInterceptor.java index 7c9f87f59927421bd074b1985e7faac7645e8a84..fd78fb63aface7d0d134112e8e0a601a46645296 100644 --- a/src/main/java/com/aolda/itda/config/AuthInterceptor.java +++ b/src/main/java/com/aolda/itda/config/AuthInterceptor.java @@ -43,10 +43,10 @@ public class AuthInterceptor implements HandlerInterceptor { if (projectId != null) { try { - String role = authService.getBestRoleWithinProject(token, projectId).get("role"); - if (!role.equals("admin")) { - log.error("Unauthorized Token for URI {}: {}", request.getRequestURI(), request.getRemoteAddr()); + authService.getBestRoleWithinProject(token, projectId).get("role"); + if (!request.getMethod().equals("GET") && !authService.getBestRoleWithinProject(token, projectId).get("role").equals("admin")) { throw new CustomException(ErrorCode.UNAUTHORIZED_USER, request.getRequestURI()); + } } catch (Exception e) { throw new CustomException(ErrorCode.UNAUTHORIZED_USER, request.getRequestURI()); diff --git a/src/main/java/com/aolda/itda/service/AuthService.java b/src/main/java/com/aolda/itda/service/AuthService.java index a222e289e1535a22955f14238429c1a30f7677e0..4c1740ec38b088511b9e4cf68afc54ac71fb365f 100644 --- a/src/main/java/com/aolda/itda/service/AuthService.java +++ b/src/main/java/com/aolda/itda/service/AuthService.java @@ -96,7 +96,6 @@ public class AuthService { try { res = restTemplate.postForEntity(url, requestEntity, Map.class); } catch (Exception e) { - e.printStackTrace(); throw new CustomException(ErrorCode.INVALID_USER_INFO); } Map<String, Object> resToken = (Map<String, Object>) res.getBody().get("token"); @@ -140,7 +139,7 @@ public class AuthService { try { requestEntity = new HttpEntity<>(requestBody, headers); res = restTemplate.postForEntity(url, requestEntity, Map.class); - } catch (RuntimeException e) { + } catch (Exception e) { return null; } @@ -185,8 +184,7 @@ public class AuthService { } catch (HttpClientErrorException.Forbidden e) { return unscopedToken; } - catch (RuntimeException e) { - e.printStackTrace(); + catch (Exception e) { throw new CustomException(ErrorCode.INVALID_TOKEN); } @@ -290,7 +288,7 @@ public class AuthService { ResponseEntity<String> res; try { res = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); - } catch (HttpClientErrorException.NotFound e) { + } catch (Exception e) { throw new CustomException(ErrorCode.INVALID_TOKEN); } return objectMapper.readTree(res.getBody()).path("token").path("user").path("id").asText(); @@ -305,7 +303,7 @@ public class AuthService { ResponseEntity<String> res; try { res = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); - } catch (HttpClientErrorException.NotFound e) { + } catch (Exception e) { throw new CustomException(ErrorCode.INVALID_TOKEN); } @@ -338,8 +336,7 @@ public class AuthService { ResponseEntity<String> res; try { res = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class); - } catch (RuntimeException e) { - e.printStackTrace(); + } catch (Exception e) { return false; } JsonNode node = objectMapper.readTree(res.getBody()).path("role_assignments");