gpt4 book ai didi

Spring Security 和多部分请求

转载 作者:IT老高 更新时间:2023-10-28 13:48:07 26 4
gpt4 key购买 nike

我有一个受 Spring Security 和 OAuth2 保护的 @Controller,我试图让我的用户上传文件:

@Controller
@RequestMapping(value = "/api/image")
public class ImageController {

@PreAuthorize("hasAuthority('ROLE_USER')")
@RequestMapping(value = "/upload", method = RequestMethod.PUT)
public @ResponseBody Account putImage(@RequestParam("title") String title, MultipartHttpServletRequest request, Principal principal){
// Some type of file processing...
System.out.println("-------------------------------------------");
System.out.println("Test upload: " + title);
System.out.println("Test upload: " + request.getFile("file").getOriginalFilename());
System.out.println("-------------------------------------------");

return ((Account) ((OAuth2Authentication) principal).getPrincipal());
}
}

当我尝试上传文件和标题时,出现以下异常。我将 Content-Type header 设置为 multipart/form-data。

java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ org.apache.catalina.connector.RequestFacade@1aee75b7]]
at org.springframework.web.servlet.mvc.method.annotation.ServletRequestMethodArgumentResolver.resolveArgument(ServletRequestMethodArgumentResolver.java:84)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)

如何在 Spring Security 后面进行文件上传?似乎请求永远不会变成 M​​ultiPartHttpServerRequest,所以它不起作用?

如果我将方法签名更改为采用 @RequestParam MultipartFile,则会收到如下异常:

DEBUG DefaultListableBeanFactory - Returning cached instance of singleton bean 'imageController'
DEBUG ExceptionHandlerExceptionResolver - Resolving exception from handler [public com.tinsel.server.model.Account com.tinsel.server.controller.ImageController.putImage(java.lang.String,org.springframework.web.multipart.MultipartFile,java.security.Principal)]: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
DEBUG ResponseStatusExceptionResolver - Resolving exception from handler [public com.tinsel.server.model.Account com.tinsel.server.controller.ImageController.putImage(java.lang.String,org.springframework.web.multipart.MultipartFile,java.security.Principal)]: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
DEBUG DefaultHandlerExceptionResolver - Resolving exception from handler [public com.tinsel.server.model.Account com.tinsel.server.controller.ImageController.putImage(java.lang.String,org.springframework.web.multipart.MultipartFile,java.security.Principal)]: java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
DEBUG DispatcherServlet - Could not complete request
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?
at org.springframework.util.Assert.notNull(Assert.java:112)

...但我确实在我的 XML 中配置了 MultipartResolver:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="268435456"/> <!-- 256 megs -->
</bean>

我确实看到了 this blog post about getting this working under Spring 3.0 - 但我正在努力保持最新状态,目前正在使用 3.1。是否有更新的修复?

最佳答案

问题是我使用的是 PUT 而不是 POST。 Commons FileUpload 被硬编码为只接受文件的 POST 请求。

查看isMultipartContent method那里。要解决此问题,请使用 POST 或扩展该类并覆盖该方法以按照您的喜好工作。

我打开了FILEUPLOAD-214对于这个问题。

关于Spring Security 和多部分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15058548/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com