-1

System deployed in elasticbeanstalks+github+tomcat+AWS I've tried the Gzip compression using ebextensions files but it not works.

option_settings:
  aws:elasticbeanstalk:environment:proxy:
    GzipCompression: 'true'
    ProxyServer: nginx

and

files:
  /etc/nginx/conf.d/gzip.conf:
    content: |
      gzip_types application/json application/xml text/html text/xml text/plain application/javascript text/css;

(similar configs tried with .ebextensions/tomcat-settings.config as well, but not works)

So moved to Gzip compression using code level. So added dependencies on pom and create a bean in the Application class

@Bean
public Filter compressingFilter() {
CompressingFilter compressingFilter = new CompressingFilter();
return compressingFilter;
}

<dependency>
    <groupId>net.sourceforge.pjl-comp-filter</groupId>
    <artifactId>pjl-comp-filter</artifactId>
    <version>1.6.4</version>
</dependency>

Then Gzip compression works fine in each and every API response. But it causes a negative impact on the web app because the same app provides web-view too, (Spring Boot + Spring Security + Spring MVC and deployed as .war file). As a result of GZIP now css js not loading and page not appear as expected under SSL. but its http one works fine but getting more time to load css/js files.

So I've tried removing the below entries in the application properties file to avoid any double compression in there.

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css

and tried adding some filter in code level

@Bean
public FilterRegistrationBean filterRegistrationBean () {
    CompressingFilter compressingFilter = new CompressingFilter();
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(compressingFilter);
    Map<String, String> initParams = new HashMap();
    initParams.put("exclusions", "*.js,*.css");
    registrationBean.setInitParameters(initParams);
    return registrationBean;
}

also tried with web-security ignore filtering

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/css/**", "/js/**");
}

Therefore let me know any workaround or some code level filtering to avoid this issue.

enter image description here

Error Stack

s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
o.s.s.w.a.AnonymousAuthenticationFilter  : Set SecurityContextHolder to anonymous SecurityContext
o.s.s.w.a.i.FilterSecurityInterceptor    : Authorized filter invocation [GET /myapp/css/bootstrap.min.css] with attributes [permitAll]
o.s.security.web.FilterChainProxy        : Secured GET /myapp/css/bootstrap.min.css
o.s.web.servlet.DispatcherServlet        : GET "/myapp/css/bootstrap.min.css", parameters={}
.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler@7534e708
o.s.security.web.FilterChainProxy        : Securing GET /myapp/js/bootstrap.min.js
s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
Karan Sindhu
  • 5
  • 1
  • 3
  • I would try with just `server.compression.enabled` and `server.compression.mime-types`. And without `pjl-comp-filter` and without `filterRegistrationBean`. – indybee Feb 04 '22 at 12:20
  • thanks @indybee, but it not works as expected – Karan Sindhu Feb 04 '22 at 12:36
  • with which of the above options are you getting 404 for the js/css? can you confirm the js/css paths are correct? – indybee Feb 08 '22 at 00:49

1 Answers1

0

try server.compression.enabled=true and setting server.compression.mime-types

with the specific mime-types to be compressed, instead of removing it entirely

indybee
  • 1,507
  • 13
  • 17
  • thanks @indybee, but it not works as expected. thatswhy tried to nginx-compression, tomncat-compression, and finally pjl-comp-filter. using the filter it works but issue is css and js not working with https, even http very slow to load page – Karan Sindhu Feb 04 '22 at 12:38
  • did you keep the security `configure(WebSecurity)` method with your test using `server.compression.mime-types`? I know that is needed for caching static resources, I don't know if also needed for compression. – indybee Feb 04 '22 at 12:50
  • Yes, I tried both ways – Karan Sindhu Feb 07 '22 at 08:20
  • @KaranSindhu I am not sure. Does the browser show any error when trying to decompress the css/js? I tried those properties with springboot 2.5 & and it worked out of the box. I do see this though for springboot 1.5 - if you are on that version - https://stackoverflow.com/questions/62626807/gzip-compression-not-working-in-my-project-with-spring-boot-1-5-10-release – indybee Feb 07 '22 at 12:41
  • thanks & added image for your convenience – Karan Sindhu Feb 07 '22 at 13:09
  • what does it show as reason for `(failed)` when you click on the css/js? – indybee Feb 07 '22 at 13:16
  • its 404, updated in question now – Karan Sindhu Feb 07 '22 at 13:35