我如何在Spring bean中获得一个HttpServletRequest? [英] How do I get a HttpServletRequest in my spring beans?

查看:446
本文介绍了我如何在Spring bean中获得一个HttpServletRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于Flex的前端和Spring / Hibernate后端的应用程序。



为了让Facebook整合工作,目前,我需要阅读后端前端的JavaScript中设置的cookies,并在登录过程中进行一些验证,看看用户是否试图欺骗他的Facebook登录。



这很简单,但是我不知道如何获得HttpServletRequest。我正在使用一个相当基本的Spring配置(这是我第一个真正的Spring应用程序,现在我很熟悉它,但是我从来没有看过很多)。



我没有使用Spring MVC或Spring WebFlow或任何类似的东西。我可以得到ServletContext,但是我还没有弄清楚如何获取请求。



任何帮助?

解决方案

如果FlexContext不可用:



解决方案1:内部方法(> =需要Spring 2.0)

  HttpServletRequest request =((ServletRequestAttributes)RequestContextHolder 

.getRequestAttributes())。getRequest();

解决方案2:内部bean(可能需要Spring 3.0 for singelton beans!)

  @Autowired 

私人HttpServletRequest请求;


I'm developing an app with a Flex-based front end and a Spring/Hibernate back-end.

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.

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.)

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.

Any help?

解决方案

If FlexContext is not available:

Solution 1: inside method (>= Spring 2.0 required)

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder

        .getRequestAttributes()).getRequest();

Solution 2: inside bean (probably Spring 3.0 for singelton beans required!)

@Autowired

private HttpServletRequest request;

这篇关于我如何在Spring bean中获得一个HttpServletRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆