Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Spring Security 3.1 and Multipart File Uploads

  1. #1
    Join Date
    Feb 2013
    Posts
    6

    Default Spring Security 3.1 and Multipart File Uploads

    I'm having a heck of a time getting Spring 3.1 / Spring Security 3.1 to allow file uploads.

    I have the following controller..

    Code:
    @PreAuthorize("hasAuthority('ROLE_USER')")
        @RequestMapping(value = "/upload", method = RequestMethod.PUT)
        public @ResponseBody Account putImage(@RequestParam("title") String title, MultipartHttpServletRequest request, Principal principal)
    And when I try to access it using a REST client, with Content-Type multipart/form-data set, I get the following exception:

    Code:
    java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ org.apache.catalina.connector.RequestFacade@1aee75b7]]
    It seems like this has to do with the Spring Security filter chain, but I'm really not sure what to do about this. Any help or pointers would be very much appreciated - I've spent a lot of my weekend on this seemingly simple problem!

    I've also tried using a @RequestParam MutipartFile in the arguments, but that leads to a different error where it claims that my multipartResolver is not set up (it is).

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    If your setup would be correct the request would be of the type MultipartHttpServletRequest. Both errors lead me to believe your setup is wrong. Please post your configuration ...

    Edit: A little code inspection shows that the CommonsMultipartResolver only accepts multipart form-data when the request is post, all other requests aren't allowed and as such the method isMultipart returns false.

    I suggest submitting a JIRA to improve this.
    Last edited by Marten Deinum; Feb 25th, 2013 at 12:46 PM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2013
    Posts
    6

    Default

    That's what I thought too. Here are the relevant parts of my config:

    Annotations are set up:
    Code:
    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    This request's full url would be /api/image/upload:
    Code:
    <http pattern="/api/**" create-session="stateless" entry-point-ref="oauthAuthenticationEntryPoint"
              use-expressions="true" xmlns="http://www.springframework.org/schema/security">
            <anonymous enabled="false" />
            <intercept-url pattern="/api/**" />
            <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
            <access-denied-handler ref="oauthAccessDeniedHandler" />
            <expression-handler ref="oauthWebExpressionHandler" />
    </http>
    The expression handler is the one from the spring-security-oauth2 project:
    Code:
    <oauth:web-expression-handler id="oauthWebExpressionHandler" />

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I edited my previous reply but here it is again...

    Quote Originally Posted by Marten Deinum
    Edit: A little code inspection shows that the CommonsMultipartResolver only accepts multipart form-data when the request is post, all other requests aren't allowed and as such the method isMultipart returns false.

    I suggest submitting a JIRA to improve this.

    For now you could extend the MultipartResolver you use and override the isMultipart method.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2013
    Posts
    6

    Default

    Ah, nice catch. Interesting!

    In looking at the 3.1 source for CommonsMultipartResolver (https://fisheye.springsource.org/bro...r.java?hb=true) I don't see where it's limited to only POST methods. I'm happy to submit a jira, even with patch code, but I'm not sure I see where that limit happens.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Well actually it is in the commons-fileupload class not in the Spring one (sorry for the confusion). The file ServletFileUpload contains that code in the method isMultipartContent.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Feb 2013
    Posts
    6

    Default

    Oh, I see. Thank you very much, sorry for the confusion. I'll try this out shortly and let you know how it goes.

  8. #8
    Join Date
    Feb 2013
    Posts
    6

    Default

    Problem solved. Thanks again!

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Great to hear. Could you still register a JIRA to enhance the file upload support to support other requests next to POST.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  10. #10
    Join Date
    Feb 2013
    Posts
    6

    Default

    I opened FILEUPLOAD-214 for this issue.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •