Skip to content

Instantly share code, notes, and snippets.

@h-hub
Created February 17, 2022 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-hub/274b1b8a0a413ee1c21cbac5d41ce4c3 to your computer and use it in GitHub Desktop.
Save h-hub/274b1b8a0a413ee1c21cbac5d41ce4c3 to your computer and use it in GitHub Desktop.
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyRequestContext {
private Jwt jwt;
public MyRequestContext(){
this.jwt = (Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
public String getUsername() {
String username = jwt.getClaim("sub");
return username;
}
public boolean isAdmin() {
List<String> claims = jwt.getClaim("cognito:groups");
if(claims.contains("ROLE_ADMIN")) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment