JBoss 5.1 - how to send HTTP responses in UTF-8? - utf-8

We're migrating from JBOSS 4.x to 5.1, and having problems with the character encodings.
Certain characters in the extended ASCII range were O.K. under the previous JBoss version, but with the new JBoss they cause problems (e.g., incomplete http responses).
The solution seems to be to use UTF-8, but the only way I've found to cause JBoss to send charset=UTF-8 in the Content-Type header is to specify this in the page directive of every JSP page. Otherwise the charset in the http response is specified as ISO-8859-1. I'd like to find a global solution to set the charset to UTF-8.
I've seen several other questions about character encoding with JBoss, but none seem to address the encoding of http responses.
I have tried without success:
in jboss/bin/run.bat, setting set "JAVA_OPTS=-Dfile.encoding=utf-8 %JAVA_OPTS%"
in jboss/server//deploy/jbossweb.sar/server.xml setting

I have used spring encoding filter to set the encoding:
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
If you dont use spring, you will need a servlet filter that sets the encoding.

Related

Filter mapping in TomCat for unicode prevents localhost from getting resolved

I have a webApp that is suppose to display unicode characters, which is not working with the default tomcat settings. The unicode characters are displayed as "??????". After looking into it I found a solution where adding an encodingFilter to web.xml fixes the problem.
But it creates another problem where the default tomcat landing page "localhost:8080" and all other services like "localhost:8080/manager" fail to resolve. I get a HTTP Status 404 – Not Found. Although by webapp is working properly "localhost:8080/myWebapp/servletName", URL with my webapp is resolved properly and the unicode characters are also displayed properly.
The filter that I added to web.xml is as follows
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I am pretty new to this so there must some very basic things that I don't understand. But any pointers would help.

What is the purpose of Spring CharacterEncodingFilter

I'm working on an existing project in the configuration file I found this filter that I didn't understand the the purpose of this filter Spring CharacterEncodingFilter I read some documentation but still i didn't understand how it's work :
<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>
Any help Thank you
I think documentation is pretty clear: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CharacterEncodingFilter.html
"Servlet Filter that allows one to specify a character encoding for requests. This is useful because current browsers typically do not set a character encoding even if specified in the HTML page or form."

Endless redirect loop after ticket validation

I'm using the Liferay Portal 6.1.1 CE GA2.
After hours of research I got the following things to work:
Import from a LDAP-Server and custom mapping of an attribute to an usergroup.
Redirect to a specific page after login (based on the usergroup).
Authentication via CAS. This means getting a ServiceTicket and logging in the corresponding user.
Now I'm trying to obtain ProxyTickets so I can proxy to other applications behind the same CAS-Server.
I'm not really getting any error, but Mozilla gives me a redirection error, e.g. the page is redirected in such a way that it can never be loaded.
I googled a lot and tried different approaches but nothing helped.
My web.xml is configured as follows (I snipped out urls. If they're important I can hand them in later):
<filter>
<filter-name>CAS Authentication Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>* snip */cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>* snip *</param-value>
</init-param>
</filter>
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>* snip */cas/</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>* snip *</param-value>
</init-param>
<init-param>
<param-name>proxyCallbackUrl</param-name>
<param-value>https://* snip */pgtCallback</param-value>
</init-param>
<init-param>
<param-name>proxyReceptorUrl</param-name>
<param-value>/pgtCallback</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Authentication Filter</filter-name>
<url-pattern>/c/portal/login</url-pattern>
</filter-mapping>
I tried various combinations of the filter-mappings but nothing helped.
The output of the console in Eclipse hints that multiple consecutive requests are done. Each gets me a TGT, PGTIOU and PGT but after the ST is validated a new validation request is fired. This goes until Mozilla ends the redirect loop.
I also tried specifying service instead of serverName but all remains the same.
Setting the param redirectAfterValidation to false but then I get a MalformedURLException.
Hopefully I didn't forget any information, please help me.
Thanks in advance.

Spring security and special characters

I need to log in with j_spring_security_check using special characters in the username and/or in the password via url
http://localhost:8080/appname/j_spring_security_check?j_username=username&j_password=üüü
isn't working and
http://localhost:8080/appname/j_spring_security_check?j_username=username&j_password=%c3%bc%c3%bc%c3%bc
(with "üüü" urlencoded)
isn't working either
Any suggestion? Let me know if you need to see any other configuration.
Thanks
The Java Servlet standard is lamentably poor at supporting Unicode. The default of ISO-8859-1 is useless and there is still no cross-container-compatible means of configuring it to something else.
The filter method in matteosilv's answer works for request bodies. For parameters in the URL, you have to use container-specific options. For example in Tomcat, set URIEncoding on the <Connector> in server.xml; in Glassfish it's <parameter-encoding> in glassfish-web.xml.
(If you have to work in a fully cross-container-compatible manner you end up having to write your own implementation of getParameter(), which is sad indeed. Bad Servlet.)
However in any case it is a bad idea to pass login form fields in GET URL parameters.
This is firstly because a login causes a state-change to occur, so it is not "idempotent". This makes GET an unsuitable method and causes a load of practical problems like potentially logging you in when you navigate a page, or failing to log you in due to caching, and so on.
Secondly there are a range of ways URLs can 'leak', including referrer tracking, logging, proxies and browser history retention. Consequently you should never put any sensitive data such as a password in a URL, including in GET form submissions.
I'd suggest using a POST form submission instead, together with the CharacterEncodingFilter.
Maybe an encodingFilter in the web.xml file could be helpful:
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
source: Spring security: Form login special characters
The issue was actually solved for me by moving the CharacterEncodingFilter ABOVE the SpringSecurityFilterChain in web.xml.

How to make Spring MVC controller work with UTF-8?

I am using jQuery AJAX to submit a form to a Spring MVC controller in the backed. I am setting encoding on top of the jsp. In my request headers in Firebug I see -
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
Content-Type application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With XMLHttpRequest
However in my Spring MVC controller all the form values entered in Cyrillic turn into junk. And a twist to this is that this works fine in Safari but not in IE/FF/Chrome.
Any thoughts as to how I can set the correct encoding and prevent junk chars from getting submitted?
I found the solution to this problem. I had set the encoding on top of each jsp page. Yet it was not working. So I added a spring character encoding filter in the web.xml. This will ensure that the encoding is correctly in the request.
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Resources