在swagger调用中不发送授权头

x33g5p2x  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(390)

我正在使用以下spring引导依赖项。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

    </dependencies>

我为rest端点配置了如下的授权头。

@RestController
@Api(tags = "Welcome Controller", description = "Welcome API")
public class HomeController {

    @ApiOperation(value = "Sent secret message", notes = "Sent secret message")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "Access Token", required = false, allowEmptyValue = false, paramType = "header", dataTypeClass = String.class, example = "Bearer access_token") })
    @GetMapping("/secret-message")
    public Object tokenResponse() {

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        return principal;
    }

}

但是当我打开swagger并尝试用bearer令牌执行rest端点时,这个authorization头并没有作为请求的一部分发送。我们如何解决这个问题?
参考文献https://github.com/tiangolo/fastapi/issues/1037httpshttp://github.com/tiangolo/fastapi/issues/612

w41d8nur

w41d8nur1#

这个问题与最新版本的swagger有关。但在swagger2.9.2中一切都很顺利

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

相关问题