无法通过附加凭据从Spring获取HttpServletRequest

时间:2012-03-29 12:23:28

标签: java spring servlets httprequest

我正在尝试从Web应用程序中获取Spring中的请求对象,但无论采用什么方法,我都会丢失用户名(/ credentials)。

我使用过@Autowired和

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

接近,但请求看起来像Spring已删除所有凭据信息。

我如何取回这些?我需要一个完整的请求对象来创建另一个库所需的凭证对象。

由于

1 个答案:

答案 0 :(得分:1)

您可以将HttpServletRequest注入控制器。 如果您需要获取用户名,也可以注入java.security.Principal

例如:

@RequestMapping(value = "/path", method = RequestMethod.POST)
public String path(Principal principal, HttpServletRequest request) {
   // do ur thing
   // return username
   principal.getName();
   return "path";
}