如何配置Spring Boot以允许Wicket上传文件?

时间:2019-06-27 06:20:33

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

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

在下面的代码中,Wicket的Form组件的onSubmit方法被触发,但是“上载”为空。

@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的configure方法

@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在表单组件的操作网址中添加以下建议时,甚至不会触发Wicket的“ onSubmit”方法

Spring MVC - upload file is blocked by spring security

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

1 个答案:

答案 0 :(得分:0)

将此添加到application.properties文件中即可解决此问题。

spring.servlet.multipart.enabled=false