Spring不支持“删除”方法

时间:2016-07-27 07:19:34

标签: spring spring-mvc spring-security

我的控制器:

 @RequestMapping(value = "/list/{fn}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
 public @ResponseBody ResponseEntity<Record> deleteUser(@PathVariable("fn")   String filename) {
   System.out.println("Fetching & Deleting data " + filename);

   Record user1 = rep.findByfilename(filename);
   if (user1 == null) {
       System.out.println("Unable to delete." + filename + " not found");
       return new ResponseEntity<Record>(HttpStatus.NOT_FOUND);
   }

   rep.deleteByfilename(filename);
   return new ResponseEntity<Record>(HttpStatus.NO_CONTENT);
}
}

我的js代码:

$scope.del = function (record) {
        if (confirm('Do you really want to delete?')){
             $http['delete']('/camera/list/' + record.filename).then(function() {
                 $scope.records.splice($scope.records.indexOf(record), 1);
          });
        }
      };

我的访问设置:

http
    .authorizeRequests()
    .antMatchers("/", "/home").permitAll()
    .antMatchers("/imageview", "/hello").access("hasRole('USER')")
    .antMatchers("/camera/list").permitAll()
    .antMatchers("/camera/store").permitAll()
    .antMatchers("/camera/list/{fn}").permitAll()
    .antMatchers("/imageview2", "/hello2").access("hasRole('ADMIN')").and()
    .formLogin().and().exceptionHandling()
    .accessDeniedPage("/access-denied");

    http
        .authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login").permitAll()
            .and()
        .logout()
            .permitAll();
}

我得到的错误是:

DELETE 
XHR 
http://localhost:8086/camera/list/a0c8918e4b088de4a5c7796e3eb11229 [HTTP/1.1 405 Method Not Allowed 22ms]

首先,我的删除功能可以工作,但在我使用Spring安全后,我得到了这个不支持的错误。有人可以帮忙吗?我试着在网上寻求帮助但没有解决方案。

3 个答案:

答案 0 :(得分:0)

HiddenHttpMethodFilter添加到您的web.xml。

答案 1 :(得分:0)

尝试使用/camera/list/**之类的通配符模式作为antMatcher而不是用于定义控制器配置(/camera/list/{fn})的模式,以允许访问特定项目。

答案 2 :(得分:0)

我刚刚通过添加http.csrf()。disable()弹出安全访问设置来解决问题。