Jhipster 新角色 - 需要完全身份验证

Jhipster new Roles - Full authentication is required

我有一个问题,我无法访问任何 url,同时使用自定义角色登录帐户。

为了为我的应用程序创建新角色,我在 AuthoritiesConstants class 和 authorities.csv 中添加了我的新角色。 然后我在我的 h2 数据库中手动插入了我想要的新角色:ROLE_STUDENTROLE_PROFESOR

然后我登录了管理员帐户并尝试并成功地使用 ROLE_STUDENT 创建了一个新用户。 然后我登录到这个新帐户并尝试访问 http://localhost:9000/api/users 以获取完整的用户列表。 我收到以下错误:

2020-03-19 10:59:05.687 DEBUG 13892 --- [ XNIO-1 task-15] base.aop.logging.LoggingAspect           : Enter: base.repository.CustomAuditEventRepository.add() with argument[s] = [AuditEvent [timestamp=2020-03-19T08:59:05.686Z, principal=anonymousUser, type=AUTHORIZATION_FAILURE, data={details=org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null, type=org.springframework.security.access.AccessDeniedException, message=Access is denied}]]
2020-03-19 10:59:05.691 DEBUG 13892 --- [ XNIO-1 task-15] base.aop.logging.LoggingAspect           : Exit: base.repository.CustomAuditEventRepository.add() with result = null
2020-03-19 10:59:05.693  WARN 13892 --- [ XNIO-1 task-15] o.z.problem.spring.common.AdviceTraits   : Unauthorized: Full authentication is required to access this resource
2020-03-19 10:59:05.695  WARN 13892 --- [ XNIO-1 task-15] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource]

在我的 SecurityConfiguration class 中,这个 url 属于 .antMatchers("/api/**").authenticated()。所以我应该能够从任何帐户访问它,只要我登录了。

令我沮丧的是,我似乎无法从该帐户访问除主页之外的任何 URL。我手动检查了我的数据库以查看用户是否已创建并具有正确的角色。那里一切都很好。 有人可以帮我解决这个问题吗?

你也必须打开新角色的路由,这是在客户端完成的。如果您使用 angular.

,这或多或少就是它的样子

正如您在文件 home.route.ts.

中看到的那样,主页组件对任何人开放
export const HOME_ROUTE: Route = {
  path: '',
  component: HomeComponent,
  data: {
    authorities: [], // <- Empty, so anyone can access the home
    pageTitle: 'home.title'
  }
};

另一方面,如果您想在常规组件中授予对新角色的访问权限,则必须将其添加到 [entity-name].route.ts.

中的有效权限数组中
export const fooRoute: Routes = [
  {
    ...
    data: {
      authorities: ['ROLE_STUDENT', 'ROLE_PROFESOR'],
      ...
    },
...

这允许任何拥有 ROLE_STUDENTROLE_PROFESOR 的用户访问,但不包括普通用户(只有 ROLE_USER)。这只是一个例子。

无论如何,如果我正确理解了您的问题,那么您是在尝试直接在浏览器中访问 api/... 映射。这不是一个好主意,它失败是件好事,因为客户端通常会向大多数请求添加内容,以便服务器正确处理和验证它们(XSRFauth token、...)。