Skip to content

Commit

Permalink
Refactored method getRole to getUserFromJwt to have principal
Browse files Browse the repository at this point in the history
  • Loading branch information
vburmus committed Oct 21, 2023
1 parent a1cc5d5 commit f2b4a0d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/epam/esm/model/Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.epam.esm.model;

public enum Provider {
LOCAL,GOOGLE
}
5 changes: 5 additions & 0 deletions src/main/java/com/epam/esm/model/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.epam.esm.model;

public enum Role {
USER, ADMIN
}
19 changes: 19 additions & 0 deletions src/main/java/com/epam/esm/model/UserDTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "auth-service", configuration = CustomFeignClientConfiguration.class)
public interface AuthFeignClient {
@GetMapping("api/v1/auth/role")
ResponseEntity<String> getRole(@RequestHeader(value = "Authorization") String authorizationHeader);
@GetMapping("api/v1/auth/user")
ResponseEntity<UserDTO> getUserFromJwt(@RequestHeader(value = "Authorization") String authorizationHeader);
}

0 comments on commit f2b4a0d

Please sign in to comment.