Spring swagger2:如何设置HTTPS协议?

rekjcdws  于 2024-01-05  发布在  Spring
关注(0)|答案(2)|浏览(230)

当我在swagger中执行我的spring app时,默认协议是http,我想在https中执行它,因为应用程序在HTTPS中运行。我如何配置它?
这是我的代码:

  1. @Configuration
  2. @EnableSwagger2
  3. public class SwaggerConfig{
  4. @Bean
  5. public Docket api() {
  6. return new Docket(DocumentationType.SWAGGER_2)
  7. .apiInfo(metaData())
  8. .select()
  9. .apis(RequestHandlerSelectors.basePackage("com.adp.mnc.metadata.controller"))
  10. .paths(PathSelectors.any())
  11. .build();
  12. }

字符串

xxls0lw8

xxls0lw81#

可以使用.protocols(Sets.newHashSet("http", "https"))

guykilcj

guykilcj2#

@varman解决方案的完整代码:

  1. public class SwaggerConfig extends WebMvcConfigurationSupport {
  2. @Bean
  3. public Docket api() {
  4. return new Docket(DocumentationType.SWAGGER_2)
  5. // https://github.com/springfox/springfox/issues/1139
  6. .ignoredParameterTypes(AuthenticationPrincipal.class)
  7. .host("javaops-demo.ru")
  8. .protocols(Set.of("https"))
  9. .select()
  10. .paths(ant("/rest/**"))
  11. .build()
  12. .apiInfo(new ApiInfoBuilder()
  13. .title("REST API documentation")
  14. .description("""
  15. Application (<a href='https://javaops.ru/view/topjava'>TopJava</a>)
  16. <p><b>Test credential:</b><br>
  17. - [email protected] / password<br>
  18. - [email protected] / admin<br>
  19. - [email protected] / guest</p>
  20. """)
  21. .version("1.0")
  22. .build())
  23. .securitySchemes(List.of(new BasicAuth("basicAuth")));
  24. }
  25. }

字符串

展开查看全部

相关问题