如何通过在GSP中使用Spring Security Grails插件来获取current_user [英] How to get current_user by using Spring Security Grails plugin in GSP
问题描述
我想这样...
编辑此帖子
< / g:link>
< / g:if>
这里我想要显示编辑此帖子链接,由signed_in用户创建。但它显示错误 -
错误500:内部服务器错误
URI
/ groovypublish / post / list
类
java.lang.NullPointerException
消息
无法在空对象上获取属性'principal'
这是我的Post.groovy -
class Post {
static hasMany = [评论:评论]
字符串标题
字符串预告片
字符串内容
日期lastUpdated
布尔已发布= false
SortedSet comment
Person author
....... more code ....
这是我的Person.groovy域类文件 -
class Person {
transient springSecurityService
字符串实数名称
字符串用户名
字符串密码
布尔启用
布尔accountExpired
布尔accountLocked
布尔passwordExpired
字节[]头像
字符串头像类型
静态hasM any = [follow:Person,posts:Post]
static searchable = [only:'realName']
........ more code ......
请帮助。
<尝试使用springSecurity插件提供的标签,如:
< sec:isLoggedIn>
编辑此帖子
< / g:link>
< / sec:isLoggedIn>
其实你试图在你的GSP页面上注入一个服务,你可以用一些import语句在页面上,但我会说这不会是很好的编程习惯,我认为你应该将当前登录的用户实例从控制器发送到GSP页面,然后执行检查:
假设你有控制器方法:
def showPostPage(){
Person currentLoggedInUser = springSecurityService.getCurrentUser();
[currentLoggedInUser:currentLoggedInUser]
}
以及您的GSP页面:
< g:if test =$ {post.author == currentLoggedInUser}>
编辑此帖子
< / g:link>
< / g:if>
I am newbie in Grails. I am using Spring Security Grails plugin for Authentication purpose. I want to get current user in my view gsp file.
I am trying like this ...
<g:if test="${post.author == Person.get(springSecurityService.principal.id).id }">
<g:link controller="post" action="edit" id="${post.id}">
Edit this post
</g:link>
</g:if>
Here I want to show Edit this post link to only those posts who created by signed_in user. But It showing ERROR -
Error 500: Internal Server Error
URI
/groovypublish/post/list
Class
java.lang.NullPointerException
Message
Cannot get property 'principal' on null object
Here is my Post.groovy --
class Post {
static hasMany = [comments:Comment]
String title
String teaser
String content
Date lastUpdated
Boolean published = false
SortedSet comments
Person author
....... more code ....
Here is my Person.groovy Domain Class File --
class Person {
transient springSecurityService
String realName
String username
String password
boolean enabled
boolean accountExpired
boolean accountLocked
boolean passwordExpired
byte[] avatar
String avatarType
static hasMany = [followed:Person, posts:Post]
static searchable = [only: 'realName']
........ more code ......
Please help.
Try tags provided by springSecurity plugin, something like:
<sec:isLoggedIn>
<g:link controller="post" action="edit" id="${post.id}">
Edit this post
</g:link>
</sec:isLoggedIn>
Actually you are trying to inject a service on your GSP page, you can do it with some import statement on the page, but I would say it will not be good programming practice, I think you should send current logged In user's instance from the controller to the GSP page, and then perform a check on it:
let say you have the controller method:
def showPostPage(){
Person currentLoggedInUser = springSecurityService.getCurrentUser();
[currentLoggedInUser:currentLoggedInUser]
}
and on your GSP page:
<g:if test="${post.author == currentLoggedInUser }">
<g:link controller="post" action="edit" id="${post.id}">
Edit this post
</g:link>
</g:if>
这篇关于如何通过在GSP中使用Spring Security Grails插件来获取current_user的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!