mirror of https://github.com/halo-dev/halo
* fix bug #1085 * move exclude to Application class * enable multipart configuration * fixt multipart config error and catch MaxUploadSizeExceededException * fix checkstyle error * fix if not followed by whitespace * fix checkstyle error * modify multipart exception handling to global exception handling * remove unnecessary catch * remove unnecessary catchpull/1130/head
parent
2ca92b5391
commit
b5f742582c
|
@ -51,6 +51,7 @@ ext {
|
|||
qcloudSdkVersion = "5.6.25"
|
||||
minioSdkVersion = "7.1.4"
|
||||
swaggerVersion = "2.9.2"
|
||||
commonsFileUploadVersion = "1.4"
|
||||
commonsLangVersion = "3.10"
|
||||
httpclientVersion = "4.5.12"
|
||||
dataformatYamlVersion = "2.11.0"
|
||||
|
@ -92,6 +93,7 @@ dependencies {
|
|||
implementation "io.minio:minio:$minioSdkVersion"
|
||||
implementation "io.springfox:springfox-swagger2:$swaggerVersion"
|
||||
implementation "io.springfox:springfox-swagger-ui:$swaggerVersion"
|
||||
implementation "commons-fileupload:commons-fileupload:$commonsFileUploadVersion"
|
||||
implementation "org.apache.commons:commons-lang3:$commonsLangVersion"
|
||||
implementation "org.apache.httpcomponents:httpclient:$httpclientVersion"
|
||||
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$dataformatYamlVersion"
|
||||
|
|
|
@ -2,6 +2,7 @@ package run.halo.app;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
@ -14,7 +15,7 @@ import run.halo.app.repository.base.BaseRepositoryImpl;
|
|||
* @author ryanwang
|
||||
* @date 2017-11-14
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(exclude = { MultipartAutoConfiguration.class })
|
||||
@EnableAsync
|
||||
@EnableJpaRepositories(basePackages = "run.halo.app.repository", repositoryBaseClass = BaseRepositoryImpl.class)
|
||||
public class Application extends SpringBootServletInitializer {
|
||||
|
|
|
@ -5,6 +5,8 @@ import freemarker.core.TemplateClassResolver;
|
|||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.jackson.JsonComponentModule;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -17,6 +19,8 @@ import org.springframework.http.converter.HttpMessageConverter;
|
|||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
@ -29,6 +33,7 @@ import run.halo.app.factory.StringToEnumConverterFactory;
|
|||
import run.halo.app.model.support.HaloConst;
|
||||
import run.halo.app.security.resolver.AuthenticationArgumentResolver;
|
||||
|
||||
import javax.servlet.MultipartConfigElement;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
@ -45,6 +50,7 @@ import static run.halo.app.utils.HaloUtils.*;
|
|||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(MultipartProperties.class)
|
||||
public class WebMvcAutoConfiguration extends WebMvcConfigurationSupport {
|
||||
|
||||
private static final String FILE_PROTOCOL = "file:///";
|
||||
|
@ -158,6 +164,25 @@ public class WebMvcAutoConfiguration extends WebMvcConfigurationSupport {
|
|||
return configurer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuring multipartResolver for large file upload..
|
||||
*
|
||||
* @return new multipartResolver
|
||||
*/
|
||||
@Bean(name = "multipartResolver")
|
||||
public MultipartResolver multipartResolver(MultipartProperties multipartProperties) {
|
||||
MultipartConfigElement multipartConfigElement = multipartProperties.createMultipartConfig();
|
||||
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
|
||||
resolver.setDefaultEncoding("UTF-8");
|
||||
resolver.setMaxUploadSize(multipartConfigElement.getMaxRequestSize());
|
||||
resolver.setMaxUploadSizePerFile(multipartConfigElement.getMaxFileSize());
|
||||
|
||||
//lazy multipart parsing, throwing parse exceptions once the application attempts to obtain multipart files
|
||||
resolver.setResolveLazily(true);
|
||||
|
||||
return resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuring view resolver
|
||||
*
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.springframework.web.bind.MissingServletRequestParameterException;
|
|||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
import run.halo.app.exception.AbstractHaloException;
|
||||
import run.halo.app.model.support.BaseResponse;
|
||||
|
@ -106,6 +107,15 @@ public class ControllerExceptionHandler {
|
|||
return baseResponse;
|
||||
}
|
||||
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public BaseResponse<?> handleUploadSizeExceededException(MaxUploadSizeExceededException e) {
|
||||
BaseResponse<Object> response = handleBaseException(e);
|
||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
||||
response.setMessage("当前请求超出最大限制:" + e.getMaxUploadSize() + " bytes");
|
||||
return response;
|
||||
}
|
||||
|
||||
@ExceptionHandler(AbstractHaloException.class)
|
||||
public ResponseEntity<BaseResponse<?>> handleHaloException(AbstractHaloException e) {
|
||||
BaseResponse<Object> baseResponse = handleBaseException(e);
|
||||
|
|
Loading…
Reference in New Issue