2016-11-07 5 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' Beanを、例えばあります'def springSecurityService'を呼び出し、' def currentPrincipal = springSecurityService.principal'を呼び出します。 –

関連する問題