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

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

问题描述

我正在开发具有基于 Flex 的前端和 Spring/Hibernate 后端的应用.

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

为了让 Facebook 集成以我目前的方式工作,我需要在后端读取前端的 javascript 中设置的 cookie,并在登录期间进行一些验证以查看用户是否正在尝试欺骗他的 Facebook 登录信息.

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.

这很容易,但我不知道如何获取 HttpServletRequest.我正在使用一个非常基本的 Spring 配置(这是我的第一个真正的 Spring 应用程序,我现在对它非常熟悉,但我从未看过它的很多内容.)

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

我没有使用 Spring MVC 或 Spring WebFlow 或类似的东西.可以获取到ServletContext,但是我还没有想出如何获取请求.

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.

有什么帮助吗?

推荐答案

如果 FlexContext 不可用:

If FlexContext is not available:

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

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

HttpServletRequest request = 
        ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes())
                .getRequest();

解决方案2:inside bean(需要>= 2.5,Spring 3.0支持singelton bean!)

Solution 2: inside bean (supported by >= 2.5, Spring 3.0 for singelton beans required!)

@Autowired
private HttpServletRequest request;

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

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