diff --git a/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java b/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java index ac22ba4..e77edf8 100644 --- a/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java +++ b/src/main/java/com/epam/esm/filter/ServiceAuthenticationFilter.java @@ -1,6 +1,7 @@ package com.epam.esm.filter; +import com.epam.esm.model.UserDTO; import com.epam.esm.utils.openfeign.AuthFeignClient; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; @@ -20,7 +21,7 @@ import org.springframework.web.filter.OncePerRequestFilter; import java.io.IOException; -import java.util.List; +import java.util.Collections; import static com.epam.esm.utils.AuthConstants.AUTHENTICATION_BEARER_TOKEN; @@ -38,9 +39,10 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, return; } try { - String role = authClient.getRole(bearerToken).getBody(); + UserDTO user = authClient.getUserFromJwt(bearerToken).getBody(); UsernamePasswordAuthenticationToken authToken = - new UsernamePasswordAuthenticationToken(null, null, List.of(new SimpleGrantedAuthority(role))); + new UsernamePasswordAuthenticationToken(user, null, + Collections.singleton(new SimpleGrantedAuthority(user.getRole().name()))); SecurityContextHolder.getContext().setAuthentication(authToken); filterChain.doFilter(request, response); } catch (HttpClientErrorException | HttpServerErrorException e) { diff --git a/src/main/java/com/epam/esm/model/Provider.java b/src/main/java/com/epam/esm/model/Provider.java new file mode 100644 index 0000000..9085a0c --- /dev/null +++ b/src/main/java/com/epam/esm/model/Provider.java @@ -0,0 +1,5 @@ +package com.epam.esm.model; + +public enum Provider { + LOCAL,GOOGLE +} \ No newline at end of file diff --git a/src/main/java/com/epam/esm/model/Role.java b/src/main/java/com/epam/esm/model/Role.java new file mode 100644 index 0000000..1893e05 --- /dev/null +++ b/src/main/java/com/epam/esm/model/Role.java @@ -0,0 +1,5 @@ +package com.epam.esm.model; + +public enum Role { + USER, ADMIN +} \ No newline at end of file diff --git a/src/main/java/com/epam/esm/model/UserDTO.java b/src/main/java/com/epam/esm/model/UserDTO.java new file mode 100644 index 0000000..df1ebc7 --- /dev/null +++ b/src/main/java/com/epam/esm/model/UserDTO.java @@ -0,0 +1,19 @@ +package com.epam.esm.model; + +import lombok.*; + +@Builder +@Getter +@Setter +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class UserDTO { + private Long id; + private String name; + private String surname; + private String phone; + private String email; + private Provider provider; + private Role role; +} \ No newline at end of file diff --git a/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java b/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java index 66f474c..2c91fbf 100644 --- a/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java +++ b/src/main/java/com/epam/esm/utils/openfeign/AuthFeignClient.java @@ -1,5 +1,6 @@ package com.epam.esm.utils.openfeign; +import com.epam.esm.model.UserDTO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -7,6 +8,6 @@ @FeignClient(name = "auth-service", configuration = CustomFeignClientConfiguration.class) public interface AuthFeignClient { - @GetMapping("api/v1/auth/role") - ResponseEntity getRole(@RequestHeader(value = "Authorization") String authorizationHeader); + @GetMapping("api/v1/auth/user") + ResponseEntity getUserFromJwt(@RequestHeader(value = "Authorization") String authorizationHeader); } \ No newline at end of file