WARNING! Project moved https://github.com/Grails-Plugin-Consortium/grails-cookie
This plugin makes dealing with cookies easy. Provides an injectable service and tag to easily get, set, and delete cookies with one line.
It's RFC 6265 compliant.
To install the cookie plug-in just add to BuildConfig.groovy
:
compile ':cookie:1.4'
You can configure in Config.groovy
how long the default cookie age will be (in seconds) when not explicitly supplied while setting a cookie.
grails.plugins.cookie.cookieage.default = 86400 // if not specified default in code is 30 days
You have two ways to work with cookies:
request
and response
objects found in controllers, filters, etc to allow the following.Example of setting a new cookie:
// This sets a cookie with the name `username` to the value `cookieUser123` with a expiration set to a week, defined in seconds
response.setCookie('username', 'cookieUser123', 604800)
// will use default age from Config (or 30 days if not defined)
response.setCookie('username', 'cookieUser123')
// using service
def cookieService // define field for DI
...
cookieService.setCookie('username', 'cookieUser123', 604800)
To get the cookie value:
request.getCookie('username') // returns 'cookieUser123'
// using service
def cookieService // define field for DI
...
cookieService.getCookie('username') // returns 'cookieUser123'
To delete the cookie (actually it set new expired cookie with same name):
response.deleteCookie('username') // deletes the 'username' cookie
// using service
def cookieService // define field for DI
...
cookieService.deleteCookie('username')
All this methods has other signatures and you can find all of them in CookieService JavaDoc's.
You can check out Demo project and also you can find details of implementation in CookieRequestSpec and CookieResponseSpec.
You can configure default values of attributes in Config.groovy
.
Default expiration age for cookie in seconds. Max-Age
attribute, integer.
If it has value -1
cookie will not stored and removed after browser close.
If it has null value or unset, will be used 30 days, i.e. 2592000
seconds.
Can't has value 0
, because it means that cookie should be removed.
grails.plugins.cookie.cookieage.default = 360 * 24 * 60 * 60
Default path for cookie selection strategy, string.
grails.app.context
option in Config.groovy
If default path is null or unset, it will be used 'context' strategy
grails.plugins.cookie.path.defaultStrategy = 'context'
Default secure cookie param. Secure cookie available only for HTTPS connections. Secure
attribute, boolean.
If default secure is null or unset, it will set all new cookies as secure if current connection is secure
grails.plugins.cookie.secure.default = null
Default HTTP Only param that denies accessing to JavaScript's document.cookie
.
If null or unset will be true
grails.plugins.cookie.httpOnly.default = true
You can find details of implementation in CookieServiceDefaultsSpec.
If you use property files to inject values to plugins at runtime. This is now supported as of version 1.0.2. This means that inside your external foo.properties
file you can specify the following.
grails.plugins.cookie.httpOnly.default=true
The string value will correctly be treated as a boolean.
path.defaultStrategy
optiongrails.plugins.cookie.path.defaultStrategy
doesn't work../grailsw test-app
that uses wrapper with Grails 2.4instanceof Boolean
to toBoolean()
which will correctly address boolean false
and string "false"
propertiesgrails.app.context
used null instead of actual context<cookie:get/>
. Use standard <g:cookie/>
tag instead.get()
, set()
, delete()
from CookieService
. They are replaced with corresponding getCookie()
, setCookie()
, deleteCookie()
.setCookie()
and deleteCookie()
should return added cookiepath
, secure
and httpOnly
configurable and more intelligent enhancementdeleteCookie
not works in response
<cookie:get/>
. Use standard <g:cookie/>
tag instead.get()
, set()
, delete()
from CookieService
. They are replaced with corresponding getCookie()
, setCookie()
, deleteCookie()
.In the v0.3 release a big issue was fixed that now sets the cookie's path to the root /
context.
Otherwise it was setting the path to the same as the controller/service that triggered it.
Most users I believe will want this behavior. If setting the path is desired, that can be accommodated.
Please contact me or do a pull request if you'd like that.