<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>PortalUtil.getUser(httpServletRequest) Does not return the logged in user</title>
  <link rel="self" href="https://liferay.dev/fi/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=122371711" />
  <subtitle>PortalUtil.getUser(httpServletRequest) Does not return the logged in user</subtitle>
  <id>https://liferay.dev/fi/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=122371711</id>
  <updated>2026-04-27T02:06:08Z</updated>
  <dc:date>2026-04-27T02:06:08Z</dc:date>
  <entry>
    <title>RE: RE: PortalUtil.getUser(httpServletRequest) Does not return the logged in user</title>
    <link rel="alternate" href="https://liferay.dev/fi/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122395996" />
    <author>
      <name>Leonildes Silva</name>
    </author>
    <id>https://liferay.dev/fi/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122395996</id>
    <updated>2024-02-12T17:46:39Z</updated>
    <published>2024-02-12T08:56:49Z</published>
    <summary type="html">&lt;p&gt;Hi Christoph Rabel, first, thank you very much for the return.&lt;/p&gt;
&lt;p&gt;Passing header in request works perfectly. However, in my case, the
  second suggestion would be more appropriate to my context. &lt;/p&gt;
&lt;p&gt;However, testing the configuration via
  &lt;strong&gt;portal-ext.properties&lt;/strong&gt; did not work. What am I missing
  in the configuration?&lt;br&gt; Here is the example.&lt;/p&gt;
&lt;p&gt;#whitelist services in portal-ext.properties:&lt;br&gt;
  auth.token.ignore.origins=\&lt;br&gt; com.test.rest.application.TestRestApplication&lt;/p&gt;
&lt;p&gt;I did, as described here:&lt;/p&gt;
&lt;p&gt;https://docs.liferay.com/portal/7.4-ga92/propertiesdoc/portal.properties.html&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Thank you!&lt;/p&gt;</summary>
    <dc:creator>Leonildes Silva</dc:creator>
    <dc:date>2024-02-12T08:56:49Z</dc:date>
  </entry>
  <entry>
    <title>RE: PortalUtil.getUser(httpServletRequest) Does not return the logged in user</title>
    <link rel="alternate" href="https://liferay.dev/fi/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122381043" />
    <author>
      <name>Christoph Rabel</name>
    </author>
    <id>https://liferay.dev/fi/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122381043</id>
    <updated>2024-02-05T14:58:07Z</updated>
    <published>2024-02-05T14:58:06Z</published>
    <summary type="html">&lt;p&gt;Newer versions of Liferay need the CSRF token in the request,
  preferrably as a header. If it isn't present, the auth verifier
  &amp;quot;strips&amp;quot; the user from the request and the call is not authenticated.&lt;/p&gt;
&lt;p&gt;So, we usually set it in our javascript applications modules as a
  header, e.g.&lt;br&gt;
  &lt;br&gt; fetch(&lt;br&gt;      &amp;quot;/o/&amp;lt;my-rest-service&amp;gt;,&lt;br&gt;      { 
  headers: {&amp;quot;x-csrf-token&amp;quot;: window.Liferay.authToken}}&lt;br&gt; ).then(..)&lt;br&gt;
  &lt;br&gt;
  &lt;strong&gt;This is the preferred approach!&lt;/strong&gt;
  &lt;br&gt;
  &lt;br&gt; But this is sometimes not possible (or tricky) for various
  reasons. So, there is a second mechanism to whitelist urls and not
  require the token. BUT you must do this only for readonly requests
  (GET Requests usually are, but who knows), and make sure, that there
  is no security issue due to the missing CSRF token. Again: The first
  approach is preferable.&lt;br&gt; ​​​​​​​You can whitelist services in
  portal-ext.properties:&lt;br&gt;
  auth.token.ignore.origins=&amp;lt;my-rest-service&amp;gt;,
  &amp;lt;my-rest-service2&amp;gt;, ...&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;</summary>
    <dc:creator>Christoph Rabel</dc:creator>
    <dc:date>2024-02-05T14:58:06Z</dc:date>
  </entry>
  <entry>
    <title>PortalUtil.getUser(httpServletRequest) Does not return the logged in user</title>
    <link rel="alternate" href="https://liferay.dev/fi/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122371710" />
    <author>
      <name>Leonildes Silva</name>
    </author>
    <id>https://liferay.dev/fi/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122371710</id>
    <updated>2024-01-31T17:43:13Z</updated>
    <published>2024-01-31T15:48:45Z</published>
    <summary type="html">&lt;p&gt;
  &lt;strong&gt;PortalUtil.getUser(httpServletRequest) &lt;/strong&gt;Does not
  return the logged in user, but the default user on &lt;strong&gt;Liferay
    Digital Experience Platform 7.4.13 Update 92&lt;/strong&gt;
  &lt;br&gt; I have a &lt;strong&gt;JAX-RS application &lt;/strong&gt;that has the
  implementation of retrieving the logged in user to perform some
  validations, the implementation was working as expected on  version
  &lt;strong&gt;U52&lt;/strong&gt;, but when I use the &lt;strong&gt;U95 &lt;/strong&gt;version
  does not work as expected, the returned user is default.&lt;/p&gt;
&lt;p&gt;Looking at the documentation and the github I was not able to notice
  something that may have changed between these versions, because
  apparently nothing has changed in the PortalUtil class from
    &lt;strong&gt;U52 &lt;/strong&gt;to &lt;strong&gt;U92&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Does anyone know if anything has changed and how can we now retrieve
  the logged in user in the context of a JAX-RS app?&lt;/p&gt;
&lt;p&gt;I tried this too and it didn't work:&lt;/p&gt;
&lt;p&gt;
  &lt;em&gt;
    &lt;strong&gt;ServiceContext serviceContext = ServiceContextFactory.getInstance(httpServletRequest);&lt;/strong&gt;
  &lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;em&gt;
    &lt;strong&gt;User user = serviceContext.getUser();&lt;/strong&gt;
  &lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;********************************************************&lt;/p&gt;
&lt;p&gt;
  &lt;em&gt;
    &lt;strong&gt;HttpSession httpSession = httpServletRequest.getSession();&lt;/strong&gt;
  &lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
  &lt;em&gt;
    &lt;strong&gt;User user = (User) httpSession.getAttribute(WebKeys.USER);&lt;/strong&gt;
  &lt;/em&gt;
&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Here’s an &lt;strong&gt;example &lt;/strong&gt;of what I’ve coded:&lt;/p&gt;
&lt;p&gt;@Component(immediate = true, &lt;br&gt;     property = {&lt;br&gt;       
   JaxrsWhiteboardConstants.JAX_RS_APPLICATION_BASE +
  &amp;quot;=greetings&amp;quot;,&lt;br&gt;       
   JaxrsWhiteboardConstants.JAX_RS_NAME +
  &amp;quot;=Greetings.Rest&amp;quot;,&lt;br&gt;       
   &amp;quot;jaxrs.application=true&amp;quot;,&lt;br&gt;       
   &amp;quot;auth.verifier.guest.allowed=false&amp;quot;,&lt;br&gt;       
   &amp;quot;liferay.oauth2=false&amp;quot;&lt;br&gt;     },&lt;/p&gt;
&lt;p&gt;    configurationPolicy = ConfigurationPolicy.OPTIONAL,&lt;br&gt;   
   service = Application.class&lt;br&gt; )&lt;/p&gt;
&lt;p&gt;@ApplicationPath(&amp;quot;/greetings&amp;quot;)&lt;br&gt; public class
  TestRestApplication extends Application {&lt;br&gt;     &lt;br&gt;     private
  static final Log LOG = LogFactoryUtil.getLog(TestRestApplication.class);&lt;/p&gt;
&lt;p&gt;    public Set&amp;lt;Object&amp;gt; getSingletons() {&lt;br&gt;       
   LOG.info(&amp;quot;Start******&amp;quot;);&lt;br&gt;         return
  Collections.&amp;lt;Object&amp;gt;singleton(this);&lt;br&gt;     }&lt;/p&gt;
&lt;p&gt;    @GET&lt;br&gt;     @Path(&amp;quot;/user/logged&amp;quot;)&lt;br&gt;   
   @Produces(&amp;quot;text/plain&amp;quot;)&lt;br&gt;     public String
   getUserLoggedIn(@Context HttpServletRequest httpServletRequest)
  throws PortalException {&lt;/p&gt;
&lt;p&gt;
  &lt;br&gt;         User user = PortalUtil.getUser(httpServletRequest);&lt;br&gt;
          String screenName=user.getScreenName();&lt;br&gt;         String
  emailAddress=user.getEmailAddress();&lt;br&gt;         &lt;br&gt;       
   LOG.info(&amp;quot;********screenName:&amp;quot;+screenName+&amp;quot;\n&amp;quot;+&amp;quot;emailAddress:&amp;quot;+emailAddress);&lt;br&gt;
          return
  &amp;quot;screenName:&amp;quot;+screenName+&amp;quot;\n&amp;quot;+&amp;quot;emailAddress:&amp;quot;+emailAddress;&lt;br&gt;
      }&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;</summary>
    <dc:creator>Leonildes Silva</dc:creator>
    <dc:date>2024-01-31T15:48:45Z</dc:date>
  </entry>
</feed>
