2016-11-07 107 views
0

我正在开发一个订购Web应用程序,我是学习Grails的新手。我使用了Spring Security插件。我已经配置并覆盖登录和注册,我想要的是让当前登录的用户申请一个订单。有没有办法做到这一点,而不使用静态belongsTo?因为使用它总是会显示一个下拉框,我可以在用户和管理员之间进行选择。我是否正确地让登录用户订购?获取已登录用户的Grails弹簧安全性

回答

-1

检索当前认证委托人的最简单的方法是通过一个静态调用SecurityContextHolder中:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
if (!(authentication instanceof AnonymousAuthenticationToken)) { 
    String currentUserName = authentication.getName(); 
    return currentUserName; 
} 

OR

进样SpringSecurityService豆, 这样到控制器

def springSecurityService 

然后致电

def currentPrincipal = springSecurityService.principal 

print "The details of current logged in user is" + currentPrincipal 

欲了解更多详情请点击此链接 http://www.baeldung.com/get-user-in-spring-security

How to get the current logged in user object from spring security?

+4

与插件的最简单方法是依赖项注入'SpringSecurityService'豆,例如'def springSecurityService'并调用'def currentPrincipal = springSecurityService.principal' –