如何从Spring代理对象中获取HttpServletRequest。

huangapple go评论73阅读模式
英文:

How to get HttpServletRequest from spring proxy object

问题

在拦截器类中我想获取HttpServletRequest对象

public class MyEndpointInterceptorAdapter extends EndpointInterceptorAdapter {

  @Autowired
  private HttpServletRequest httpServletRequest;

  // ...

}

实际上,我得到的是一个HttpServletRequest的代理对象。
(图片已省略)

我如何从这个代理的Spring对象中获取原始的HttpServletRequest对象?

更新1:

根据请求,以下是堆栈跟踪:
java.lang.ClassCastException: com.sun.proxy.$Proxy622 无法强制转换为 weblogic.servlet.internal.ServletRequestImpl
at weblogic.servlet.internal.ServletRequestImpl.getOriginalRequest(ServletRequestImpl.java:1654)
at weblogic.servlet.security.ServletAuthentication.runAs(ServletAuthentication.java:726)
at weblogic.servlet.security.ServletAuthentication.runAs(ServletAuthentication.java:710)
// 更多堆栈信息...


<details>
<summary>英文:</summary>
Into an interceptor class I want to get the HttpServletRequest object:
public class MyEndpointInterceptorAdapter extends EndpointInterceptorAdapter {
@Autowired
private HttpServletRequest httpServletRequest;
...
}
What I actually get is a proxy object of HttpServletRequest.
[![enter image description here][1]][1]
How I can get the original HttpServletRequest object out of this proxy spring object?
[1]: https://i.stack.imgur.com/UtEUt.png
Update1:
Per request here is the stack trace:
java.lang.ClassCastException: com.sun.proxy.$Proxy622 cannot be cast to weblogic.servlet.internal.ServletRequestImpl
at weblogic.servlet.internal.ServletRequestImpl.getOriginalRequest(ServletRequestImpl.java:1654)
at weblogic.servlet.security.ServletAuthentication.runAs(ServletAuthentication.java:726)
at weblogic.servlet.security.ServletAuthentication.runAs(ServletAuthentication.java:710)
at weblogic.servlet.security.ServletAuthentication.runAs(ServletAuthentication.java:681)
at eu.cec.digit.ecas.client.authentication.AbstractWeblogic9ServletAuthentication.impersonate(AbstractWeblogic9ServletAuthentication.java:170)
at eu.europa.ec.comp.cmr.iop.soap.security.interceptor.EcasTicketAuthenticationInterceptor.authenticateAndImpersonate(EcasTicketAuthenticationInterceptor.java:107)
at eu.europa.ec.comp.cmr.iop.soap.security.interceptor.EcasTicketAuthenticationInterceptor.handleRequest(EcasTicketAuthenticationInterceptor.java:89)
at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispatcher.java:227)
at org.springframework.ws.server.MessageDispatcher.receive(MessageDispatcher.java:176)
at org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport.handleConnection(WebServiceMessageReceiverObjectSupport.java:89)
at org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter.handle(WebServiceMessageReceiverHandlerAdapter.java:61)
at org.springframework.ws.transport.http.MessageDispatcherServlet.doService(MessageDispatcherServlet.java:293)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:884)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at eu.europa.ec.comp.cmr.iop.soap.security.filter.ExceptionHandlerFilter.doFilterInternal(ExceptionHandlerFilter.java:23)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:130)
at org.springframework.boot.web.servlet.support.ErrorPageFilter.access$000(ErrorPageFilter.java:66)
at org.springframework.boot.web.servlet.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.boot.web.servlet.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:123)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3701)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3667)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2443)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2291)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2269)
at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1703)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1663)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:272)
at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:352)
at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:337)
at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:57)
at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:644)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
</details>
# 答案1
**得分**: -1
从由Spring在@Autowired注释下返回的代理对象中获取答案时,您无法访问原始的HttpServletRequest对象。
您可以通过不同的路径从RequestContextHolder获取HttpServletRequest对象:
```java
HttpServletRequest httpServletRequest = 
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

这样可以获取到原始的HttpServletRequest对象。

英文:

Fund the answer, from the proxy object returned by spring on @Autowired, you dont have access to original HttpServletRequest object.

you can get the HttpServletRequest object, through a different path from: RequestContextHolder

HttpServletRequest httpServletRequest = 
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

This gives you the original HttpServletRequest.

huangapple
  • 本文由 发表于 2020年8月21日 05:42:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63513504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定