2016-09-01 5 views
0

私はSpring 4.2.6.RELEASEとSpring Security 4.xを使用しています。 WebRequestやその他のSpringオブジェクトをREST API呼び出しのメソッドに渡すことなく、Spring REST Controller内のUser(Principal)オブジェクトへのハンドルを取得できるかどうかを知りたいと思います。たとえばgetBanks (WebRequest pWebRequest)APIメソッド呼び出しに渡すことなく、Spring REST Controllerのユーザー/プリンシパル/認証へのハンドルを取得する方法は?

現在、メソッドに渡されたWebRequestを使用して、Springでユーザーの詳細を取得しています。これは特に、SWAGGERを使用してYAMLファイルを生成する際に、私のRESTドキュメント定義に影響を与えています。

プリンシパルまたはユーザーへのハンドルをREST APIメソッド呼び出しに渡したり、APIのセキュリティを保持したりせずに、ハンドルを挿入または取得することは可能でしょうか。

@RestController 
@RequestMapping(value = "/banks", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) 
public class BankResource extends Resource { 

@RequestMapping(value = "/banks", method = RequestMethod.GET) 
public ResponseEntity<List<Banks>> getBanks(WebRequest pWebRequest) { 

Authentication authentication = (Authentication) pWebRequest.getUserPrincipal(); 
Object principal = pAuthentication != null ? pAuthentication.getPrincipal() : null; 
User user = (User) principal; 
BankCriteria criteria = new BankCriteria(); 
List<Bank> banks = _bankService.getBanks(user, criteria); 
return ResponseEntity.ok(banks); 
} 
+1

【使い方の可能性のある重複をSpring Bootで現在ログインしているユーザーを見つけますか?](http://stackoverflow.com/questions/31159075/how-to-find-out-the-currently-logged-in-user-in-spring-boot) –

答えて

0

あなたは、パラメータとして認証を含めることができます。

public ResponseEntity<List<Banks>> getBanks(Authentication auth) { 
... 
} 

それとも、コントローラのメソッド内の静的メソッドの呼び出しによってそれを得ることができます。

Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
関連する問題