【发布时间】:2020-04-23 03:11:19
【问题描述】:
如何在自定义 AuthenticationProvider 中访问 HttpServletRequest。我试过这样做
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
String username = (String) httpReq.getAttribute("j_username");
或
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest httpReq = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
String username = (String) httpReq.getAttribute("j_username");
我的用户名为空
但RequestContextHolder.getRequestAttributes(); 返回null。
我想引用 requestcontext,将其传入,或者让 Spring 发挥它的魔力,以便我可以引用它。
我还在 web.xml 中提供 RequestContextListener
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
我已经搜索了安全论坛,但仍然没有找到任何东西。
提前致谢。
【问题讨论】:
-
这些凭据不应该包含在 Authentication 对象中吗? static.springsource.org/spring-security/site/docs/3.0.x/apidocs/…
-
Dardo 是正确的。我认为您可能应该查看一些基本示例并阅读一些文档,然后更详细地解释您要实现的目标,因为我想不出尝试以这种方式访问用户名的正当理由。
-
凭据确实在 Authentication 对象中。但是,我也在寻找解决方案。我也需要访问该请求,但出于另一个原因。在我的应用程序中,URL 在身份验证中起作用。关于如何在本课程中获得它的任何想法?
标签: spring-mvc spring-security