【问题标题】:How to configure Spring Boot that it permits Wicket's file upload?如何配置允许 Wicket 文件上传的 Spring Boot?
【发布时间】:2019-07-02 13:50:00
【问题描述】:

我在 Web 应用程序中使用 Spring Boot 和 Apache Wicket。我必须添加文件上传。

在下面的代码中,Wicket 的 Form 组件的 onSubmit 方法被触发,但 "uploads" 为空。

@Override
            protected void onSubmit()
            {
                final List<FileUpload> uploads = fileUploadField.getFileUploads();

                if (uploads != null)
                {
                    for (FileUpload upload : uploads)
                    {
                        // Create a new file
                        File newFile = new File(getUploadFolder(), upload.getClientFileName());

                        // Check new file, delete if it already existed
                        checkFileExists(newFile);
                        try
                        {
                            // Save to new file
                            newFile.createNewFile();
                            upload.writeTo(newFile);

//                          UploadPage.this.info("saved file: " + upload.getClientFileName());
                        }
                        catch (Exception e)
                        {
                            throw new IllegalStateException("Unable to write file", e);
                        }
                    }
                }
            }

Spring的配置方法

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests().antMatchers("/**").permitAll()
            .and()
            .logout()
            .permitAll();
        http.headers().frameOptions().disable();

    }

我创建了一个单独的应用程序,其中包含 Wicket 但不包含 Spring,并且相同的上传代码可以正常工作。

这个我试过了,还是不行。

public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {

         @Override
         protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
          insertFilters(servletContext, new MultipartFilter()); 
         }
    } 

    @Bean(name = "filterMultipartResolver")
     public CommonsMultipartResolver getMultipartResolver() {
          CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
          return multipartResolver;
     }

当我通过 javascript 向表单组件的操作 url 添加以下建议时,甚至不会触发 Wicket 的“onSubmit”方法

Spring MVC - upload file is blocked by spring security

编辑: 当我在网络上观看时,我看到 Spring 向 Wicket 的 POST 上传请求返回 302。

【问题讨论】:

  • 您使用哪些版本的 Spring Boot 和 Security ?
  • Spring Boot 2.0.2、Spring Security 5.0.5

标签: java spring-boot file-upload spring-security wicket


【解决方案1】:

将此添加到 application.properties 文件解决了问题。

spring.servlet.multipart.enabled=false

【讨论】:

  • 谢谢,我今天在维护旧代码库时遇到了这个问题。拯救了我的一天!
猜你喜欢
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 2020-04-19
相关资源
最近更新 更多