2012-07-12 52 views
0

這裏有一個控制器我試圖取代:Spring集成和http入站適配器,如何提取主體用戶?

@RequestMapping("/user") 
public @ResponseBody UserRsp callUserService(Principal principal) { 
    String authenticatedUsername = principal.getName(); 
    return userService.getUser(authenticatedUsername); 
} 

我想用一個int-HTTP來替代它:入站網關,被映射到一個服務適配器。我無法找到任何這文件,但我想要做的是這樣的:

<int-http:inbound-gateway 
request-channel="requests" 
reply-channel="replies" 
path="/user" 
view-name="/user" 
payload-expression="#payload.principal.name" 
supported-methods="GET"/> 

但payload.principal是我做的。任何想法正確的方式來做到這一點?

回答

1

假設你正在使用Spring Security的,這樣的事情應該工作...

payload-expression="T(org.springframework.security.core.context.SecurityContextHolder).context.authentication.principal" 
+0

好極了!我現在在SPeL文檔中看到了這個功能,這是有道理的。事實證明,返回org.springframework.security.core.userdetails.User對象而不是java.security.Principal,但我可以調用.username並獲得相同的結果。 – Joe 2012-07-13 11:19:32

+0

順便說,主要的(如果存在的話),始終映射到一個頭... \t消息消息= messageBuilder \t \t .setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_URL,請求。 getURI()。的toString()) \t \t .setHeader(org.springframework.integration.http.HttpHeaders.REQUEST_METHOD,request.getMethod()。的toString()) \t \t .setHeader(org.springframework.integration.http。 HttpHeaders.USER_PRINCIPAL,servletRequest.getUserPrincipal()) \t \t .build(); – 2012-07-13 21:35:39

相關問題