SpringBoot之路SpringBoot极简教程 · Spring Boot Spring Security

Springboot的swagger页面中加入JWT token

2017-12-02  本文已影响206人  褪色的记忆1994

创建一个swagger.class文件

@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.test.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Lists.newArrayList(apiKey()));
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title(" RestApi Document")
                .description("decription for api")
                .termsOfServiceUrl("www.test.com")
                .contact("lytech")
                .version("1.0")
                .build();
    }

    private ApiKey apiKey() {
        return new ApiKey("apikey", "Authorization", "header");
    }
}

访问swagger页面 http://address/swagger-ui.html

屏幕快照 2017-12-02 下午12.49.24.jpg

然后点击右上方Authorize按钮


屏幕快照 2017-12-02 下午12.50.09.jpg

然后出现如上图的弹窗,在value中填入JWT token,点击Authorize就在jwt token加入到所有请求的header中了。

个人博客:https://blog.xvjialing.xyz

github主页:https://github.com/xvjialing

微信公众号

微信公众号
上一篇 下一篇

猜你喜欢

热点阅读