<div dir="ltr">Ok, I was using getId() ok got it<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 11, 2015 at 3:54 AM, Marek Posolda <span dir="ltr">&lt;<a href="mailto:mposolda@redhat.com" target="_blank">mposolda@redhat.com</a>&gt;</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">
    <div>I suggest to look into our demo, which
      handles this well and shows various info about user:
      <a href="https://github.com/keycloak/keycloak/tree/master/examples/demo-template" target="_blank">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&#39;re configure <span style="color:#008000;font-weight:bold">&quot;principal-attribute&quot;</span>
      
      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&#39;s pretty
      straightforward from the getter methods. Like getter for email as
      Scott pointed)<span class="HOEnZb"><font color="#888888"><br>
      <br>
      Marek</font></span><div><div class="h5"><br>
      <br>
      On 11.7.2015 01:27, Juan Diego wrote:<br>
    </div></div></div><div><div class="h5">
    <blockquote 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">&lt;<a href="mailto:scott@xigole.com" target="_blank">scott@xigole.com</a>&gt;</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>
                <pre>import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.IDToken;</pre>
              </span>
              <pre>@Path(&quot;/user&quot;)
@Stateless
public class UserService {
    private static final Log log = LogFactory.getLog(UserService.class);</pre>
              <pre>    @Resource
    private SessionContext sessionContext;

    @Path(&quot;/getCurrentUserInfo&quot;)
    @Produces({ MediaType.APPLICATION_JSON })
    @GET
    @RolesAllowed({&quot;someRole&quot;})
    public Response getCurrentUser() {

        @SuppressWarnings(&quot;unchecked&quot;)
        KeycloakPrincipal&lt;KeycloakSecurityContext&gt; kcPrincipal = (KeycloakPrincipal&lt;KeycloakSecurityContext&gt;)(sessionContext.getCallerPrincipal());
        IDToken idToken = kcPrincipal.getKeycloakSecurityContext().getIdToken();

        log.debug( &quot;email from token is \&quot;&quot; + idToken.getEmail() + &quot;\&quot;&quot; );        </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><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">&lt;<a href="mailto:scott@xigole.com" target="_blank">scott@xigole.com</a>&gt;</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(&quot;unchecked&quot;)
KeycloakPrincipal&lt;KeycloakSecurityContext&gt; kcPrincipal = (KeycloakPrincipal&lt;KeycloakSecurityContext&gt;)(sessionContext.getCallerPrincipal());
IDToken idToken = kcPrincipal.getKeycloakSecurityContext().getIdToken();

log.debug( &quot;email from token is \&quot;&quot; + idToken.getEmail() + &quot;\&quot;&quot; );</pre>
                            <br>
                            Not sure if that&#39;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 href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a>
<a 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 href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a><br>
                          <a 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></fieldset>
      <br>
      <pre>_______________________________________________
keycloak-user mailing list
<a href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a></pre>
    </blockquote>
    <br>
  </div></div></div>

</blockquote></div><br></div>