Sum*_*ppi 9 session grails grails-controller grails-2.0
我是grails的新手.我必须与会议合作.我见过会话文档.但不知道将代码放在我的控制器中的哪个位置.我有一个学生创建名称createStudent的页面.现在我希望只有在用户进入会话时才能访问此页面.现在我该怎么办呢.我是否必须在登录时将用户设置为变量.有人可以帮我这个吗?
def index() {
def user = session["user"]
if (user){
redirect(controller: 'admistratorAction', action: 'createUser')
}else{
redirect(controller: 'login', action: 'index')
}
}
Run Code Online (Sandbox Code Playgroud)
osb*_*orp 14
您可以使用控制器内的session.getAttribute(key)和session.setAttribute(key, value)方法.或者,有一些插件,如Spring Security Core Plugin,已经很好地处理了这个问题.
有一个很好的教程由彼得·莱德布鲁克为Spring安全插件,这里和插件文档链接到至少一个其他教程.
**编辑**
如您所建议的,为了直接使用会话,需要在较早的时间点在会话中设置用户.例如:
def setCurrentStudent() {
def aStudent = [name: "Student1"]
session["user"] = aStudent
render "Added $aStudent to the session."
}
Run Code Online (Sandbox Code Playgroud)
Spring Security将在登录时自动执行此操作.然后,可以使用springSecurityService随时访问当前用户.
class SomeController {
def springSecurityService
def someAction = {
def user = springSecurityService.currentUser
…
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17710 次 |
| 最近记录: |