我有一个 Spring Boot 应用程序,Angular 前端与 Auth0 集成。安全工作正常。但我想在 spring 端收到用户电子邮件,
我在端点中添加了以下代码
@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 端登录电子邮件地址? 为什么 Angular 请求会给出空指针?
谢谢
编辑:
关注以下文章
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]
},
app.module
httpInterceptor: {
allowedList: [
{
uri: '/*',
tokenOptions: {
audience: 'http://localhost:8080',
scope: 'view:account',
},
},
],
我使用代理配置将角度端点 (4200) 映射到任何 /server 的 spring (8080),即返回 this.http.get('/server/api/v1/...')
所以无法识别正确的uri(而不是uri:'/*',)
有什么想法吗?
答案 0 :(得分:0)
ok 发现问题了。 http.get 具有旧模型(已删除)
{headers: new HttpHeaders().set('Authorization', 'Bearer ' + token)}
uri 现在接受 uri: '/server/api/..*' matching url in the call this.http.get(url)