【问题标题】:Extracting a Principal User in Spring Integration HTTP Inbound adapter在 Spring Integration HTTP Inbound 适配器中提取主要用户
【发布时间】:2017-10-05 21:19:51
【问题描述】:

在我的 Spring Integration 项目中(使用基本授权的 Spring Security API),我试图访问 Principal 对象以读取用户名。

这是入站网关的结构:

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

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

我从这个回复中得到了上述表达: Spring Integration and http inbound adapter, how do I extract a principal user?

尽管身份验证成功,但我看不到主体 - 我的语法是否正确,期望表达式的结果映射到消息头?

如果我要使用以下内容,而不是映射到标头,我如何访问代码层中的 Principal 值(假设它被添加到有效负载中)?

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

谁能帮帮我?

真诚地, 巴拉特

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    已经有这样的标题:

    .setHeader(org.springframework.integration.http.HttpHeaders.USER_PRINCIPAL,
                            servletRequest.getUserPrincipal())
    

    在发送到gatewayRequests的消息中。

    为什么这对你不起作用?

    OTOH,该表达式也必须有效。如果你没有那个,那么你就不能假设你的身份验证是正确的......

    【讨论】:

    • 在监听“gatewayRequests”的ServiceActivator中,我尝试了2种方法: 方法1:Authentication authentication = SecurityContextHolder.getContext().getAuthentication();字符串 callerUserName = authentication.getName();方法二:Principal principal = (Principal) incomingMessage.getHeaders().get("USER_PRINCIPAL");字符串 callerUserName = principal.getName();其中,方法 1 有效,但方法 2 无效。这意味着用户已通过身份验证,但不确定为什么 USER_PRINCIPAL 不包含任何内容。
    • 这不是 USER_PRINCIPAL 文字。那是.HttpHeaders.USER_PRINCIPAL 常量。好吧,您绝对可以检查消息中的所有标题。只需记录它,例如
    • 完美!根据您的建议,我记录了所有标头,发现值“http_userPrincipal”可用,这也是 HttpHeaders.USER_PRINCIPAL 常量的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    相关资源
    最近更新 更多