Skip to content

Should CharacterEncodingFilter apply to all assets? #5459

@codefromthecrypt

Description

@codefromthecrypt

CharacterEncodingFilter is auto-configured such that it appends a charset to all resources. The trouble is that this adds charset to resources such as png files. This causes confusion for people debugging their apps.

I've overridden it like below, but wondering if there's a smarter way to address this.

  /**
   * This opts out of adding charset to png resources.
   *
   * <p>By default, {@linkplain CharacterEncodingFilter} adds a charset qualifier to all resources,
   * which helps, as javascript assets include extended character sets. However, the filter also
   * adds charset to well-known binary ones like png. This creates confusing content types, such as
   * "image/png;charset=UTF-8".
   */
  @Bean
  @Order(Ordered.HIGHEST_PRECEDENCE)
  public CharacterEncodingFilter characterEncodingFilter() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter() {
      @Override
      protected boolean shouldNotFilter(HttpServletRequest request) {
        return request.getServletPath().endsWith(".png");
      }
    };
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    return filter;
  }

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions