删除不在春天工作

时间:2016-07-26 12:53:46

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

@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 security时,我的删除工作无效。

1 个答案:

答案 0 :(得分:1)

您需要查看Spring安全配置:

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

当您说anyRequest().authenticated()时,表示应对所有请求进行身份验证。

如果您想在不进行身份验证的情况下调用camera/list,请将其添加到permitAll()