0

Letters in my header such as á are shown as á.

Apparently the Header comes in iso-8859-1 and my application fails to encode it to utf8.

I am extremely puzzled that none of my approaches work.

What I did so far in Spring Boot:

Attempt 1: set application properties

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

Attempt 2: set filterRegistrationBean

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setEncoding("UTF-8");
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setOrder(1);
        registration.setFilter(characterEncodingFilter);
        return registration;
    }

Attempt 3: add to filter Chain directly

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        filter.setForceEncoding(true);

        http.addFilterBefore(filter, CsrfFilter.class)
                .addFilterBefore(myAuthFilter, AbstractPreAuthenticatedProcessingFilter.class)
                .authorizeRequests()
                .antMatchers("/").permitAll()
                ....

None of this had any affect. I would still store and send the header as iso-8859-1.

Just for testing I changed the setup from UTF-8 to iso-8859-1, and saw the filter indeed have an effect. I would now encode it twice to iso-8859-1.

I simply don't understand why UTF-8 is not working in my case.

Teacups7k
  • 11
  • 2
  • 1
    Are you using the embedded tomcat or are deploying to an external tomcat? The `CharacterEncoding` filter is added by default, adding another one won't change a thing, also adding it to the security filter chain is wrong as it doesn't belong there!. – M. Deinum Oct 06 '22 at 06:20
  • 1
    Also the HTTP spec doesn't really allow UTF8 in headers, some headers have exceptions and you can use a specific encoding to send special chars. See https://www.jmix.io/blog/utf-8-in-http-headers/. – M. Deinum Oct 06 '22 at 06:26
  • Thanks for your response! I work with an external tomcat. For the second remark, yes and I am not entirely sure what to do in this situation:( – Teacups7k Oct 06 '22 at 06:36
  • 1
    You might need to check the tomcat settings then, trying to configure that through Spring won't work. And as stated UTF8 isn't really allowed in headers, only in the body. As mentioned in [this blog](https://www.jmix.io/blog/utf-8-in-http-headers/). – M. Deinum Oct 06 '22 at 06:46
  • See also https://stackoverflow.com/questions/5251824/sending-non-ascii-text-in-http-post-header – dur Oct 06 '22 at 09:17

0 Answers0