Java 如何在 Spring bean 中获取 HttpServletRequest?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/559155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a HttpServletRequest in my spring beans?
提问by Jason Maskell
I'm developing an app with a Flex-based front end and a Spring/Hibernate back-end.
我正在开发具有基于 Flex 的前端和 Spring/Hibernate 后端的应用程序。
To get Facebook integration working in the way I've got it currently, I need to read the cookies set in javascript on the front end on the back-end and do some validation during login to see whether the user is attempting to spoof his Facebook login.
为了让 Facebook 集成以我目前的方式工作,我需要在后端的前端读取 javascript 中设置的 cookie,并在登录期间进行一些验证,以查看用户是否试图欺骗他的 Facebook登录。
This would be pretty easy, but I can't figure out how to get the HttpServletRequest. I'm using a pretty basic Spring config (this is my first real Spring app, and I'm pretty familiar with it now, but there's lots to it I've never looked at.)
这很容易,但我不知道如何获取 HttpServletRequest。我正在使用一个非常基本的 Spring 配置(这是我的第一个真正的 Spring 应用程序,我现在对它非常熟悉,但我从未看过它的很多内容。)
I'm not using Spring MVC or Spring WebFlow or anything like that. I can get the ServletContext, but I haven't yet figured out how to get the request.
我没有使用 Spring MVC 或 Spring WebFlow 或类似的东西。我可以获取到 ServletContext,但是我还没有弄清楚如何获取请求。
Any help?
有什么帮助吗?
采纳答案by Gerrit Brehmer
If FlexContext is not available:
如果 FlexContext 不可用:
Solution 1: inside method (>= Spring 2.0 required)
解决方案1:内部方法(需要>= Spring 2.0)
HttpServletRequest request =
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes())
.getRequest();
Solution 2: inside bean (supported by >= 2.5, Spring 3.0 for singelton beans required!)
解决方案2:inside bean(需要>= 2.5,Spring 3.0支持singelton bean!)
@Autowired
private HttpServletRequest request;
回答by Jason Maskell
This is kind of Flex/BlazeDS specific, but here's the solution I've come up with. Sorry if answering my own question is a faux pas.
这是 Flex/BlazeDS 特有的,但这是我想出的解决方案。对不起,如果回答我自己的问题是失礼的话。
HttpServletRequest request = flex.messaging.FlexContext.getHttpRequest();
Cookie[] cookies = request.getCookies();
for (Cookie c:cookies)
{
log.debug(String.format("Cookie: %s, %s, domain: %s",c.getName(), c.getValue(),c.getDomain()));
}
It works, I get the cookies. My problem was looking to Spring - BlazeDS had it. Spring probably does too, but I still don't know how to get to it.
它有效,我得到了饼干。我的问题是寻找 Spring - BlazeDS 有它。Spring 可能也是如此,但我仍然不知道如何实现它。
回答by eeezyy
The @Context
annotation (see answers in this question :What does context annotation do in Spring?) will cause it to be injected for you.
该@Context
注释(见回答这样一个问题:什么情况下做注解春?)会导致它被注入了你。
I had to use
我不得不使用
@Context
private HttpServletRequest request;
回答by chrismacp
@eeezyy's answer didn't work for me, although I'm using Spring Boot (2.0.4) and it may differ, but a variation here in 2018 works thus:
@eeezyy 的回答对我不起作用,虽然我使用的是 Spring Boot (2.0.4) 并且它可能有所不同,但 2018 年的一个变体因此有效:
@Autowired
private HttpServletRequest request;
回答by Luiz Fernando Fran?a
this should do it
这应该这样做
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getRequestURI();