首页 > 解决方案 > Jhipster 新角色 - 需要完全身份验证

问题描述

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

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

然后我登录管理员帐户并尝试并成功创建了一个新用户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课堂上,这个网址属于.antMatchers("/api/**").authenticated(). 所以我应该能够从任何帐户访问它,只要我登录。

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

标签: javaspring-bootspring-securityjhipsterroles

解决方案


您还必须打开通往新角色的路线,这是在客户端完成的。如果您使用角度,这或多或少是它的外观。

如您在文件中所见,home 组件对任何人开放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_STUDENT或的用户访问ROLE_PROFESOR,但不是普通用户(只有ROLE_USER)。这只是一个例子。

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


推荐阅读