4

Getting below exception while fetching logged in user object using xsuaa approuter login

User currentUser = UserAccessor.getCurrentUser();

I am to get currentUser object without using GuiceFilter, If I apply GuiceFilter then getting below exception.

Any one Please suggest me how to get UserAccessor.getCurrentUser() with GuiceFilter

com.sap.cloud.sdk.cloudplatform.security.user.exception.UserAccessException: Failed to get current user: no RequestContext available. Have you correctly configured a RequestContextServletFilter or have you wrapped your logic in a RequestContextExecutor when executing background tasks that are not triggered by a request?
at com.sap.cloud.sdk.cloudplatform.security.user.AbstractUserFacade.getCurrentUserIfAuthenticated(AbstractUserFacade.java:85)
at com.sap.cloud.sdk.cloudplatform.security.user.AbstractUserFacade.getCurrentUser(AbstractUserFacade.java:135)
at com.sap.cloud.sdk.cloudplatform.security.user.UserAccessor.getCurrentUser(UserAccessor.java:122)
at com.company.HelloWorldServlet2.handleRequest(HelloWorldServlet2.java:35)
at com.company.BaseServlet.doPost(BaseServlet.java:120)
at com.company.BaseServlet.doGet(BaseServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at com.google.inject.servlet.ServletDefinition.doServiceImpl(ServletDefinition.java:287)
at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:277)
at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:182)
at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91)
at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:119)
at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:133)
at com.google.inject.servlet.GuiceFilter$1.call(GuiceFilter.java:130)
at com.google.inject.servlet.GuiceFilter$Context.call(GuiceFilter.java:203)
at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:130)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at com.sap.xs.java.valves.ErrorReportValve.invoke(ErrorReportValve.java:66)
at ch.qos.logback.access.tomcat.LogbackValve.invoke(LogbackValve.java:191)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at com.sap.xs.jdbc.datasource.valve.JDBCValve.invoke(JDBCValve.java:62)
at com.sap.xs.security.UserInfoValve.invoke(UserInfoValve.java:19)
at com.sap.xs.statistics.tomcat.valve.RequestTracingValve.invoke(RequestTracingValve.java:43)
at com.sap.xs.logging.catalina.RuntimeInfoValve.invoke(RuntimeInfoValve.java:40)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:695)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1136)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:836)
Sander Wozniak
  • 650
  • 8
  • 27
Dama Ramesh
  • 159
  • 14
  • Could you provide some details on the SDK version and project archetype that you are using? – Sander Wozniak Nov 15 '18 at 11:55
  • I did build application using https://blogs.sap.com/2017/07/18/step-7-with-sap-s4hana-cloud-sdk-secure-your-application-on-sap-cloud-platform-cloudfoundry/ – Dama Ramesh Nov 15 '18 at 12:22

2 Answers2

1

Essentially, Sander's answer is already correct (please kindly accept his). To be more precise your definition in web.xml must look the following way (omitting all the other ServletFilters before):

<!-- other filter go here -->

<filter>
    <filter-name>RequestContextServletFilter</filter-name>
    <filter-class>com.sap.cloud.sdk.cloudplatform.servlet.RequestContextServletFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>RequestContextServletFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>guiceFilter</filter-name>
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>guiceFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I've tried your minimal non-working example with this and the exception disappears. Using the ALLOW_MOCKED_AUTH_HEADER: true environment variable an empty user is returned which is proof enough that the filter applies before the Guice filter:

enter image description here

Philipp Herzig
  • 350
  • 6
  • 10
  • Philipp, I am confusing.. My application should work with login authentication (http://localhost:5000/hello1) right? I need to get loggedin user object & Destination object. Please suggest me good approach... – Dama Ramesh Nov 26 '18 at 03:47
  • Your question was about the RequestContext exception. If this has disappeared, I suggest accepting this answer and raising a follow-up question if you have troubles gerting the user principal from a secured application. – Philipp Herzig Nov 26 '18 at 07:57
0

From the stack trace, it looks like the GuiceFilter is invoked while the RequestContextServletFilter is not. The RequestContextServletFilter initializes a RequestContext, which is a prerequisite for retrieving user information.

Can you try to explicitly declare the RequestContextServletFilter in your web.xml file?

Sander Wozniak
  • 650
  • 8
  • 27
  • Not working after adding `RequestContextServletFilter` in my web.xml – Dama Ramesh Nov 15 '18 at 09:38
  • Getting same exception after adding `new RequestContextExecutor().execute(...);` also. Suggest me what I missed – Dama Ramesh Nov 15 '18 at 11:39
  • Could you please present the entire stacktrace again and whether the RequestContextServletFilter now appears in the trace? If yes, could you please post a MWE so that we can reproduce the issue? – Philipp Herzig Nov 15 '18 at 20:31
  • Philipp Herzig, I posted complete logs in https://answers.sap.com/questions/688944/failed-to-get-current-user-no-requestcontext-avail.html – Dama Ramesh Nov 16 '18 at 06:04
  • Well, the RequestContextServletFilter is still not in the call trace. Could you please post your web.xml to which context you have mapped it? – Philipp Herzig Nov 16 '18 at 06:47
  • Philipp Herzig, Please suggest me. I posted my `web.xml` in https://answers.sap.com/questions/688944/failed-to-get-current-user-no-requestcontext-avail.html – Dama Ramesh Nov 17 '18 at 18:57
  • Philipp, Any solution? – Dama Ramesh Nov 19 '18 at 09:43
  • 1
    Can you try to move the RequestContextServletFilter before the GuiceFilter in the web.xml? – Sander Wozniak Nov 19 '18 at 14:46
  • Sander, Not working. I moved RequestContextServletFilter before the GuiceFilter in web.xml. Now I am able to get `UserInfo` from `SecurityContext.getUserInfo();` But I need to access `UserAccessor` & `DestinationAccessor` to get `ScpCfUser` and destination after login. Suggest me how to get? I am getting always below LOG `INFO com.sap.cloud.sdk.cloudplatform.servlet.RequestContextServletFilter - No RequestContextFactory configured. Falling back to DefaultRequestContextFactory.` – Dama Ramesh Nov 20 '18 at 17:33
  • Could you share a small non-working example that would help us to reproduce and investigate the issue? Thanks! – Sander Wozniak Nov 21 '18 at 09:28
  • 1
    Sander, I uploaded non-working example in [github](https://github.com/rameshdamas/scp-cloudfoundry), please suggest me. – Dama Ramesh Nov 22 '18 at 09:35