Skip to content

Instantly share code, notes, and snippets.

@h-hub
Created February 17, 2022 13:26
Show Gist options
  • 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.

Revisions

  1. h-hub created this gist Feb 17, 2022.
    30 changes: 30 additions & 0 deletions MyRequestContext.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    @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;

    }

    }