1. Which Spring annotation is used to read the value of an HTTP cookie into a handler method parameter?
Why:@CookieValue maps a handler method parameter to the value of an HTTP cookie. The other annotations read headers, query parameters and URI path values.
2. Where can the @CookieValue annotation be used?
Why:@CookieValue targets method parameters only. It tells Spring to fill that parameter with a cookie value.
3. In @CookieValue, which attribute is an alias of the value attribute?
Why:value and name are aliases. Both set the name of the cookie to bind to. The name attribute is available since Spring 4.2.
4. A handler method uses @CookieValue with only the cookie name. The cookie is missing from the request. What happens by default?
Why: The required attribute is true by default. If the cookie is missing, the request fails, unless you set a default value or set required to false.
5. Which @CookieValue attribute gives a fallback value to use when the cookie is not found?
Why:defaultValue is the fallback value. When it is set, a missing cookie does not cause an error.
6. What does Spring do when the parameter marked with @CookieValue is not a String, for example an int?
Why: Like other Spring MVC parameter binding, @CookieValue uses automatic type conversion. The cookie value is converted to the type of the parameter.
7. Which object do you use to send a cookie to the browser with the standard Servlet API?
Why: Cookies are written to the response. You create a Cookie object and add it with HttpServletResponse#addCookie.
8. When you set the max age of a Servlet Cookie with setMaxAge, in which unit is the value given?
Why: The max age of a Servlet Cookie is given in seconds. For example, 24 * 60 * 60 seconds is one day.
9. Which method of HttpServletRequest returns the cookies sent by the browser?
Why:HttpServletRequest#getCookies() returns an array of all cookies sent with the request.
10. What does request.getCookies() return when the request has no cookies?
Why: It can return null when there are no cookies. This is why you should check for null before looping through the cookies.
11. What is the WebUtils#getCookie(request, name) helper method used for?
Why:org.springframework.web.util.WebUtils#getCookie is a static helper. It finds a specific cookie by name, so you do not need to loop through all cookies yourself.
12. Which Spring class (available since Spring 5) lets you build a cookie with a fluent API to write it to the response?
Why:ResponseCookie has a builder-style API. You can set the value, max age, path and security attributes in a readable way.
13. How do you send a ResponseCookie to the browser?
Why: A cookie is sent to the browser in the Set-Cookie response header. You add the ResponseCookie string value to this header.
14. Besides @CookieValue, how else can a handler method read cookie values?
Why: You can use HttpServletRequest as a handler parameter. Then you can loop through the cookies or read the one you need.
15. What does the HttpOnly cookie attribute do?
Why:HttpOnly hides the cookie from JavaScript. This helps protect session and login cookies from XSS-based hijacking.
16. What does the Secure cookie attribute do?
Why: With Secure, the browser sends the cookie only over HTTPS. Without it, the cookie can be sent in plain text over HTTP and may be intercepted.
17. What does the SameSite cookie attribute control?
Why:SameSite controls if the cookie is sent when the request comes from another site. It helps protect against CSRF attacks.
18. What does SameSite=Strict mean?
Why: With Strict, the cookie is never sent on cross-site requests, even when the user clicks a link from another site. This gives the strongest CSRF protection, but it can break OAuth and SSO redirect flows.
19. What does SameSite=Lax mean?
Why:Lax allows the cookie on top-level navigations such as link clicks. It blocks the cookie on sub-resource requests from other sites. It is a safe default for most applications.
20. Which extra attribute must be set when you use SameSite=None?
Why:SameSite=None sends the cookie on all cross-site requests. Browsers reject such a cookie unless Secure is also set.