Search code examples
wso2wso2-api-manager

How to send end user name of api invoker to the backend in wso2 apim


I am testing the custom mediation feature of wso2 apim 4.0.0 publisher portal to send end user name of api invoker to the backend. I followed this reference and created the following custom sequence:

<sequence name="test" xmlns="http://ws.apache.org/ns/synapse"> 
<header name="user" expression="substring-before(get-property('api.ut.userId'), '@')" scope="transport"/>
</sequence> 

Added this sequence as a custom request mediation of my test api. The api itself nothing but simple rest api that returns static string, besides it prints all headers to console:

@RestController
@RequestMapping("greet")
public class GreetApi {

    @GetMapping
    public String greet(@RequestHeader Map<String,String> headers) {
        System.out.println(headers);
        return "Greetings";
    }
}

But it's not printing the end user name as a custom header in console:

{activityid=3221a82e-0679-4f6e-9a36-bd9abdefb6a4, accept=*/*, host=localhost:8080, user=, connection=Keep-Alive, user-agent=Synapse-PT-HttpComponents-NIO}

Any navigation to address this issue is highly appreciated


Solution

  • Another approach is to pass the JWT token coming from the client to the back and decode it and get the username. For this add the following to the deployment.toml

    [apim.oauth_config]
    enable_outbound_auth_header = true