mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Auto configure CharacterEncodingFilter
Provide a default UTF-8 encoding for HTTP requests and responses unless specified otherwise. Fixes gh-1182
This commit is contained in:
parent
6eab4628f4
commit
109c3a3439
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.filter.CharacterEncodingFilter;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for configuring the
|
||||
* encoding to use in web applications.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(HttpEncodingProperties.class)
|
||||
@ConditionalOnClass(CharacterEncodingFilter.class)
|
||||
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
|
||||
public class HttpEncodingAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private HttpEncodingProperties httpEncodingProperties;
|
||||
|
||||
@ConditionalOnMissingBean
|
||||
@Bean
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public CharacterEncodingFilter characterEncodingFilter() {
|
||||
CharacterEncodingFilter filter = new CharacterEncodingFilter();
|
||||
filter.setEncoding(httpEncodingProperties.getCharset().name());
|
||||
filter.setForceEncoding(httpEncodingProperties.isForce());
|
||||
return filter;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for http encoding.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.http.encoding")
|
||||
public class HttpEncodingProperties {
|
||||
|
||||
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
/**
|
||||
* The Charset to use.
|
||||
*/
|
||||
private Charset charset = DEFAULT_CHARSET;
|
||||
|
||||
/**
|
||||
* To force the encoding to the configured charset typically on HTTP requests and responses.
|
||||
*/
|
||||
private boolean force = true;
|
||||
|
||||
public Charset getCharset() {
|
||||
return charset;
|
||||
}
|
||||
|
||||
public void setCharset(Charset charset) {
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
public boolean isForce() {
|
||||
return force;
|
||||
}
|
||||
|
||||
public void setForce(boolean force) {
|
||||
this.force = force;
|
||||
}
|
||||
|
||||
}
|
@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@ -55,7 +54,6 @@ import com.google.gson.Gson;
|
||||
@ConditionalOnClass(HttpMessageConverter.class)
|
||||
public class HttpMessageConvertersAutoConfiguration {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
@Autowired(required = false)
|
||||
private final List<HttpMessageConverter<?>> converters = Collections.emptyList();
|
||||
@ -125,12 +123,16 @@ public class HttpMessageConvertersAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(StringHttpMessageConverter.class)
|
||||
@EnableConfigurationProperties(HttpEncodingProperties.class)
|
||||
protected static class StringHttpMessageConverterConfiguration {
|
||||
|
||||
@Autowired
|
||||
private HttpEncodingProperties httpEncodingProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public StringHttpMessageConverter stringHttpMessageConverter() {
|
||||
return new StringHttpMessageConverter(UTF_8);
|
||||
return new StringHttpMessageConverter(httpEncodingProperties.getCharset());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -53,6 +53,12 @@
|
||||
"description": "Automatically enable Solr repositories.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.http.encoding.enabled",
|
||||
"dataType": "java.lang.Boolean",
|
||||
"description": "Enable http encoding support.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "spring.jmx.enabled",
|
||||
"dataType": "java.lang.Boolean",
|
||||
|
@ -60,6 +60,7 @@ org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfigura
|
||||
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
|
||||
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration,\
|
||||
|
@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.filter.CharacterEncodingFilter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link HttpEncodingAutoConfiguration}
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class HttpEncodingAutoConfigurationTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultConfiguration() {
|
||||
load(EmptyConfiguration.class);
|
||||
CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class);
|
||||
assertCharacterEncodingFilter(filter, "UTF-8", true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disableConfiguration() {
|
||||
load(EmptyConfiguration.class, "spring.http.encoding.enabled:false");
|
||||
|
||||
thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
context.getBean(CharacterEncodingFilter.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customConfiguration() {
|
||||
load(EmptyConfiguration.class, "spring.http.encoding.charset:ISO-8859-15", "spring.http.encoding.force:false");
|
||||
CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class);
|
||||
assertCharacterEncodingFilter(filter, "ISO-8859-15", false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customFilterConfiguration() {
|
||||
load(FilterConfiguration.class, "spring.http.encoding.charset:ISO-8859-15", "spring.http.encoding.force:false");
|
||||
CharacterEncodingFilter filter = context.getBean(CharacterEncodingFilter.class);
|
||||
assertCharacterEncodingFilter(filter, "US-ASCII", false);
|
||||
}
|
||||
|
||||
private void assertCharacterEncodingFilter(CharacterEncodingFilter actual, String encoding, boolean forceEncoding) {
|
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(actual);
|
||||
assertEquals("Wrong encoding", encoding, accessor.getPropertyValue("encoding"));
|
||||
assertEquals("Wrong forceEncoding flag", forceEncoding, accessor.getPropertyValue("forceEncoding"));
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
this.context = doLoad(new Class<?>[] {config}, environment);
|
||||
}
|
||||
|
||||
private AnnotationConfigApplicationContext doLoad(Class<?>[] configs,
|
||||
String... environment) {
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
|
||||
applicationContext.register(configs);
|
||||
applicationContext.register(HttpEncodingAutoConfiguration.class);
|
||||
applicationContext.refresh();
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
static class EmptyConfiguration {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class FilterConfiguration {
|
||||
|
||||
@Bean
|
||||
public CharacterEncodingFilter myCharacterEncodingFilter() {
|
||||
CharacterEncodingFilter filter = new CharacterEncodingFilter();
|
||||
filter.setEncoding("US-ASCII");
|
||||
filter.setForceEncoding(false);
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -93,6 +93,10 @@ content into your application; rather pick only the properties that you need.
|
||||
spring.resources.cache-period= # cache timeouts in headers sent to browser
|
||||
spring.resources.add-mappings=true # if default mappings should be added
|
||||
|
||||
# HTTP encoding ({sc-spring-boot-autoconfigure}/web/HttpEncodingProperties.{sc-ext}[HttpEncodingProperties])
|
||||
spring.http.encoding=UTF-8 # the encoding of HTTP requests/responses
|
||||
spring.http.force=true # force the configured encoding
|
||||
|
||||
# JACKSON ({sc-spring-boot-autoconfigure}}/jackson/JacksonProperties.{sc-ext}[JacksonProperties])
|
||||
spring.jackson.date-format= # Date format string (e.g. yyyy-MM-dd HH:mm:ss), or a fully-qualified date format class name (e.g. com.fasterxml.jackson.databind.util.ISO8601DateFormat)
|
||||
spring.jackson.property-naming-strategy= # One of the constants on Jackson's PropertyNamingStrategy (e.g. CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) or the fully-qualified class name of a PropertyNamingStrategy subclass
|
||||
|
@ -64,7 +64,7 @@ public class SampleWebStaticApplicationTests {
|
||||
assertEquals(HttpStatus.OK, entity.getStatusCode());
|
||||
assertTrue("Wrong body:\n" + entity.getBody(), entity.getBody().contains("body"));
|
||||
assertEquals("Wrong content type:\n" + entity.getHeaders().getContentType(),
|
||||
MediaType.valueOf("text/css"), entity.getHeaders().getContentType());
|
||||
MediaType.valueOf("text/css;charset=UTF-8"), entity.getHeaders().getContentType());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user