【问题标题】:Delete not working in spring删除在春季不起作用
【发布时间】:2016-07-26 12:53:46
【问题描述】:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 @Override
 protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();
 }

 @Autowired
 public void configureGlobal(AuthenticationManagerBuilder auth) throws  Exception {
    auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");

}
}

我的 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);
          });
        }
      };

我的删除控制器:

@RequestMapping(value = "/list/{fn}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
public 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);
}
}

我的仓库:

public interface RecordRepository extends MongoRepository<Record, String> {


@Query("{ 'filename' : ?0 }")
Record findByfilename(String filename);

long deleteByfilename(String filename);


}

当我点击删除按钮时,它显示了这个错误:

DELETE 
XHR 
http://localhost:8086/camera/list/2fb1a2e020285cd91dc68a4fa7822151 [HTTP/1.1   403 Forbidden 14ms]

有人知道错误是什么吗?起初我的删除有效,但当我使用弹簧安全时,我的删除无效。

【问题讨论】:

  • 向我们展示您的 spring 安全访问设置。
  • 访问设置在哪里?一开始很抱歉,在我使用spring security之后我的删除可以工作,而删除根本无法工作。
  • 如果你完全不了解spring security,建议你在使用之前先找一个基础教程。通常,它有一个配置 xml 文件(可能它也可以是注解配置的),您可以在其中放置您的安全声明。看herehere。我的猜测是您应该为与该 URL 关联的 DELETE 动词输入授权规则。请参阅我指出的链接以了解如何操作。
  • 此外,有关区分 http 方法库的详细信息,请查看question。它是 xml 配置的,但我敢打赌,基于注释的配置有一些非常相似的东西。
  • 你能在控制台看到Fetching &amp; Deleting data " + filename吗?

标签: java spring mongodb spring-mvc spring-security


【解决方案1】:

您需要检查您的 spring 安全配置:

 http.authorizeRequests()
        .antMatchers("/", "/home").permitAll()
        .anyRequest().authenticated()

当您说anyRequest().authenticated() 时,表示所有请求都应该经过身份验证。

如果你想允许 camera/list 在没有身份验证的情况下被调用,请将其添加到 permitAll()

【讨论】:

  • 应该怎么写?
  • 现在,在我为我的相机/列表添加 permitall() 后,我的删除方法不受支持
猜你喜欢
  • 1970-01-01
  • 2014-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-22
  • 2015-02-27
  • 2016-08-20
相关资源
最近更新 更多