简体   繁体   English

如何在 Spring MVC 中获取 HttpServletRequest?

[英]How do I get the HttpServletRequest in Spring MVC?

How do I get the HttpServletRequest in Spring MVC?如何在 Spring MVC 中获取 HttpServletRequest?

When trying to get the HttpServletRequest, I get an exception.尝试获取 HttpServletRequest 时,出现异常。

Message No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.消息No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

Please tell me how to solve the problem?请告诉我如何解决这个问题?

    @Component
    public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {


        @Autowired
        private HttpServletRequest request;


        @Override
        public void onApplicationEvent(AuthenticationSuccessEvent a) {
            
            System.out.println(request.getRemoteAddr());
            
        }

    }

You shouldn't autowire a HttpServletRequest in your aspect as this will tie your aspect to be only runnable for classes that are called from within an executing HttpServletRequest.您不应该在您的切面中自动装配 HttpServletRequest,因为这会将您的切面绑定为只能对从正在执行的 HttpServletRequest 中调用的类运行。

Instead use the RequestContextHolder to get the request when you need one.而是在需要时使用 RequestContextHolder 来获取请求。

 private String getRemoteAddress() { RequestAttributes attribs = RequestContextHolder.getRequestAttributes(); if (attribs instanceof NativeWebRequest) { HttpServletRequest request = (HttpServletRequest) ((NativeWebRequest) attribs).getNativeRequest(); return request.getRemoteAddr(); } return null; }

https://stackoverflow.com/a/24029989/11985558 https://stackoverflow.com/a/24029989/11985558

Can you try this你能试试这个

RequestAttributes reqAtt = RequestContextHolder.getRequestAttributes();
if (RequestContextHolder.getRequestAttributes() != null) {
    HttpServletRequest req = ((ServletRequestAttributes) reqAtt).getRequest();
    return req.getRemoteAddr();
}

also u need to add/register a RequestContextListener listener in web.xml file.您还需要在 web.xml 文件中添加/注册 RequestContextListener 侦听器。

<web-app ...>
   <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
   </listener>
</web-app>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 spring bean 中获取 HttpServletRequest? - How do I get a HttpServletRequest in my spring beans? 如何像使用HttpServletRequest一样从Spring MVC中的org.springframework.web.context.request.WebRequest中获取requestUrl? - How can I get the requestUrl from org.springframework.web.context.request.WebRequest in Spring MVC like we do using HttpServletRequest? 如何在 Springboot 应用程序中获取 HTTPServletRequest - How do I get the HTTPServletRequest in Springboot application 使用spring mvc从HttpServletRequest获取数据 - Get data from HttpServletRequest with spring mvc Spring:如何将HttpServletRequest注入请求范围的bean? - Spring: how do I inject an HttpServletRequest into a request-scoped bean? 如何使用Spring MVC Jackson拦截HttpServletRequest - How to intercept HttpServletRequest with Spring MVC Jackson Spring MVC如何使HttpServletRequest字段具有线程安全性? - How Spring MVC make HttpServletRequest field threadsafe? Spring MVC中的WebRequest和HttpServletRequest - WebRequest and HttpServletRequest in Spring MVC 如何在加载JSP页面时获取HttpServletRequest对象? - How do I get a HttpServletRequest object when loading a JSP page? spring如何在SOAP拦截器中获取HttpServletRequest和HttpServletResponse - How to get HttpServletRequest and HttpServletResponse in SOAP interceptor in spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM