2016-07-27 47 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方法。 –