首页 > 解决方案 > Springboot:Swagger UI 格式在 AWS 中显示不正确

问题描述

我有一个带有两个控制器 echo-controller 和 trac-controller 的简单 Spring Boot 应用程序。我想为这些服务端点提供一个 api 文档。我正在尝试使用带有 Spring Boot 版本的swagger ui 2.6.1 : 1.5.2.RELEASE

pom.xml文件中,我添加了这些依赖项

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId> 
            <version>4.0.1.RELEASE</version>        
    </dependency>
     <dependency>
            <groupId>org.springframework.security</groupId>
             <artifactId>spring-security-web</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>

Below is my echo controller

        @Controller
    public class EchoController {   

        @ApiOperation(value="Test Echo Service",response=ResponseEntity.class)
        @ApiResponses(value={
           @ApiResponse(code=200,message="Test Echo Service Retrieved",response=ResponseEntity.class),
           @ApiResponse(code=500,message="Internal Server Error"),
           @ApiResponse(code=404,message="Test Echo Service not found")
        })
        @RequestMapping(value = "/echo/{any}", method = RequestMethod.GET)
        @ResponseBody
        public String echo(@PathVariable("any") String any) {
            return any;
        }
    }

    similar annotation i have for trac-contoller endpoints as well.

    Below is my swagger config file

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig{ 

        @Bean
        public Docket produceApi(){
        return new Docket(DocumentationType.SWAGGER_2)    
        .apiInfo(apiInfo())    
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.demo.applications.trac.controller"))
        .paths(PathSelectors.any())       
        .build();
    }
    // Describe  apis
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
        .title("TRAC MicroService Rest APIs")
        .description("This page lists all the rest apis for TRAC MicroService.")
        .version("1.0-SNAPSHOT")    
        .build();
    }
    }

    Below is a Security config

    @Configuration
    class SecurityConfig extends WebSecurityConfigurerAdapter {

        private static final String[] AUTH_WHITELIST = {

                // -- swagger ui
                "/swagger-resources/**",
                "/swagger-ui.html",
                "/v2/api-docs",
                "/configuration/ui",
                "/swagger-resources/configuration/ui",
                "/swagger-resources/configuration/security",
                "/configuration/security",
                "/webjars/**",
                "/swagger.json",
                "/csrf",
                "/webjars/springfox-swagger-ui",
                "/webjars/springfox-swagger-ui/css",
                "/webjars/springfox-swagger-ui/images",
                "/echo/**",            
                "/trac-service/**"

        };

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()       
                    .antMatchers(AUTH_WHITELIST).permitAll()
                    .antMatchers("/**/*").denyAll();
        }
        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**","/webjars/springfox-swagger-ui/css","/webjars/springfox-swagger-ui/images");
        }

    }

Swagger ui 在本地 (http) 中正确显示

但是当我在 AWS (https) 中部署它时,html 格式就消失了。我尝试了很多方法,但无法弄清楚为什么 html 格式在 AWS 中消失了。

在此处输入图像描述

标签: amazon-web-servicesspring-bootswagger

解决方案


从屏幕截图看来,您的 CSS / JS 没有加载。请检查您的浏览器日志以查看用于发出请求的 URL 并设置 httpSecurity 以允许加载这些资源,类似于您现有的安全角色。

还要确保以下事项。

1.swagger 配置如下。

@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/v2/api-docs", "/swagger-resources/**", "/webjars/**", "/swagger-ui.html")
            .permitAll();
    }
}

2.尝试清理 Chrome Web 浏览器缓存。


推荐阅读