获取当前用户失败:没有可用的 RequestContext
Failed to get current user: no RequestContext available
使用 xsuaa approuter login 获取登录用户对象时出现以下异常
User currentUser = UserAccessor.getCurrentUser();
我要在不使用 GuiceFilter 的情况下获取 currentUser 对象,如果我应用 GuiceFilter,则会出现以下异常。
任何人请建议我如何使用 GuiceFilter
获得 UserAccessor.getCurrentUser()
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.call(GuiceFilter.java:133)
at com.google.inject.servlet.GuiceFilter.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)
从堆栈跟踪来看,似乎 GuiceFilter
被调用而 RequestContextServletFilter
未被调用。 RequestContextServletFilter
初始化一个RequestContext
,这是获取用户信息的前提条件
您可以尝试在 web.xml 文件中显式声明 RequestContextServletFilter
吗?
基本上,Sander 的回答已经正确(请接受他的回答)。更准确地说,您在 web.xml 中的定义必须如下所示(省略之前的所有其他 ServletFilters):
<!-- 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>
我已经用这个尝试了你的最小非工作示例,异常消失了。使用 ALLOW_MOCKED_AUTH_HEADER: true 环境变量返回一个空用户,这足以证明过滤器在 Guice 过滤器之前应用:
使用 xsuaa approuter login 获取登录用户对象时出现以下异常
User currentUser = UserAccessor.getCurrentUser();
我要在不使用 GuiceFilter 的情况下获取 currentUser 对象,如果我应用 GuiceFilter,则会出现以下异常。
任何人请建议我如何使用 GuiceFilter
获得 UserAccessor.getCurrentUser()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.call(GuiceFilter.java:133)
at com.google.inject.servlet.GuiceFilter.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)
从堆栈跟踪来看,似乎 GuiceFilter
被调用而 RequestContextServletFilter
未被调用。 RequestContextServletFilter
初始化一个RequestContext
,这是获取用户信息的前提条件
您可以尝试在 web.xml 文件中显式声明 RequestContextServletFilter
吗?
基本上,Sander 的回答已经正确(请接受他的回答)。更准确地说,您在 web.xml 中的定义必须如下所示(省略之前的所有其他 ServletFilters):
<!-- 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>
我已经用这个尝试了你的最小非工作示例,异常消失了。使用 ALLOW_MOCKED_AUTH_HEADER: true 环境变量返回一个空用户,这足以证明过滤器在 Guice 过滤器之前应用: