如何在 Grails Web 应用程序中使用 shiro 本机会话?
目前,我在控制器和 gsp 页面中使用默认的 HttpSession 对象:
在控制器中:
...
session.mykey = anObject; // adding an object to session
...
if (session.otherkey) { // performing some checking
在 GSP 中:
...
<g:if test="${session.mykey}">
...
我希望有一个“记住我”功能。 Shiro 已经内置了它。但是,据我了解,为了做到这一点,我必须使用 shiro 本机会话模式(在 Config.groovy 中:security.shiro.session.mode="native")。默认情况下,它会保留会话状态,因此只要 cookie 过期或用户注销,对象就会保留在会话中。
我的理解对吗?
然后我将不得不将我的控制器更改为:
def shiroSession = SecurityUtils.subject.session
shiroSession.setAttribute("mykey",anObject)
....
if (shiroSession.getAttribute("otherkey") ){
我对此的看法:
<g:if test="${SecurityUtils.subject.session.getAttribute('mykey')}">
所以,我的问题是:
- 这是正确的吗?
- 我不能直接使用以前的方式访问会话吗?
- 我是否必须在某些配置中关闭默认的 http 会话?
Currently, I am using the default HttpSession object in both controllers and gsp pages:
In controllers:
...
session.mykey = anObject; // adding an object to session
...
if (session.otherkey) { // performing some checking
In GSPs:
...
<g:if test="${session.mykey}">
...
I'd like to have a "remember me" functionality. Shiro has already it built in. However, as far as I understood, in order to do it I have to use the shiro native session mode (in Config.groovy: security.shiro.session.mode="native"). By default, it persists the session state, so objects will remain in the session as far as the cookie expires or the user logs off.
Is my understanding right?
Then i will have to change my controllers to this:
def shiroSession = SecurityUtils.subject.session
shiroSession.setAttribute("mykey",anObject)
....
if (shiroSession.getAttribute("otherkey") ){
And my views to this:
<g:if test="${SecurityUtils.subject.session.getAttribute('mykey')}">
So, my questions are:
- Is that right?
- Can't I just use the previous way to access the session?
- Do I have to turn off the default http session in some configuration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我放弃了在会话中持久保留对象(直到 cookie 过期)。这就是我所做的:
在控制器的登录方法中:
第一个“if”检查会话是否具有我需要的对象。
configureSession 方法将我需要的所有信息放入会话中。
I gave up keeping objects in the session persistently (until cookie expires). Here is what i did:
In the login method in the controller:
The first "if" checks whether the session has the object i need.
The configureSession method puts in the session all information I need.