【已解决】 @PreAuthorize("@permission.admin()")不能正常放回统一的结果
@PreAuthorize("@permission.admin()")
@GetMapping("/list")
public ResponseResult listUser(@RequestParam("page") int page,
@RequestParam("size") int size) {
return userService.listUser(page, size);
}
@PreAuthorize("@permission.admin()")
@DeleteMapping("/{userId}")
public ResponseResult deleteUser(@PathVariable("userId") String userId) {
return userService.deleteUser(userId);
}
上面两个接口 get会返回没有权限,第二个没有统一的返回结果
@Configuration
public class ErrorCodeConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
registry.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403"));
registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
registry.addErrorPages(new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/405"));
registry.addErrorPages(new ErrorPage(HttpStatus.GATEWAY_TIMEOUT, "/504"));
registry.addErrorPages(new ErrorPage(HttpStatus.HTTP_VERSION_NOT_SUPPORTED, "/505"));
}
}

控制台打印
020-08-11 16:35:44.182 WARN 45800 --- [nio-2020-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'DELETE' not supported]
改成这样子
解决了吗
这个bug在很后面的视频里才解掉。
因为405里面的是get请求,换成RequestMapping即可。