【问题标题】:What int value gets used for ServletRequestAttributes scope?ServletRequestAttributes 范围使用什么 int 值?
【发布时间】:2016-05-13 23:04:24
【问题描述】:

从ServletRequestAttributes 对象请求属性值时,如何知道为scope 参数提供什么值?

特别是,我想获取发送到this GitHub example app 的授权端点的请求的_csrf 属性的值。我设置了一个CustomOAuth2RequestFactory 以包含对ServletRequestAttributes.getAttribute("_csrf", scope) 方法的调用,但是我如何知道输入什么值作为scope 以获得正确的_csrf 值?

这里是a link to the API。

另外,我找到了this link to the Spring documentation for bean scopes,但是应该如何引用上面显示的方法调用中的范围之一?只需输入作用域的字符串名称,就会在 eclipse 中出现编译错误警告,因为它试图将字符串作为 int 传递。

【问题讨论】:

  • 这些不是 bean 范围。它们是 Servlet 范围(请求/会话)的抽象。常量在RequestAttributes 接口中定义。使用适当的请求或会话。

标签: java spring spring-mvc spring-security spring-boot


【解决方案1】:

与ServletRequestAttributes 一起使用的术语范围 与bean 范围无关。

ServletRequestAttributes 将范围定义为 request 和 session

之一

从 servlet 请求和 HTTP 会话范围访问对象,没有 “会话”和“全局会话”的区别。

它通过这些可能的scope 值提供对请求和会话属性的访问。这些常量在RequestAttributes、SCOPE_REQUEST 和SCOPE_SESSION 中定义。


请注意,ServletRequestAttributes 的当前实现只考虑了两种情况。您提供SCOPE_REQUEST 并尝试查找请求属性,或者您提供任何其他值(不一定是SCOPE_SESSION),它会忽略它并尝试查找会话属性。

其他RequestAttributes 实现可能使用不同的scope 值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 2018-07-26
    • 2012-12-16
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多