小编pra*_*eep的帖子

在自定义AuthenticationProvider中访问HttpServletRequest

如何在自定义AuthenticationProvider中访问HttpServletRequest.我试过这样做

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
String username = (String) httpReq.getAttribute("j_username");
Run Code Online (Sandbox Code Playgroud)

要么

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest httpReq = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();

String username = (String) httpReq.getAttribute("j_username");
Run Code Online (Sandbox Code Playgroud)

我得到的用户名为null

但是RequestContextHolder.getRequestAttributes(); 返回null.

我想引用requestcontext,传入它,或让Spring做它的魔术,所以我可以引用它.

我也在我的web.xml中提供RequestContextListener

  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
Run Code Online (Sandbox Code Playgroud)

我搜索过安全论坛,但还没找到任何东西.

提前致谢.

spring-mvc spring-security

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

spring-mvc ×1

spring-security ×1