2017-10-05 90 views
0

在我的Spring集成項目(使用基本授權的Spring Security API)中,我嘗試訪問Principal對象以讀取用戶名。在Spring集成中提取主體用戶HTTP入站適配器

這是入站網關的結構:,我不 Spring Integration and http inbound adapter, how do I extract a principal user?

儘管成功驗證:

<int-http:inbound-gateway request-channel="gatewayRequests" 
 
\t \t reply-channel="gatewayResponses" 
 
\t \t supported-methods="POST" 
 
\t \t path="/api/v1/myservice" 
 
\t \t request-payload-type="java.lang.String" reply-timeout="100"> 
 

 
\t \t <int-http:request-mapping consumes="application/xml" produces="application/xml"/> 
 
\t \t <int-http:header name="principal" expression="T(org.springframework.security.core.context.SecurityContextHolder).context.authentication.principal"/> 
 
\t </int-http:inbound-gateway>

我從答覆在此上述表達式t看到委託人 - 我的語法是否正確,期望表達式的結果映射到消息頭?

而不是映射到頭,如果我要使用以下內容,我如何訪問代碼層中的主體值(假設它被添加到有效內容中)?

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

誰能好心幫我嗎?

此致 巴拉斯

回答

1

已經有像頭:

.setHeader(org.springframework.integration.http.HttpHeaders.USER_PRINCIPAL, 
         servletRequest.getUserPrincipal()) 
發送到 gatewayRequests消息中

爲什麼這不適合你?

OTOH該表達式也必須工作。如果你沒有那個,那麼你不能認爲你的身份驗證是正確的......

+0

在ServiceActivator內部偵聽「gatewayRequests」,我嘗試了2種方法:方法1:身份驗證身份驗證= SecurityContextHolder.getContext( ).getAuthentication(); String callerUserName = authentication.getName();方法2:Principal principal =(Principal)incomingMessage.getHeaders()。get(「USER_PRINCIPAL」); String callerUserName = principal.getName();其中,方法1有效,但方法2沒有。這意味着用戶已通過身份驗證,但不確定USER_PRINCIPAL爲什麼不包含任何內容。 – bmylavar

+0

這不是'USER_PRINCIPAL'文字。這是'.HttpHeaders.USER_PRINCIPAL'常量。那麼,你絕對可以檢查你留言中的所有標題。只需登錄它,例如 –

+0

完美!根據您的建議,我記錄了所有標題,並看到值「http_userPrincipal」可用,並且這也是HttpHeaders.USER_PRINCIPAL常量的值。 – bmylavar

相關問題