You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot下OpenAPI Java客户端转发用户Token的最佳实践

Spring Boot中使用openapi-generator-maven-plugin生成客户端并转发用户Token的最佳实践

我想了解在Spring Boot应用中,使用openapi-generator-maven-plugin生成客户端的最佳实践——该应用需要通过这个客户端与外部API通信,同时转发用户的Token。

我不确定怎么用现有的@Autowired功能,因为它似乎默认Bearer Token只配置一次(比如用client credentials流程的场景)。

曾看到帖子建议给端点添加安全方案,让客户端新增请求参数,但实际没改变客户端的行为。

还有文章建议修改RestTemplate来添加Bearer Token,但这不太理想,因为会影响全局的RestTemplate。

我是不是必须放弃@Autowired,为每个用户请求都构建客户端来转发Token?

我的openapi-generator-maven-plugin配置如下:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${openapi-generator.version}</version>
    <executions>
        <execution>
            <id>Generate server</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/petclinic.yml</inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>com.example.petclinic.api</apiPackage>
                <modelPackage>com.example.petclinic.model</modelPackage>
                <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
                <configOptions>
                    <delegatePattern>true</delegatePattern>
                    <useSpringBoot3>true</useSpringBoot3>
                </configOptions>
                <configHelp>false</configHelp>
            </configuration>
        </execution>
        <execution>
            <id>Generate client</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/petclinic.yml</inputSpec>
                <generatorName>java</generatorName>
                <apiPackage>com.example.petclinic.client</apiPackage>
                <modelPackage>com.example.petclinic.model</modelPackage>
                <generateApiTests>false</generateApiTests>
                <generateModelTests>false</generateModelTests>
                <library>resttemplate</library>
                <configOptions>
                    <useJakartaEe>true</useJakartaEe>
                    <generateClientAsBean>false</generateClientAsBean>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

内容的提问来源于Stack Exchange,提问作者Heuriskos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 15:42:40