<div dir="ltr"><div>Thanks for your answer. Once I have configured correctly the servlet, I can use <span style="font-size:13px">@RolesAllowed on my EJBs. </span>KeycloakPrincipal and KeycloakSecurityContext are now filled. <br></div><div><br></div><div>But now, I have a new issue. When users haven&#39;t permissions, jboss server is returning 500 (Internal Server Error) error code. Exact error is this:</div><div>javax.ejb.ejbaccessexception: jbas014502: invocation on method: public xxxx is not allowed.<br></div><div><br></div><div>I think it should return 403 (Forbidden) or 401 (Unauthorized), isn&#39;t it? Is this behavior correct? Is it a JBoss or Keycloak issue?  Is it possible to configure status code returned? </div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-01-20 15:13 GMT+01:00 Bill Burke <span dir="ltr">&lt;<a href="mailto:bburke@redhat.com" target="_blank">bburke@redhat.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You still need to set up servlet security though and all the security<br>
constraints.  Set up your security constraints to be very broad, i.e.<br>
&quot;*&quot;, then use @RolesAllowed within your EJBs.<br>
<br>
On 1/20/2015 8:15 AM, Juan Escot wrote:<br>
&gt; Yes, I already have created it. I&#39;m using Jboss EAP 6.3. I have<br>
&gt; installed the adapter. But I have found a difference between adapter<br>
&gt; installation in Keycloak 1.0.4.Final and 1.1.0.beta2.<br>
&gt;<br>
&gt; I&#39;m using 1.0.4.Final and I add this line (as described at for Jboss EAP<br>
&gt; at<br>
&gt; <a href="http://docs.jboss.org/keycloak/docs/1.0.4.Final/userguide/html/ch07.html#jboss-adapter-installation" target="_blank">http://docs.jboss.org/keycloak/docs/1.0.4.Final/userguide/html/ch07.html#jboss-adapter-installation</a><br>
&gt; ):<br>
&gt; &lt;extension module=&quot;org.keycloak.keycloak-as7-subsystem&quot;/&gt;<br>
&gt;<br>
&gt; In 1.1.0.beta2 this configuration seems to be only for AS7. Should I use<br>
&gt; this? If I try it, I get an error (JBAS014674 module cannot be loaded)<br>
&gt; &lt;extension module=&quot;org.keycloak.keycloak-subsystem&quot;/&gt;<br>
&gt;<br>
&gt; All changes made at my standalone.xml are:<br>
&gt;<br>
&gt; &lt;extensions&gt;<br>
&gt; &lt;extension module=&quot;org.keycloak.keycloak-as7-subsystem&quot;/&gt;<br>
&gt; ...<br>
&gt; &lt;/extensions&gt;<br>
&gt; ...<br>
&gt; &lt;security-domains&gt;<br>
&gt; &lt;security-domain name=&quot;keycloak&quot;&gt;<br>
&gt; &lt;authentication&gt;<br>
&gt; &lt;login-module code=&quot;org.keycloak.adapters.jboss.KeycloakLoginModule&quot;<br>
&gt; flag=&quot;required&quot;/&gt;<br>
&gt; &lt;/authentication&gt;<br>
&gt; &lt;/security-domain&gt;<br>
&gt; ...<br>
&gt; &lt;/security-domains&gt;<br>
&gt;<br>
&gt; Do you think is a configuration problem? Do any of my attemps to get<br>
&gt; user information should work? Which one?<br>
&gt;<br>
&gt; Regards,<br>
&gt; Juan Escot<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; 2015-01-20 12:41 GMT+01:00 Stian Thorgersen &lt;<a href="mailto:stian@redhat.com">stian@redhat.com</a><br>
&gt; &lt;mailto:<a href="mailto:stian@redhat.com">stian@redhat.com</a>&gt;&gt;:<br>
&gt;<br>
&gt;     For the security context to propagate to EJBs you need to create a<br>
&gt;     shared security domain, see<br>
&gt;     <a href="http://docs.jboss.org/keycloak/docs/1.1.0.Beta2/userguide/html/ch07.html#jboss-adapter-installation" target="_blank">http://docs.jboss.org/keycloak/docs/1.1.0.Beta2/userguide/html/ch07.html#jboss-adapter-installation</a><br>
&gt;<br>
&gt;     ----- Original Message -----<br>
&gt;      &gt; From: &quot;Juan Escot&quot; &lt;<a href="mailto:juan.escot@cdtec.es">juan.escot@cdtec.es</a> &lt;mailto:<a href="mailto:juan.escot@cdtec.es">juan.escot@cdtec.es</a>&gt;&gt;<br>
&gt;      &gt; To: <a href="mailto:keycloak-dev@lists.jboss.org">keycloak-dev@lists.jboss.org</a><br>
&gt;     &lt;mailto:<a href="mailto:keycloak-dev@lists.jboss.org">keycloak-dev@lists.jboss.org</a>&gt;<br>
&gt;      &gt; Sent: Tuesday, 20 January, 2015 11:46:36 AM<br>
&gt;      &gt; Subject: [keycloak-dev] Rest Service authentication.<br>
&gt;      &gt;<br>
&gt;      &gt; Hi,<br>
&gt;      &gt; I&#39;m developing an application with AngularJS and Rest Services.<br>
&gt;     I&#39;m using<br>
&gt;      &gt; Keycloak for authentication and role management.<br>
&gt;      &gt;<br>
&gt;      &gt; Mi Angular project is registered as &#39;confidential&#39; and work&#39;s<br>
&gt;     fine. It<br>
&gt;      &gt; refresh tokens and sends it on header like this:<br>
&gt;     &#39;Authorization:Bearer<br>
&gt;      &gt; eyJhbGciOiJSUzI1Ni...&#39;<br>
&gt;      &gt;<br>
&gt;      &gt; Mi java project is defined as &#39;bearer only&#39; and it&#39;s developed<br>
&gt;     with Java EJBs<br>
&gt;      &gt; as Rest Services. I need more control over permissions and roles,<br>
&gt;     so I don&#39;t<br>
&gt;      &gt; want to secure my project with security-contraints at web.xml.<br>
&gt;     I&#39;d like to<br>
&gt;      &gt; get user info and roles inside my Rest methods from token<br>
&gt;     received. I have<br>
&gt;      &gt; checked I received the token with this line:<br>
&gt;      &gt;<br>
&gt;      &gt; String token = request.getHeader(&quot;authorization&quot;);<br>
&gt;      &gt;<br>
&gt;      &gt; But, I can&#39;t get any additional information about user. I have tried<br>
&gt;      &gt; different approaches but I can&#39;t fin a solution. Could I have a<br>
&gt;     Keycloak<br>
&gt;      &gt; object with user info?.<br>
&gt;      &gt;<br>
&gt;      &gt; This is a fragment of my code with all my attemps:<br>
&gt;      &gt;<br>
&gt;      &gt; @Stateless<br>
&gt;      &gt; @LocalBean<br>
&gt;      &gt; @Path(&quot;/promociones&quot;)<br>
&gt;      &gt; @SecurityDomain(&quot;keycloak&quot;)<br>
&gt;      &gt; public class PromocionRest {<br>
&gt;      &gt; @Context<br>
&gt;      &gt; HttpServletRequest request;<br>
&gt;      &gt; @Context<br>
&gt;      &gt; SecurityContext securityContext;<br>
&gt;      &gt; @Resource<br>
&gt;      &gt; SessionContext sc;<br>
&gt;      &gt; @GET<br>
&gt;      &gt; @Produces(&quot;application/json&quot;)<br>
&gt;      &gt; @Path(&quot;/list&quot;)<br>
&gt;      &gt; //@RolesAllowed({ &quot;user&quot; }) &lt;-- If I use this annotation y get an<br>
&gt;     error.<br>
&gt;      &gt; @PermitAll<br>
&gt;      &gt; public RespuestaListaBase&lt;Promocion&gt; listadoPromociones(...){<br>
&gt;      &gt; KeycloakPrincipal principal =<br>
&gt;      &gt; (KeycloakPrincipal)securityContext.getUserPrincipal();<br>
&gt;      &gt; KeycloakSecurityContext session = (KeycloakSecurityContext)<br>
&gt;      &gt; request.getAttribute(KeycloakSecurityContext.class.getName());<br>
&gt;      &gt; if (sc!=null &amp;&amp; sc.getCallerPrincipal()!=null){<br>
&gt;      &gt; System.out.println(&quot;Principal&#39;s name according to EJB: &quot; +<br>
&gt;      &gt; sc.getCallerPrincipal().getName());<br>
&gt;      &gt; }<br>
&gt;      &gt;<br>
&gt;      &gt; System.out.println(&quot;Is user in role &#39;user&#39;? &quot; +<br>
&gt;      &gt; request.isUserInRole(&quot;user&quot;));<br>
&gt;      &gt;<br>
&gt;      &gt; String token = request.getHeader(&quot;authorization&quot;);<br>
&gt;      &gt; HttpClient client = new<br>
&gt;     HttpClientBuilder().disableTrustManager().build();<br>
&gt;      &gt; try {<br>
&gt;      &gt; String url = request.getRequestURL().toString();<br>
&gt;      &gt; url = url.substring(0, url.indexOf(&#39;/&#39;, 8));<br>
&gt;      &gt; HttpGet get = new HttpGet(url + &quot;/auth/admin/realms/demo/roles&quot;);<br>
&gt;      &gt; get.addHeader(&quot;Authorization&quot;, &quot;Bearer &quot; + token);<br>
&gt;      &gt; try {<br>
&gt;      &gt; HttpResponse response = client.execute(get);<br>
&gt;      &gt; if (response.getStatusLine().getStatusCode() != 200) {<br>
&gt;      &gt; //throw new Failure(response.getStatusLine().getStatusCode());<br>
&gt;      &gt; }<br>
&gt;      &gt; HttpEntity entity = response.getEntity();<br>
&gt;      &gt; InputStream is = entity.getContent();<br>
&gt;      &gt;<br>
&gt;      &gt; } catch (IOException e) {<br>
&gt;      &gt; throw new RuntimeException(e);<br>
&gt;      &gt; }<br>
&gt;      &gt; } finally {<br>
&gt;      &gt; client.getConnectionManager().shutdown();<br>
&gt;      &gt; }<br>
&gt;      &gt; }<br>
&gt;      &gt; }<br>
&gt;      &gt;<br>
&gt;      &gt; I also have configured jboss-web.xml like this:<br>
&gt;      &gt; &lt;jboss-web&gt;<br>
&gt;      &gt; &lt;security-domain&gt;keycloak&lt;/security-domain&gt;<br>
&gt;      &gt; &lt;/jboss-web&gt;<br>
&gt;      &gt;<br>
&gt;      &gt; And web.xml like this:<br>
&gt;      &gt; &lt;login-config&gt;<br>
&gt;      &gt; &lt;auth-method&gt;KEYCLOAK&lt;/auth-method&gt;<br>
&gt;      &gt; &lt;realm-name&gt;demo&lt;/realm-name&gt;<br>
&gt;      &gt; &lt;/login-config&gt;<br>
&gt;      &gt;<br>
&gt;      &gt; &lt;security-role&gt;<br>
&gt;      &gt; &lt;role-name&gt;user&lt;/role-name&gt;<br>
&gt;      &gt; &lt;/security-role&gt;<br>
&gt;      &gt;<br>
&gt;      &gt; Some notes about the code:<br>
&gt;      &gt; - KeycloakPrincipal principal =<br>
&gt;      &gt; (KeycloakPrincipal)securityContext.getUserPrincipal(); &lt;--<br>
&gt;     principal is<br>
&gt;      &gt; always null<br>
&gt;      &gt; - KeycloakSecurityContext session = (KeycloakSecurityContext)<br>
&gt;      &gt; request.getAttribute(KeycloakSecurityContext.class.getName());<br>
&gt;     &lt;-- session<br>
&gt;      &gt; is always null<br>
&gt;      &gt; - sc.getCallerPrincipal().getName() &lt;-- returns &#39;anonymous&#39;, so<br>
&gt;     it seems it<br>
&gt;      &gt; isn&#39;t taking security-domain?<br>
&gt;      &gt; - request.isUserInRole(&quot;user&quot;) &lt;-- returns null<br>
&gt;      &gt; - HttpResponse response = client.execute(get) &lt;-- throws an<br>
&gt;     exception:<br>
&gt;      &gt; org.jboss.resteasy.spi.UnauthorizedException: Bearer<br>
&gt;      &gt; - If I use @RolesAllowed({ &quot;user&quot; }) annotation I get this error:<br>
&gt;     JBAS014502:<br>
&gt;      &gt; The invocation is not allowed in the method<br>
&gt;      &gt; - String token = request.getHeader(&quot;authorization&quot;); &lt;-- I get<br>
&gt;      &gt; &#39;Authorization:Bearer eyJhbGciOiJSUzI1Ni...&#39;<br>
&gt;      &gt;<br>
&gt;      &gt; I suppose i&#39;m doing it wrong, but I don&#39;t know what is the<br>
&gt;     correct form.<br>
&gt;      &gt; Could I get user information from token received?<br>
&gt;      &gt;<br>
&gt;      &gt; Thanks in advance,<br>
&gt;      &gt; Juan Escot<br>
&gt;      &gt;<br>
&gt;      &gt; _______________________________________________<br>
&gt;      &gt; keycloak-dev mailing list<br>
&gt;      &gt; <a href="mailto:keycloak-dev@lists.jboss.org">keycloak-dev@lists.jboss.org</a> &lt;mailto:<a href="mailto:keycloak-dev@lists.jboss.org">keycloak-dev@lists.jboss.org</a>&gt;<br>
&gt;      &gt; <a href="https://lists.jboss.org/mailman/listinfo/keycloak-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-dev</a><br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; keycloak-dev mailing list<br>
&gt; <a href="mailto:keycloak-dev@lists.jboss.org">keycloak-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/keycloak-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-dev</a><br>
&gt;<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Bill Burke<br>
JBoss, a division of Red Hat<br>
<a href="http://bill.burkecentral.com" target="_blank">http://bill.burkecentral.com</a><br>
_______________________________________________<br>
keycloak-dev mailing list<br>
<a href="mailto:keycloak-dev@lists.jboss.org">keycloak-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-dev</a><br>
</font></span></blockquote></div><br></div>