Set and read cookies in Spring MVC / Security

I am new to Spring and I need to set a custom cookie when the β€œLogin” button is clicked and then this cookie will be red inside the webapp. What is the best way to do this? Also, this begs the question, how to read it later on every page in webapp?

I thought I could set cookies using JavaScript and later read it through a special filter (which will read the cookie with the request, set it to an attribute and send it to the controller.

Is this a correct thought? Or should I set a cookie somewhere else (if so, where and why?)

UPDATE 1:

What I want to achieve: I have a drop-down list (which is a language selector) on the login page that has some values ​​(language code, for example, "en"), and the selected value should be set as a cookie (for example, "lang") ) and that the "lang" cookie will be red for i18n later on the pages. I made i18n to work, but I need to read the "lang" cookie to set the selected language.

UPDATE 2:

I did what I wanted, but this is not entirely clean:

I set a cookie using Javascript or jQuery, and if the user selects or changes the selection in <select/>, then Javascript enters the language value as a cookie (for example, en):

HTML:

<select name="language" id="selectLanguage" class="form-control">
   <option val="en">English</option>
</select>

JS:

var cookie = {

    set: function($this) {
        var now = new Date();
        var time = now.getTime();
        var expireTime = time + 1000*36000;
        now.setTime(expireTime);
        document.cookie = 'lang=' + $this.val() +';expires='+now.toGMTString()+';path=/';
    }

}

$('#selectLanguage').change(function(event) {
    cookie.set($(this));
});

Then I created a new Filer , which I called CookieFilter.java :

public class CookieFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;

        Cookie[] cookies = ((HttpServletRequest) req).getCookies();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                if(ck.getName().toString().equals("lang")){
                    req.setAttribute("languageCookie", ck.getValue());
                } else {
                    req.setAttribute("languageCookie", "en");
                };
            }
            chain.doFilter(req, res);
        }
    }

    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }
    public void destroy() {
        // TODO Auto-generated method stub
    }
}

web.xml:

<filter>
    <filter-name>CookieFilter</filter-name>
    <filter-class>
        package.path.to.CookieFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>CookieFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

, CookieFilter , :

String cookie = request.getAttribute("languageCookie").toString();
model.addAttribute("languageCookie",cookie);

model .JSP , .

, ...:)

+4
1

, cookie , ? i18n Spring MVC Interceptors. , . , .

0

All Articles