Difference between revisions of "Resource Server jwt Sample code"

From ESS-WIKI
Jump to: navigation, search
(Created page with "*Maven Dependency <pre><code data-lang="xml"><dependencies> <dependency> <groupId>org.keycloak</groupId> <artifactId>keycloak-authz-client</artifactId>...")
 
 
Line 1: Line 1:
*Maven Dependency
+
*Restful API for idTokenString from Openid Server
<pre><code data-lang="xml"><dependencies>
+
<pre>Base64.Decoder decoder = Base64.getDecoder();
    <dependency>
+
String[]&nbsp; tokenParts&nbsp; = idTokenString.split("\\.");
        <groupId>org.keycloak</groupId>
+
String tokenpayload = new String(decoder.decode(tokenParts[1]), "UTF-8");
        <artifactId>keycloak-authz-client</artifactId>
+
JSONObject jsonObj = new JSONObject(tokenpayload);
        <version>${KEYCLOAK_VERSION}</version>
+
String username = jsonObj.getJSONObject("name").toString();
    </dependency>
+
String email = jsonObj.getJSONObject("email").toString();</pre>
</dependencies></code></pre>
 
 
 
*Obtaining User Entitlements
 
<pre><code data-lang="java">// create a new instance based on the configuration defined in keycloak-authz.json
 
AuthzClient authzClient = AuthzClient.create();
 
 
 
// obtain an Entitlement API Token to get access to the Entitlement API.
 
// this token is an access token issued to a client on behalf of an user
 
// with a scope = kc_entitlement
 
String eat = getEntitlementAPIToken(authzClient);
 
 
 
// send the entitlement request to the server to
 
// obtain an RPT with all permissions granted to the user
 
EntitlementResponse response = authzClient.entitlement(eat)
 
    .getAll("hello-world-authz-service");
 
String rpt = response.getRpt();
 
 
 
System.out.println("You got a RPT: " + rpt);
 
 
 
// now you can use the RPT to access protected resources on the resource server</code></pre>
 

Latest revision as of 10:46, 17 January 2017

  • Restful API for idTokenString from Openid Server
Base64.Decoder decoder = Base64.getDecoder();
String[]  tokenParts  = idTokenString.split("\\.");
String tokenpayload = new String(decoder.decode(tokenParts[1]), "UTF-8");
JSONObject jsonObj = new JSONObject(tokenpayload);
String username = jsonObj.getJSONObject("name").toString();
String email = jsonObj.getJSONObject("email").toString();