Spring Boot Response-Komprimierung funktioniert nicht
Ich habe eine Javascript-Datei, die ziemlich groß ist, ~ 1MB. Ich versuche, die Antwortkomprimierung mit den folgenden Anwendungseigenschaften in meiner yml-Datei zu aktivieren:
server.compression.enabled: true
server.compression.mime-types: application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css
Aber es funktioniert nicht. Es findet keine Komprimierung statt.
Request-Header:
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
Accept: */*
Accept-Encoding: gzip, deflate, sdch, br
Response headers
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Connection:keep-alive
Content-Length:842821
Content-Type:application/javascript;charset=UTF-8
Die Antwort enthält keinen Header für die Inhaltskodierung.
Ich verwende Spring Boot Version 1.3.5.RELEASE
Was vermisse ich
=== EDIT 4 === Ich wollte eine eigenständige App erstellen, um weiter zu untersuchen, warum die Eigenschaften der Inhaltskomprimierung nicht funktionieren. Aber plötzlich fing es an zu funktionieren und ich habe nichts in Bezug auf die Konfiguration geändert, nicht die Änderung der POM-Datei, nicht die Änderung der application.yml-Datei. Also ich weiß nicht, was sich geändert hat, dass es funktioniert hat ...
=== EDIT 3 === Follow @ Chimmi Vorschläge weiter. Ich habe an den vorgeschlagenen Stellen Haltepunkte gesetzt. Es sieht so aus, als ob Anforderungen an statische Ressourcen (js-Dateien) an diesen Haltepunkten niemals gestoppt wurden. Nur Rest-API-Anforderungen tun dies. Und für diese Anforderung war die Inhaltslänge aus irgendeinem Grund Null, wodurch die Inhaltskomprimierung übersprungen wird.
=== EDIT 2 === Ich habe einen Haltepunkt in der Zeile 180 von o.s.b.a.w.ServerProperties gesetzt, dank des Vorschlags von @ chimmi, und es zeigt, dass alle Eigenschaften gesetzt sind, aber irgendwie beachtet der Server die Einstellung nicht ...:
=== EDIT 1 ===
Ich bin mir nicht sicher, ob es wichtig ist, aber ich füge meinen Haupt- und Konfigurationscode für die Anwendung hier ein:
Application.java:
@SpringBootApplication
public class TuangouApplication extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(TuangouApplication.class, args);
}
// this is for WAR file deployment
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TuangouApplication.class);
}
@Bean
public javax.validation.Validator localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
}
Aufbau
@Configuration
public class TuangouConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**").permitAll()
.and().antMatcher("/**").authorizeRequests().antMatchers("/api/**").permitAll()
.and().exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
.and().formLogin().loginPage("/login").failureUrl("/login?error").permitAll()
.and().logout().logoutSuccessUrl("/").permitAll()
.and().csrf().csrfTokenRepository(csrfTokenRepository())
.and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
.headers().defaultsDisabled().cacheControl();
// @formatter:on
}
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true)
protected static class AuthenticationSecurity extends GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
}
@Bean
public UserDetailsService userDetailsService() {
return new DatabaseUserServiceDetails();
}
}
private Filter csrfHeaderFilter() {
return new OncePerRequestFilter() {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request
.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null
|| token != null && !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setPath("/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
};
}
private CsrfTokenRepository csrfTokenRepository() {
HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
repository.setHeaderName("X-XSRF-TOKEN");
return repository;
}
}
Ressourcenserverkonfiguration:
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter{
@Autowired
private TokenStore tokenStore;
@Override
public void configure(ResourceServerSecurityConfigurer resources)
throws Exception {
resources.tokenStore(tokenStore);
}
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.antMatcher("/**").authorizeRequests().antMatchers("/api/**").permitAll();
// @formatter:on
}
}