Springboot:Swagger UI格式在AWS中无法正确显示

时间:2018-11-09 08:38:53

标签: amazon-web-services spring-boot swagger

我有一个简单的spring boot应用程序,其中包含两个控制器echo-controller和trac-controller。我想要这些服务端点的api文档。我正在尝试使用 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中不可用。

enter image description here

1 个答案:

答案 0 :(得分:0)

从屏幕快照看来,您的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浏览器缓存。