JHipster:允许匿名用户读取实体,但不更新?

JHipster: Enable anonymous users to read entity, but not update?

我已经使用这些值生成了一个 JHipster 应用程序:

{
  "generator-jhipster": {
    "jhipsterVersion": "3.1.0",
    "baseName": "app",
    "packageName": "my.app",
    "packageFolder": "my/app",
    "serverPort": "8080",
    "authenticationType": "session",
    "hibernateCache": "ehcache",
    "clusteredHttpSession": "no",
    "websocket": "no",
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "prodDatabaseType": "mysql",
    "searchEngine": "elasticsearch",
    "buildTool": "gradle",
    "enableSocialSignIn": false,
    "rememberMeKey": "",
    "useSass": true,
    "applicationType": "monolith",
    "testFrameworks": [],
    "jhiPrefix": "jhi",
    "enableTranslation": false
  }
 }

我想允许匿名用户查看实体,但不能更新或删除该实体。我尝试编辑生成的 SecurityConfiguration.java 文件以在 configure(HttpSecurity http) 方法中为 authorizeRequests() 添加 permitAll(HttpMethod.GET,"/**")。尝试访问该实体时,我仍然被定向到 accessdenied

以前有人解决过这个用例吗?

这是给 AngularJS 1.x

用于访问资源:在SecurityConfiguration.java中在configure(HttpSecurity http)方法中

    .and()
        .authorizeRequests()
        .antMatchers(HttpMethod.GET, "/api/**").permitAll()

为了访问 angular views/states:对于每个实体,注释掉或删除只读状态的 authorities 属性。下面是 src/main/webapp/app/entities/book/book.state.jsBook 实体的示例:

    .state('book', {
        parent: 'entity',
        url: '/book',
        data: {
            // authorities: ['ROLE_USER'],
            pageTitle: 'monoApp.book.home.title'
        },
        ....
    })
    .state('book-detail', {
        parent: 'entity',
        url: '/book/{id}',
        data: {
            // authorities: ['ROLE_USER'],
            pageTitle: 'monoApp.book.detail.title'
        },

但是,要注意两点:

  • 通过在 SecurityConfiguration 中使用这种模式,您还可以在 /api/users 中暴露您的用户。为每个实体添加一个 permitAll() 会更安全,这样您就可以完全控制所公开的内容(白名单方法)
  • 用户体验很差,因为您仍然暴露用于添加或删除实体的按钮。所以你可以用 ng-hide
  • 隐藏它们

同意 @Gaël Marziou 但我认为与其允许所有 GET /api,不如添加新前缀

/public

及授权:

.antMatchers("/public/**").permitAll()

所有 public api 可能看起来更好