在Spring MVC / Security中设置和读取Cookie [英] Set and read cookies in Spring MVC/Security

查看:677
本文介绍了在Spring MVC / Security中设置和读取Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring的新手,我需要设置一个自定义cookie,当登录按钮被点击,然后该cookie将是红色的webapp。最好的做法是什么?此外,这会带来问题,如何阅读它的每一个网页上的webapp?

I am new to Spring and I need to set a custom cookie when "Login" button is clicked, and then that cookie will be red inside the webapp. What is the best practice to do exactly that? Also, this brings question how to read it later on every page in webapp?

我想我可以用JavaScript设置cookie,然后通过自定义过滤器从请求中读取cookie,将其设置为属性并发送到控制器。

I thought I can set cookies with JavaScript, and read it later via custom filter (which would read the cookie from request, set it to the attribute and send it to controller.

这是否正确?或者我应该将Cookie设置在其他地方为什么?)

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

更新1:

我想实现:在登录页面上有具有一些值(语言代码,例如en)的下拉框(其是语言选择器),并且所选择的值需要被设置为cookie(例如lang),并且lang我已经使i18n工作,但我需要读langcookie来设置所选择的语言。

What I want to achieve: I have dropdown box (which is language selector) on login page that has some values (language code, e.g. "en"), and selected value needs to be set as cookie (e.g. "lang") and that "lang" cookie will be red for i18n later on the pages. I have made i18n to work, but I need to read "lang" cookie to set the selected language.

更新2:

我已经做了我想做的事,但它不完全干净:

I have done what I wanted to do, but it is not exactly clean:

我通过Javascript或jQuery设置cookie,当用户选择或更改< select /> 中的选择时,Javascript会将语言值注入cookie en):

I am setting cookie via Javascript, or jQuery to be exact, and when user selects or changes the selection in <select/> then Javascript injects language value as cookie (e.g. 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));
});

然后我创建了新的 Filer ,我叫 CookieFilter.java ::

Then I created new Filer that 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
    }
}


b $ b

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 和红色我发送的属性:

And to finish everything, I got the request from CookieFilter in my controller and red the attribute I sent:

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

现在我可以从 model 中读取属性

Now I can read attribute from model and set it inside .JSP or do everything I want with it.

这是我的解决方案,但必须有其他方法...:)

That is my solution, but there must be other way ... :)

推荐答案

那么,这是使用cookie的要求还是只是你采取的方法?你可以非常干净地处理i18n在Spring MVC使用拦截器。我在此此处。基本上你需要注册一个拦截器,并确定你的浏览器请求。

So, is it a requirement to use a cookie or is that just the approach that you are taking? You can very cleanly handle i18n in Spring MVC using Interceptors. I cover that here. Basically you need to register an interceptor and have that determine your browser requests.

这篇关于在Spring MVC / Security中设置和读取Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆