【问题标题】:Jhipster new Roles - Full authentication is requiredJhipster 新角色 - 需要完整身份验证
【发布时间】:2020-06-30 09:31:06
【问题描述】:

我在使用自定义角色登录帐户时无法访问任何网址。

为了为我的应用创建新角色,我在 AuthoritiesConstants 类和 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 类中,此网址属于.antMatchers("/api/**").authenticated()。所以我应该能够从任何帐户访问它,只要我登录。

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

【问题讨论】:

    标签: java spring-boot spring-security jhipster roles


    【解决方案1】:

    您也必须打开通往新角色的路线,这是在客户端完成的。这或多或少是使用 angular 时的样子。

    如您在文件home.route.ts 中所见,home 组件对任何人开放。

    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,...)。

    【讨论】:

    • 我的印象是问题是 Spring 安全问题,而不是角度问题。现在我明白了。感谢您的回答和您的时间!
    猜你喜欢
    • 2020-06-08
    • 2016-09-23
    • 2016-07-21
    • 2015-01-20
    • 2015-10-10
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多