<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">I suggest to look into our demo, which
handles this well and shows various info about user:
<a class="moz-txt-link-freetext" href="https://github.com/keycloak/keycloak/tree/master/examples/demo-template">https://github.com/keycloak/keycloak/tree/master/examples/demo-template</a><br>
<br>
By default, the User ID can be obtained directly from the
principal (unless you're configure <span
style="color:#008000;font-weight:bold;">"principal-attribute"</span>
<meta http-equiv="content-type" content="text/html;
charset=windows-1252">
in your keycloak.json ) :<br>
<br>
String userId = kcPrincipal.getName();<br>
<br>
From access token it can be obtained as well:<br>
<br>
String userId = accessToken.getSubject()<br>
<br>
See the example on how to retrieve more user data (but it's pretty
straightforward from the getter methods. Like getter for email as
Scott pointed)<br>
<br>
Marek<br>
<br>
On 11.7.2015 01:27, Juan Diego wrote:<br>
</div>
<blockquote
cite="mid:CAJGEj6cJ+Q7OgZRk_hC4EJHjK_ChYHrhJv+Et8ZCni35JjFVwA@mail.gmail.com"
type="cite">
<div dir="ltr">Are you identifying you user mainly by the email?<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Jul 10, 2015 at 6:24 PM, Scott
Dunbar <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:scott@xigole.com" target="_blank">scott@xigole.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"> It is injected into
the bean - sorry, might not have been enough code before.
A small example:<br>
<pre>import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;</pre>
<pre>import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;</pre>
<span class="">
<pre>import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.IDToken;</pre>
</span>
<pre>@Path("/user")
@Stateless
public class UserService {
private static final Log log = LogFactory.getLog(UserService.class);</pre>
<pre> @Resource
private SessionContext sessionContext;
@Path("/getCurrentUserInfo")
@Produces({ MediaType.APPLICATION_JSON })
@GET
@RolesAllowed({"someRole"})
public Response getCurrentUser() {
        @SuppressWarnings("unchecked")
KeycloakPrincipal<KeycloakSecurityContext> kcPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>)(sessionContext.getCallerPrincipal());
IDToken idToken = kcPrincipal.getKeycloakSecurityContext().getIdToken();
log.debug( "email from token is \"" + idToken.getEmail() + "\"" ); </pre>
<pre> // your return is likely something more useful
return Response.ok().build();
}
}</pre>
<br>
Your use case might be different but this is how it is
working for me. Again, there may be a better way.
<div>
<div class="h5"><br>
<br>
<br>
<br>
<div>On 07/10/2015 05:01 PM, Juan Diego wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">Where do you get sessionContext from?<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Jul 10, 2015 at
5:54 PM, Scott Dunbar <span dir="ltr"><<a
moz-do-not-send="true"
href="mailto:scott@xigole.com"
target="_blank">scott@xigole.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0
0 0 .8ex;border-left:1px #ccc
solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"> I use
something like:<br>
<br>
<pre>import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.IDToken;
...
@Resource
private SessionContext sessionContext;
...
@SuppressWarnings("unchecked")
KeycloakPrincipal<KeycloakSecurityContext> kcPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>)(sessionContext.getCallerPrincipal());
IDToken idToken = kcPrincipal.getKeycloakSecurityContext().getIdToken();
log.debug( "email from token is \"" + idToken.getEmail() + "\"" );</pre>
<br>
Not sure if that's the recommended way but
it works well. <br>
<div>
<div> <br>
<br>
<div>On 07/10/2015 04:48 PM, Juan Diego
wrote:<br>
</div>
</div>
</div>
<blockquote type="cite">
<div>
<div>
<div dir="ltr">
<div>
<div>
<div>
<div>Hi<br>
<br>
</div>
I want to be able to update
the user password and some
preferences from my web app,
in order to update some of the
user info from my portal i can
see in the rest api that you
need the user ID. <br>
</div>
I have a backend with java that
should connect to my keycloak
server once it gets the token<br>
<br>
KeycloakSecurityContext
securityContext =
(KeycloakSecurityContext)
httpRequest<br>
.getAttribute(KeycloakSecurityContext.class.getName());<br>
<br>
AccessToken accessToken =
securityContext.getToken();<br>
<br>
</div>
I dont know how to get info from
the accesToken, or does the access
token class already has methods to
do that. I know this is more of a
question of design. This part is
not really clear for me.<br>
<br>
</div>
Thanks<br>
<br>
</div>
<br>
<fieldset></fieldset>
<br>
</div>
</div>
<pre>_______________________________________________
keycloak-user mailing list
<a moz-do-not-send="true" href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a>
<a moz-do-not-send="true" href="https://lists.jboss.org/mailman/listinfo/keycloak-user" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a></pre>
<span><font color="#888888"> </font></span></blockquote>
<span><font color="#888888"> <br>
<div>-- <br>
<span style="font-weight:bold">Scott
Dunbar</span><br>
<span>Xigole Systems, Inc.</span><br>
<span>Enterprise consulting,
development, and hosting</span><br>
<span>303·667·6343</span><br>
</div>
</font></span></div>
<br>
_______________________________________________<br>
keycloak-user mailing list<br>
<a moz-do-not-send="true"
href="mailto:keycloak-user@lists.jboss.org"
target="_blank">keycloak-user@lists.jboss.org</a><br>
<a moz-do-not-send="true"
href="https://lists.jboss.org/mailman/listinfo/keycloak-user"
rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a><br>
</blockquote>
</div>
<br>
</div>
</blockquote>
<br>
<div>-- <br>
<span style="font-weight:bold">Scott Dunbar</span><br>
<span>Xigole Systems, Inc.</span><br>
<span>Enterprise consulting, development, and
hosting</span><br>
<span>303·667·6343</span><br>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
keycloak-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/keycloak-user">https://lists.jboss.org/mailman/listinfo/keycloak-user</a></pre>
</blockquote>
<br>
</body>
</html>