首页 > 解决方案 > 如何在 Angular - Spring Boot - Auth0 中获取电子邮件声明

问题描述

我有一个 Spring Boot 应用程序,Angular 前端与 Auth0 集成。保安工作正常。但我想在春季收到用户电子邮件,

我在端点中添加了以下代码

@GetMapping(value = "/private")
public Message privateEndpoint(@AuthenticationPrincipal Jwt jwt) {
    Map<String, Object> claims = jwt.getClaims();
    for (Object key: claims.keySet()) {
        System.out.println("key: and value: "+ key.toString());// + "..."+  claims.get(key).asString());
    }

场景 1: 客户是邮递员。

范围 = openid 个人资料电子邮件

访问令牌 url = https://{{auth0_domain}}/oauth/token

日志:

key: and value: sub
key: and value: aud
key: and value: azp
key: and value: scope
key: and value: iss
key: and value: exp
key: and value: iat

场景 2:

从角度进行测试在行给出空指针(jwt)

System.out.println("headers:\n" + jwt.getHeaders());

角度请求中似乎缺少一些东西。不确定它是什么。

一些角度代码:

  responseType: 'token id_token',
    audience: 'http://localhost:8080',
    redirectUri: 'http://localhost:4200/callback',
    scope: 'openid email view:some view:another'



  private setSession(authResult): void {
    // Set the time that the access token will expire at 
    const expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
    localStorage.setItem('access_token', authResult.accessToken);
    localStorage.setItem('id_token', authResult.idToken);
    localStorage.setItem('expires_at', expiresAt);
  }

问题:如何在 spring 端记录电子邮件地址?为什么角度请求给出空指针?

谢谢

编辑:

按照下面的文章

https://auth0.com/docs/libraries/auth0-angular-spa

但我在识别我的 uri 时遇到问题

  // Matching on HTTP method
  {
    uri: '/api/orders',
    httpMethod: 'post',
    tokenOptions: {
      audience: 'http://my-api/',
      scope: 'write:orders',
    },
  },

流程:按钮有 [routerLink]="['/todo', fid,sid]

应用程序路由

{
        path: 'todo/:sId/:tId', // child route path cid/:sid/:tid
        component: ThatComponent, // child route component that the router renders
        canActivate: [AuthGuard]
      },

应用程序模块

 httpInterceptor: {
    allowedList: [                 
                   {
                     uri: '/*',
                     tokenOptions: {
                       audience: 'http://localhost:8080',
                       scope: 'view:account',
                     },
                   },

    ],

我使用代理配置将角度端点(4200)映射到任何 /server 的弹簧(8080),即返回 this.http.get('/server/api/v1/...')

所以无法识别正确的uri(而不是uri:'/ *',)

任何想法?

标签: angularspring-bootauth0

解决方案


好的,找到问题了。http.get 有遗留模型(删除它)

{headers: new HttpHeaders().set('Authorization', 'Bearer ' + token)}

uri 现在接受 uri: '/server/api/..*'在调用 this.http.get( url )中匹配url


推荐阅读