2016-07-27 40 views
1

我的控制器支持:'删除' 方法不使用Spring

@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 security之后,我得到了这个不支持的错误。任何人都可以帮忙?我尝试在网上寻求帮助,但没有解决方案。

回答

0

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

0

HiddenHttpMethodFilter添加到您的web.xml中。

+0

我没有web.xml文件 –

+0

然后在你的类中实现WebApplicationInitializer – Alexander

+0

创建另一个新类的权利? –

0

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

+0

仍然是一样的错误 –

+0

您的控制器类为'/ camera'指定了'RequestMapping',对吧?方法注解只声明'/ list/{fn}'。请注意,如果你在一个无效的URL上执行了其他的GET操作,Spring会返回一个405 ... –

+0

我认为spring安全限制了我使用delete方法。 –