Created
October 6, 2018 11:04
-
-
Save rkbalgi/ae1169e047fa88460791434beaafeb74 to your computer and use it in GitHub Desktop.
Check permissions of a user in Keycloak with Java API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AccessTokenResponse token = authzClient | |
.obtainAccessToken(userName, password); | |
final AuthorizationRequest authReq = new AuthorizationRequest(); | |
//checking for a specific permission | |
authReq.setMetadata(new Metadata()); | |
authReq.getMetadata().setResponseMode("decision"); | |
authReq.addPermission("payroll", "write"); | |
AuthorizationResponse authResponse = null; | |
try { | |
authResponse = authzClient | |
.authorization(token.getToken()).authorize(authReq); | |
LOG.warn("Permission granted .. for ..."); | |
authReq.getPermissions().getPermissions().forEach(p -> { | |
LOG.debug(p.getResourceId() + " with scopes - " + p.getScopes()); | |
}); | |
} catch (AuthorizationDeniedException e) { | |
LOG.warn("Permission denied .. for "); | |
authReq.getPermissions().getPermissions().forEach(p -> { | |
LOG.debug(p.getResourceId() + " with scopes - " + p.getScopes()); | |
}); | |
return JsonNodeFactory.instance.objectNode().put("token", token.getToken()); | |
} | |
LOG.info("RPT = " + authResponse.getToken()); | |
TokenIntrospectionResponse introspectionResponse = authzClient | |
.protection() | |
.introspectRequestingPartyToken(authResponse.getToken()); | |
introspectionResponse.getPermissions().stream().forEach(p -> { | |
LOG.debug(p.getResourceName() + "-- scopes: " + p.getScopes() + " -" + p.getResourceId()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're using 4.4 and wondering if you have to use application.yml or keycloak.json then the answer is "use application.yml only". Below is a sample -
Please note that the last part DISABLES the URI (i.e. unprotected URL's)