0
votes

In my Spring Integration project (with Spring Security API using Basic Authorization), I am trying to access the Principal object in order to read the Username.

This is the structure of the inbound gateway:

<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>

I got the aforementioned expression from the reply in this: Spring Integration and http inbound adapter, how do I extract a principal user?

Despite successful authentication, I don't see the principal - is my syntax correct in expecting the result of the expression to be mapped to a message header?

Instead of the mapping to the header, if I were to use the following, how do I access the Principal value in the code layer (assuming it gets added into the payload)?

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

Can anyone kindly help me?

Sincerely, Bharath

1

1 Answers

1
votes

There is already a header like:

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

in the message sent to the gatewayRequests.

Why that doesn't work for you?

OTOH that expression must work too. If you don't have that one, then you can't assume that you are authenticated correctly...