【问题标题】:Forbidden response when trying to access putmapping url尝试访问 putmapping url 时禁止响应
【发布时间】:2019-12-27 21:37:20
【问题描述】:

我尝试使用 PutMapping 更新控制器中的记录,并且我也使用 spring-security。我不知道为什么它显示禁止,因为我在设置 httpSecurity 对象时提到了access("permitAll")

@Override
protected void configure(HttpSecurity http) throws Exception {
http
    .authorizeRequests()
     .antMatchers("/home").hasAuthority("USER")
      .antMatchers("/username").authenticated()
      .antMatchers("/testAdmin").hasAuthority("ADMIN")
      .antMatchers(HttpMethod.PUT, "/updateLocation/**").access("permitAll")
      .antMatchers("/","/**").access("permitAll")
      .anyRequest().authenticated()
    /* ... */
}


// in controller:
@Data
class UpdateUserLocation{
    private double lat;
    private double lng;
}
//@CrossOrigin(origins = "http://localhost:8085")
@PutMapping(path="/updateLocation/{userId}", consumes={MediaType.APPLICATION_JSON_VALUE}, produces= {MediaType.APPLICATION_JSON_VALUE})
UserLocations updateUserLocation(@PathVariable long id, @RequestBody UpdateUserLocation usr) {
    UserLocations usrLoc = userLocRepo.findByUser_id(id);
    System.out.println("Usr loc:" + usrLoc.getId() + " " + usrLoc.getLat()+","+usrLoc.getLng());
    usrLoc.setLat(usr.getLat());
    usrLoc.setLng(usr.getLng());
    userLocRepo.saveAndFlush(usrLoc);
    return usrLoc;
}

在邮递员中,我使用我的链接: localhost/updateLocation/2
其中 2 - 代表我要更新的对象的 id 在正文中我添加了:
{ "lat" : "47.2", "lng" : "27.9" }
我收到了这样的回复:
{ "timestamp": "2019-08-22T13:56:09.189+0000", "status": 403, "error": "Forbidden", "message": "Forbidden", "path": "/updateLocation/2" }

【问题讨论】:

  • 还是不行,也显示禁止:(
  • 我的控制器没有基本映射
  • @ViorelCasapu 你没有发送 CSRF 令牌。您必须在请求中发送令牌。
  • @danilo 还是同样的问题
  • @dur 我在 configure() 中禁用了 csrf 并且有效,但我认为禁用它是不行的。你能提示如何在邮递员中发送 csrf 令牌吗?

标签: spring-boot spring-security postman


【解决方案1】:

使用 antMatchers(HttpMethod.PUT, "/updateLocation/*") 代替 antMatchers(HttpMethod.PUT, "/updateLocation/**")。

去掉一个星号。

【讨论】:

  • 还是同样的问题
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 2018-12-17
  • 1970-01-01
  • 2015-08-20
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多