如何将API调用者的最终用户名称发送到WSO2 API管理器后端。

huangapple go评论46阅读模式
英文:

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

问题

我正在测试WSO2 APIM 4.0.0发布者门户的自定义调解功能,以将API调用者的端用户名称发送到后端。我遵循了此参考链接 并创建了以下自定义序列:

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

将此序列添加为我的测试API的自定义请求调解。该API本身只是一个简单的REST API,返回静态字符串,另外它将所有标头打印到控制台:

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

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

但它没有将端用户名称作为自定义标头打印到控制台:

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

非常感谢任何关于解决此问题的指导。

英文:

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:

&lt;sequence name=&quot;test&quot; xmlns=&quot;http://ws.apache.org/ns/synapse&quot;&gt; 
&lt;header name=&quot;user&quot; expression=&quot;substring-before(get-property(&#39;api.ut.userId&#39;), &#39;@&#39;)&quot; scope=&quot;transport&quot;/&gt;
&lt;/sequence&gt; 

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(&quot;greet&quot;)
public class GreetApi {

    @GetMapping
    public String greet(@RequestHeader Map&lt;String,String&gt; headers) {
        System.out.println(headers);
        return &quot;Greetings&quot;;
    }
}

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

答案1

得分: 0

另一种方法是将来自客户端的JWT令牌传递到后端并解码它,然后获取用户名。为此,请将以下内容添加到deployment.toml文件中:

[apim.oauth_config]
enable_outbound_auth_header = true
英文:

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

huangapple
  • 本文由 发表于 2023年7月18日 13:24:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定