即使状态为200

时间:2017-02-21 14:40:02

标签: java spring google-chrome

我的应用程序尝试多次重新加载相同的资源,但资源仍然正确加载且状态为200.应用程序正在尝试加载 http://localhost:8080/scripts/app.js?_=1487686739222 http://localhost:8080/scripts/vendor.js?_=1487686739293 直到浏览器没有因错误而停止。我尝试过禁用缓存,但它仍然会发生。当我尝试手动加载该资源时,资源正确加载。我无法理解为什么浏览器会尝试多次重新加载相同的资源。我正在使用Spring并使用这种配置来加载 index.html

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private String apiPath;

    private final ResourceProperties resourceProperties;

    @Autowired
    public WebMvcConfig(ResourceProperties resourceProperties) {
        this.resourceProperties = resourceProperties;
    }

    public WebMvcConfig(String apiPath, ResourceProperties resourceProperties) {
        this(resourceProperties);
        this.apiPath = apiPath;
    }

    private String getApiPath() {
        return apiPath;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/app/**", "/pages/**", "/components/**", "/sample", "/")
            .addResourceLocations(resourceProperties.getStaticLocations())
            .setCachePeriod(resourceProperties.getCachePeriod()).resourceChain(true)
            .addResolver(new SinglePageAppResourceResolver());
    }

    private class SinglePageAppResourceResolver extends PathResourceResolver {

        @Override
        protected Resource getResource(String resourcePath, Resource location) throws IOException {
            Resource resource = location.createRelative(resourcePath);
            if (resource.exists() && resource.isReadable()) {
                return resource;
            } else if (getApiPath() != null && ("/" + resourcePath).startsWith(getApiPath())) {
                return null;
            } else {
                resource = location.createRelative("index.html");
                return (resource.exists() && resource.isReadable()) ? resource : null;
            }
        }

    }

}

这是我的安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
            .disable()
            .authorizeRequests()
            .antMatchers("/api/public/**", "/app/**", "/pages/**", "/components/**", "/sample", "/apple-icon.png", "/favicon.ico", "/index.html", "/", "/scripts/**", "/styles/**")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .loginPage("/pages/auth")
            .loginProcessingUrl("/api/public/auth")
            .permitAll()
            .and()
            .logout()
            .permitAll()
            .and()
            .exceptionHandling()
            .authenticationEntryPoint(new Http403ForbiddenEntryPoint());
    }

}

这是Chrome中请求的常规数据:

Request URL:http://localhost:8080/scripts/app.js?_=1487687587847
Request Method:GET
Status Code:200 

此处还有控制台输出:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
Tried to load angular more than once.

0 个答案:

没有答案