【问题标题】:Spring Rest Controller with Byte Array file带有字节数组文件的 Spring Rest 控制器
【发布时间】:2016-07-23 22:13:07
【问题描述】:

我正在尝试将文件内容的字节数组从 angular 发送到其余 api。但我正在禁止 Http 403。我没有启用任何安全性。我不确定我做错了什么。我尝试了几种不同的 MIME 类型。以下是来自 angular js 的 http 帖子, $scope.encoded_file 包含字节数组中的文件。

$http({
  method:'POST',
  url:'http://localhost:9200/docupload',
  data:{'content':$scope.encoded_file,'name':'test','type':'pdf'},
  headers:{'Content-Type':'application/octet-stream'}
  }).success(function(){}); 
 };

DocUploadService

  @RestController
  public class DocUploadService {

  @RequestMapping(value="/docupload",method={RequestMethod.GET,RequestMethod.POST},consumes="application/octet-stream")
  public String UploadDocuments(@RequestParam(value="content")byte[] content,@RequestParam(value="type") String type,@RequestParam(value="name") String name) throws IOException
    {
        System.out.println("****Inside DocUpload*****");}}

请求当然没有到达服务,我不知道为什么会收到 403。有什么想法吗?

请求标头:

在服务方法之前添加了@CrossOrigin,但仍然没有运气

以下是否将 CrossOrigin 添加到控制器方法中。

public class DocUploadService {
@CrossOrigin(origins = "http://localhost:9200")
@RequestMapping(value="/docupload",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.OPTIONS,RequestMethod.PUT},consumes="application/octet-stream")

另外,添加了全球 CORS 注册表。

 @EnableWebMvc
 public class WebMVCconfig extends WebMvcConfigurerAdapter {

 @Override
 public void addCorsMappings(CorsRegistry registry) {
    // TODO Auto-generated method stub
    registry.addMapping("/docupload")
    .allowedOrigins("http://localhost:9200")
    .allowedMethods("POST", "GET","OPTION","PUT");
    super.addCorsMappings(registry);

}

【问题讨论】:

  • 这可能是 CORS 问题。您还有其他实际工作的控制器吗?
  • 尝试检查请求(例如使用 chrome firebug)以查看是否有任何 CORS 错误
  • 应该是CORS。但我不知道如何解决。请检查帖子的编辑部分。我添加了请求标头。
  • 看我的回答,我认为这会有所帮助

标签: spring rest


【解决方案1】:

尝试添加过滤器以避免 CORS:

   @Component
public class SimpleCORSFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Expose-Headers", "Location");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

或者你也可以使用@CrossOrigin注解:

@CrossOrigin(origins = "http://localhost:9200")

这里有一个例子:

https://spring.io/guides/gs/rest-service-cors/

【讨论】:

  • 您好,感谢您花时间在这方面。我尝试了基于 CORS 的 spring doc 的所有内容,但仍然没有运气,我不知道问题是什么。我使用spring boot,所以我没有web.xml。我也会尝试过滤方法。
  • 我编辑了我的答案,这是 spring-boot 的过滤器定义。
  • 嗨,阿尔瓦罗。感谢您在这方面帮助我。添加过滤器对我也不起作用。我最终不得不禁用 CSRF 以使其工作。我知道这不是正确的解决方法。如果我发现新内容,我将进一步研究并更新此线程。谢谢。
猜你喜欢
  • 2015-01-28
  • 2019-01-02
  • 2018-02-06
  • 2017-02-16
  • 2016-08-26
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 2020-01-03
相关资源
最近更新 更多