带有@PreAuthorize 的 JHipster 始终允许访问

JHipster with @PreAuthorize always allow access

我的资源有这个代码:

    @DeleteMapping("/devices/{id}")
    @Timed
    @PreAuthorize("hasAuthority('ROLE_ADMIN')")
    public ResponseEntity<Void> deleteDevice(@PathVariable Long id) {
        log.debug("REST request to delete Device : {}", id);
        deviceService.delete(id);
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString())).build();
    }

它应该只有 'ROLE_ADMIN' 的用户才能执行。但是,任何登录用户都可以 运行 它,即使他们不拥有 ROLE_ADMIN 权限。

我也试过添加

            .antMatchers(HttpMethod.DELETE,"/api/**").hasAuthority(AuthoritiesConstants.ADMIN)

在 SecurityConfiguration 上,但请求从未被阻止。我错过了什么?我正在使用 JHipster 6.9.1

如我的评论所述 - 在您的 Configuration 中启用 prePostEnabled 并激活方法安全性:

@EnableGlobalMethodSecurity(
  prePostEnabled = true, 
...)

我不知道为什么 JHipster 默认不启用它