Skip to content

Commit

Permalink
Merge pull request #5 from cloudogu/bugfix/add_encoding_filter
Browse files Browse the repository at this point in the history
add encoding-filter for confluence >= 8.0
  • Loading branch information
meiserloh authored Jan 3, 2025
2 parents f5a60ea + e4cd345 commit eb5b42c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.jasig.cas.client.integration.atlassian;

import org.apache.commons.lang.StringUtils;
import org.jasig.cas.client.configuration.ConfigurationKeys;
import org.jasig.cas.client.util.AbstractConfigurationFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
* Confluence80EncodingFilter sets the character-encoding of the request to the configured encoding (default: "UTF-8").
* This filter needs to be added before the other CAS-Filters because they will access request-parameters.
* Accessing request-parameters for post-requests without the proper encoding will also affect all subsequent filters (e.g. Confluence)
* because the request-parameters are cached once they have been read.
*/
public class Confluence80EncodingFilter extends AbstractConfigurationFilter {

private final String UTF8 = java.nio.charset.StandardCharsets.UTF_8.name();

private String encoding = UTF8;

@Override
public void init(final FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
this.encoding = getString(ConfigurationKeys.ENCODING);
if (StringUtils.isEmpty(this.encoding)) {
this.encoding = UTF8;
}
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) servletRequest;
request.setCharacterEncoding(this.encoding);
filterChain.doFilter(request, servletResponse);
}

@Override
public void destroy() {
super.destroy();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.servlet.ServletException;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.integration.atlassian.Confluence80EncodingFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.springframework.core.Ordered;
Expand Down Expand Up @@ -42,12 +43,28 @@ public class Confluence80CasWebApplicationInitializer implements WebApplicationI
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
logger.info("Initializing CAS SSO filters");
// apply the encoding filter first to avoid it not being applied at all due to caching. See {@link Confluence80EncodingFilter}
initEncodingFilter(servletContext);
initSingleSignOutFilter(servletContext);
initAuthenticationFilter(servletContext);
initValidationFilter(servletContext);
logger.info("End of initializing CAS SSO filters");
}

private void initEncodingFilter(ServletContext servletContext) {
FilterRegistration casEncodingFilterRegistration = servletContext.getFilterRegistration("CasEncodingFilter");
if (casEncodingFilterRegistration == null) {
casEncodingFilterRegistration = servletContext.addFilter("CasEncodingFilter", Confluence80EncodingFilter.class);
}
logger.info("Registering CAS encoding filter");

casEncodingFilterRegistration.addMappingForUrlPatterns(
EnumSet.allOf(DispatcherType.class),
false,
"/*");
logInitParams(casEncodingFilterRegistration);
}

private void initSingleSignOutFilter(ServletContext servletContext) {
FilterRegistration casSingleSignOutRegistration = servletContext.getFilterRegistration("CasSingleSignOutFilter");
if (casSingleSignOutRegistration == null) {
Expand Down

0 comments on commit eb5b42c

Please sign in to comment.