【发布时间】: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之后我的删除可以工作,而删除根本无法工作。
-
此外,有关区分 http 方法库的详细信息,请查看question。它是 xml 配置的,但我敢打赌,基于注释的配置有一些非常相似的东西。
-
你能在控制台看到
Fetching & Deleting data " + filename吗?
标签: java spring mongodb spring-mvc spring-security