How to get Spring multipart file uploading working with spring security

Recently while working on my latest website which is using Spring I was adding the ability to upload files. I Googled around for a bit and eventually figured out what configuration steps you need in order to get it working. It was a little difficult to find out and took a few more Google searches than I would’ve liked so I thought it might be helpful for others if I wrote a separate article explaining what you need to do exactly.

Step #1

First step we need to add a filterMultipartResolver bean to your root config class (apologies I’m not covering the XML file way if you’re into that). Note there seems to be some bad information about the exact bean name out there so be sure to use ‘filterMultipartResolver’. So add the below code block to your root configuration class file, mines named ‘ApplicationConfig’.

    @Bean
    public CommonsMultipartResolver filterMultipartResolver(){
        return new CommonsMultipartResolver();
    }

Step #2

Next since we are using Spring Security we need to insert a MultipartFilter in a AbstractSecurityWebApplicationInitializer implementation or else you will get a 403 error when trying to upload a file. If you don’t already have an AbstractSecurityWebApplicationInitializer (could they have made a longer class name??) you can copy the below code into a new class file or if you already have a class you can just grab the method inside it.

public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    @Override
    protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
        insertFilters(servletContext, new MultipartFilter());
    }
}

Step #3

Last step for the configuration is to add commons-fileupload and commons-io dependencies to your pom.xml file, or grab the jars from the net if you’re not enlightened yet and not using Maven. The necessary Maven dependency additions are below:

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>

Appendix/Step #4

Next if you haven’t already set up your thymeleaf view controller request mapping for the upload here’s how to do it. First jump over to your thymeleaf html file and add a form with a input[file] element like this

<form method="post" th:action="@{/saveProfilePic}" enctype="multipart/form-data">
    <label for="files" class="btn">Upload New Profile Image</label>
    <input id="files" type="file" name="file" style="float: left;visibility:hidden;" class="btn" th:value="upload"/>
    <button type="submit">Save</button>
</form>

Then jump over to your controller and add the request mapping for the upload file action with the file being a RequestParam and MultipartFile as the type, see below

    @RequestMapping(value = "/saveProfilePic", method = RequestMethod.POST)
    public String saveProfilePic(@RequestParam("file") MultipartFile file, Principal principal) {
        /* your logic here */
        /* use file.getInputStream() to get the file stream or file.getBytes() for the full byte array */
    }

After adding that you should be all set to upload your files with Spring.

Hope this article was helpful for you and thank you for reading!

Only cool people share. You do wanna be cool right?Share on Reddit0Share on Facebook0Share on StumbleUpon0Tweet about this on Twitter0

Leave a Reply

Time limit is exhausted. Please reload the CAPTCHA.