CodeMed CodeMed - 6 months ago 18
Java Question

What int value gets used for ServletRequestAttributes scope?

How does one know what value to give for the

scope
parameter when requesting an attribute value from a
ServletRequestAttributes
object?

In particular, I would like to get the value of the
_csrf
attribute of a request sent to the Authorization Endpoint of this GitHub example app. I have a
CustomOAuth2RequestFactory
set up to contain the call to the
ServletRequestAttributes.getAttribute("_csrf", scope)
method, but how do I know what value to enter as the
scope
in order to get the correct
_csrf
value?

Here is a link to the API.

Also, I found this link to the Spring documentation for bean scopes, but how should one reference one of the scopes in the method call shown above? Simply typing in the String name of a scope results in a compilation error warning in eclipse for trying to pass a String as an int.

Answer

The term scope used with ServletRequestAttributes is unrelated to bean scopes.

ServletRequestAttributes defines scope as one of request and session

Accesses objects from servlet request and HTTP session scope, with no distinction between "session" and "global session".

It provides access to request and session attributes through these possible scope values. Constants for these are defined in RequestAttributes, SCOPE_REQUEST and SCOPE_SESSION.


Note that the current implementation of ServletRequestAttributes only ever considers two scenarios. Either you provide SCOPE_REQUEST and it attempts to find a request attribute or you provide any other value (not necessarily SCOPE_SESSION), it ignores it, and attempts to find a session attribute.

Other RequestAttributes implementations may use different scope values.

Comments