r0d*_*aes 6 grails session-management shiro
目前,我在控制器和gsp页面中使用默认的HttpSession对象:
在控制器中:
...
session.mykey = anObject; // adding an object to session
...
if (session.otherkey) { // performing some checking
Run Code Online (Sandbox Code Playgroud)
在GSP中:
...
<g:if test="${session.mykey}">
...
Run Code Online (Sandbox Code Playgroud)
我想要一个"记住我"的功能.Shiro已经内置了.但是,据我所知,为了做到这一点,我必须使用shiro本地会话模式(在Config.groovy:security.shiro.session.mode ="native").默认情况下,它会持久保存会话状态,因此只要Cookie过期或用户注销,对象就会保留在会话中.
我的理解是对的吗?
然后我将不得不将我的控制器更改为:
def shiroSession = SecurityUtils.subject.session
shiroSession.setAttribute("mykey",anObject)
....
if (shiroSession.getAttribute("otherkey") ){
Run Code Online (Sandbox Code Playgroud)
我对此的看法:
<g:if test="${SecurityUtils.subject.session.getAttribute('mykey')}">
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:
我放弃了在会话中持久保留对象(直到 cookie 过期)。这是我所做的:
在控制器的登录方法中:
if (! session.currentProfile){
Subject currentUser = SecurityUtils.getSubject()
if (currentUser.isRemembered()){
boolean success = configureSession(session, currentUser.getPrincipal())
if (success){
...
}
}
....
}
Run Code Online (Sandbox Code Playgroud)
第一个“if”检查会话是否有我需要的对象。
configureSession 方法将我需要的所有信息放入会话中。