Skip to content

Adds cookie based RequestCache #8653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2020

Conversation

zeeshanadnan
Copy link
Contributor

fixes gh-8034

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 4, 2020
@eleftherias eleftherias self-assigned this Jun 4, 2020
@eleftherias eleftherias added in: web An issue in web modules (web, webmvc) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 4, 2020
Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @zeeshanadnan!
I have left some feedback inline.

*
* @param cookieName is the new name of the cookie.
*/
public void setCookieName(String cookieName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to make the cookie name configurable.
We can always add this functionality later if a user requests it.
Similarly, I don't think we need setCookieMaxAge and setCookiePath for now.



/**
* {@code RequestCache} which stores the {@code SavedRequest} in a cookie.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should let users know that this cache will not store the complete request, but just the request URI.
Unlike HttpSessionRequestCache, it does not store headers etc.

@Override
public SavedRequest getRequest(HttpServletRequest request, HttpServletResponse response) {
Cookie savedRequestCookie = WebUtils.getCookie(request, cookieName);
return savedRequestCookie != null ? new SimpleSavedRequest(savedRequestCookie.getValue()) : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use DefaultSavedRequest instead of SimpleSavedRequest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eleftherias i did not find a suitable constructor to do it in an easy way. Also in the getMatchingRequest method its always going to return null. Is it going to make a difference or i am missing something? Any suggestion on a better approach will be very helpful. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zeeshanadnan It can use the builder,

new DefaultSavedRequest.Builder().setRequestURI(savedRequestCookie.getValue()).build()

Copy link
Contributor

@eleftherias eleftherias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @zeeshanadnan!
I have left some more feedback inline.

request.setServerName("abc.com");
request.setRequestURI("/destination");

String encodedRedirectUrl = Base64.getEncoder().encodeToString("http://abc.com/destination".getBytes());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "https://" here, or a relative URL. We avoid using "http://" in our code and use the nohttp library to find occurrences. You can read more about it here https://github.com/spring-io/nohttp.

public void matchingRequestMethodSetsAnExpiredCookieOnRequestWithSavedRequestCookie() {
CookieRequestCache cookieRequestCache = new CookieRequestCache();
MockHttpServletRequest request = new MockHttpServletRequest();
String redirectUrl = "http://abc.com/destination?param1=a&param2=b&param3=1122";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use "https://" here, or a relative path. We avoid using "http://" in our code and use the nohttp library to find occurrences. You can read more about it here https://github.com/spring-io/nohttp.

private RequestMatcher requestMatcher = AnyRequestMatcher.INSTANCE;
protected final Log logger = LogFactory.getLog(this.getClass());

private static final String COOKIE_NAME = "saved_request";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to be consistent with the server implementation, the cookie should be named "REDIRECT_URI".

protected final Log logger = LogFactory.getLog(this.getClass());

private static final String COOKIE_NAME = "saved_request";
private static final String COOKIE_PATH = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the path is hardcoded to null?
I would expect it to change based on the request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially my thought was user should be able to control the cookie path so i included a property and setter method. But yes you are right the default value should be request's context path.

private static final String DEFAULT_COOKIE_NAME = "saved_request";

@Test
public void originalRequestSetsSavedRequestInACookieOnResponse() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an effort to have test names be consistent across the codebase, we would like all new tests to follow the naming convention methodWhenConditionThenResult.
For example, this test could be renamed to something like saveRequestWhenMatchesThenSavedRequestInACookieOnResponse.

@zeeshanadnan
Copy link
Contributor Author

Sorry @eleftherias for the late updates.

@eleftherias eleftherias merged commit 9708a2d into spring-projects:master Jul 2, 2020
@eleftherias eleftherias added this to the 5.4.0-RC1 milestone Jul 2, 2020
@eleftherias eleftherias added the status: duplicate A duplicate of another issue label Jul 2, 2020
@eleftherias
Copy link
Contributor

Thanks for the PR @zeeshanadnan!
This is now merged into master.

@zeeshanadnan zeeshanadnan deleted the gh-8034 branch July 6, 2020 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web An issue in web modules (web, webmvc) status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add request cache that uses cookie
3 participants