简体   繁体   中英

HTTP 403 Forbidden Jersey on Put Request

I am facing an 'HTTP 403 Forbidden' error while trying to consume PUT request of a restful resource from an angular client. I created this restful resource using jersey and I am using tomcat 7 as application server.

Here is my resource code:

@Path("/doc")
public class DocResource {


@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("file/upload")
public Response uploadFile(MultipartBody body, @QueryParam("ID") long ID) {
    try {
        Attachment attachment = body.getAttachment("file");
        MultivaluedMap<String, String> headers = attachment.getHeaders();
        String fileName = getFileName(headers);
        DataHandler dataHandler = attachment.getDataHandler();
        InputStream inputStream = dataHandler.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] tmp = new byte[4096];
        int ret = 0;

        while ((ret = inputStream.read(tmp)) > 0) {
            bos.write(tmp, 0, ret);
        }

        // TODO - Save contents as process attachment
        byte[] contents = bos.toByteArray();

        return Response.ok(getDocumentService().createAttachment(ID, fileName, contents, attachment.getContentType()), MediaType.APPLICATION_JSON).build();
    } catch (Exception e) {
        return handleException(e, "failed to upload Attachement");
    }
}
}

Here is my angular js snippet

  this.uploadFile = function uploadFile(callback, ID, file) {
  var baseRestURL="http://localhost:8080/rest/doc"
              // resource query
              var query ;
              // create form data
              var formData = new FormData();
              formData.append('file', file);

              // set up the resource

          var resource = $resource(baseRestURL + '/file/upload', {
                  ID: ID
              }, {
                  'ID': ID,
                  'upload': {
                      method: 'PUT',
                      headers: {
                          'Content-Type': 'multipart/form-data'
                      }
                  }
              });

              resource.upload(query, formData).$promise.then(function success(response) {
                 if (callback) callback(response);
              }, function error() {
                  //TODO handle error
              });
          };

I want to notice that other type of http calls such as DELETE, POST and GET are working properly. I have only problems with PUT calls.

I had a similar issue, but both DELETE and PUT were not working for me - returned HTTP 403 error. After I searched I stumbled across this link , and it pointed out where I went wrong.

I had a CORS filter added in my web.xml as part of some other R&D i was doing, and forgot to remove it.

This was from the original web.xml

 <servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>
      io.swagger.jaxrs.listing,
      com.xxx.yyy.rest</param-value>
    </init-param>
    <init-param>
      <param-name>jersey.config.server.provider.classnames</param-name>
      <param-value>
            org.glassfish.jersey.jackson.JacksonFeature;
            org.apache.catalina.filters.CorsFilter
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

Once I removed the org.apache.catalina.filters.CorsFilter it started to work for me!

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM