我需要在有人登录时设置一个 cookie,并且出现的 JSP 页面需要能够在 Javascript 中读取该 cookie 并基于它执行操作。我坚持的部分是在登录时设置 cookie。我正在使用 Spring 3.0 和 Spring Security 3.0。
我已经注册了一个 LoginListener:
public class LoginListener implements ApplicationListener {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof AuthenticationSuccessEvent) {
somehowGetResponseObject.addCookie(new Cookie(name, value));
}
}
}
理想情况下,我还可以获取请求对象,以便我可以找到 contextPath 并将其设置为 cookie 路径。我可以将 cookie 添加到 ThreadLocal 对象,然后稍后在我有权访问响应对象时读取它,但是稍后我可以访问该对象吗?我不太热衷于将 cookie 添加到 session 中,然后在生成的 JSP 页面中读取它,但如果必须的话,我会这样做。我可以访问 HttpSession 吗?
更新 :指定身份验证成功处理程序引用解决了这个问题:http://static.springsource.org/spring-security/site/docs/3.0.x/reference/core-web-filters.html#form-login-flow-handling
最佳答案
完全不同的东西,为什么不把那个值当作一个 JS 变量来打印呢?
<script>
var something = '${somebean.someproperty}';
if (something) {
performActionWith(something);
}
</script>
关于session - 在 Spring Security 3.0 中登录时设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4306815/