Installation

Configuration

You can configure in Config.groovy or application.yml 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

Usage

You have two ways to work with cookies:

The cookie plug-in extends the request and response objects found in controllers, filters, etc to allow the following.

The cookie plug-in provides a CookieService that can be used anywhere in your Grails application.

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)

Default Secure

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

Default HTTP Only param that denies accessing to JavaScript's document.cookie.

v0.60 Last release with deprecated taglib and methods in service

#17 Since v0.5 few things was deprecated and will be removed in version v1.0:
* Tag <cookie:get/>. Use standard <g:cookie/> tag instead.
* Methods get(), set(), delete() from CookieService. They are replaced with corresponding getCookie(), setCookie(), deleteCookie().

v0.3

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.