Wednesday, December 14, 2011

Spring saved my day on an UTF-8 encoding problem

I'm working on Jasig CAS (an SSO server) and I had an issue with special chars. For example, it was impossible to log in with a password like &é"'(-.
  • I checked my jsp file :it was encoded in UTF-8
  • I checked the encoding directive (<%@ page contentType="text/html; charset=UTF-8" %>) : OK
The idea was to force the request encoding with something like :

request.setCharacterEncoding("UTF-8");

But I was in Spring MVC and I didn't want an ugly hack. Fortunately, Spring saved my day with a builtin servlet filter to declare in the web.xml.

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

3 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Its important to say that this filter must come before other filters (took me some time to figure this out) https://wiki.jasig.org/display/CASC/Configuring+Single+Sign+Out

simashree said...

Thanks for the great information in your blog Selenium Training in Chennai