简体   繁体   English

如何在 Angular 中获取 email 声明 - Spring Boot - Auth0

[英]How to get email claim in Angular - Spring Boot - Auth0

I have a Spring boot app, with Angular frontend integrated with Auth0.我有一个 Spring 启动应用程序,Angular 前端与 Auth0 集成。 The security is working ok.安全工作正常。 But I want to get user email on spring side,但我想在 spring 端获取用户 email,

I added below code in an endpoint我在端点中添加了以下代码

@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());
    }

SCENARIO 1: Client is Postman.场景1:客户是 Postman。

scope = openid profile email scope = openid 配置文件 email

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

Log:日志:

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

SCENARIO 2:情景 2:

Testing from angular gives null pointer (jwt) at line从 angular 进行的测试在行给出了 null 指针(jwt)

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

It seems something is missing in angular request. angular 请求中似乎缺少某些内容。 Not sure what it is.不知道它是什么。

Some angular code:一些 angular 代码:

  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);
  }

Question: How to log email address on spring side?问:如何在spring端登录email地址? Why angular request is giving null pointer?为什么 angular 请求给出 null 指针?

Thanks谢谢

EDIT:编辑:

Followed the below article按照下面的文章

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

But I'm having issue in identifying my uri但我在识别我的 uri 时遇到问题

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

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

app-routing has应用程序路由

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

app.module应用模块

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

    ],

I use proxy config to map angular endpoints (4200) to spring (8080) for any /server ie return this.http.get('/server/api/v1/...')我使用代理配置到 map angular 端点 (4200) 到 spring (8080) 对于任何 /server 即返回 this.http.get('/server/api/v1/...')

So unable to identify the right uri ( instead of uri: '/*',)所以无法识别正确的 uri(而不是 uri: '/*',)

Any idea?任何的想法?

ok found the issue.好的,找到了问题。 http.get was having legacy model (removed it) http.get 有遗留的 model(删除它)

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

uri now accespts uri: '/server/api/..*' matching url in the call this.http.get( url ) uri 现在接受 uri: '/server/api/..*' 在调用 this.http.get( url ) 中匹配url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM