Salut,

J'ai fait une connexion oauth Spring qui a réussi.
Mon souci est de récupérer et afficher le 'username' à partir du "User Profile" que j'obtiens dans ma console. ci-jointe photo

Nom : photoSuccess.png
Affichages : 312
Taille : 154,7 Ko

Voici ma classe authentification.ts

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
 
import { Injectable } from '@angular/core';
import {Router} from "@angular/router";
import {JwksValidationHandler, OAuthService, OAuthStorage} from "angular-oauth2-oidc";
import {authConfig} from "./authConfig";
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {CookieService} from "ngx-cookie-service";
import {User} from "./User";
import {Observable} from "rxjs";
 
@Injectable({
  providedIn: 'root'
})
 
 
export class AuthentificationService {
 
  host:string = 'http://localhost:8080/edasta';
 
  public isAuthentificated: boolean;
 
  username : string;
  roles: Array<string>;
 
  user : User;
 
  constructor(
 
    private router: Router,
    private oauthService : OAuthService,
    private _http: HttpClient,
    public Cookie: CookieService
 
  )
{ }
 
 
    private ConfigureImplicitFlowAuthentication() {
 
      this.oauthService.configure(authConfig);
 
      this.oauthService.tokenValidationHandler = new JwksValidationHandler();
 
      this.oauthService.setupAutomaticSilentRefresh();
 
     this.oauthService.loadDiscoveryDocument().then(()=>{
       this.oauthService.tryLogin({
         onTokenReceived: (info)=>{
           this.router.navigate([info.state]);
         }
       })
     })
 
  }
 
 
  obtainAccess(data) {
 
 
      let params = new URLSearchParams();
 
        params.append('grant_type', 'password');
        params.append('username', data.username);
        params.append('password', data.password);
        params.append('scope','openid');
        params.append('client_id', 'eddy');
 
 
        const headers = new HttpHeaders(
          {
            'Content-type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'XMLHttpRequest',
            'Authorization': 'Basic '+btoa("eddy" +':'  + "secret")
          }
        )
 
 
        let options = {
          headers: headers
        };
 
       this._http.post(this.host + "/oauth/token",
          params.toString(), options)
          .subscribe(
 
            user  => {
 
              this.saveToken(user)
 
            },
            error  => {
 
              alert("Authentification invalide");
 
            });
  }
 
 
 
 
  saveToken(token){
 
 
    var expireDate = new Date().getTime() + (1000 * token.expires_in);
 
 
    this.Cookie.set("access_token", token.access_token, expireDate);
 
    localStorage.setItem("access_token",token.access_token);
 
 
  }
 
 
  loadMainMenu(){
 
    if(localStorage.getItem('access_token')){
 
      this.isAuthentificated = true;
      this.router.navigateByUrl('menu');
    }
    else {
 
      this.isAuthentificated = false;
      this.router.navigateByUrl('/login');
    }
 
 
  }
 
 
 
 
 
 
 
  getUsers() {
 
 
    var headers = new HttpHeaders({
      'Content-type': 'application/x-www-form-urlencoded; charset=utf-8',
      'Authorization': 'Bearer '+ this.Cookie.get('access_token')
    });
 
    let options = {
      headers: headers
    };
 
    return this._http.get(
      this.host + '/me',options)
      .subscribe(
        user=> {
 
          console.log(user);
          this.user = user;
 
 
        },
        error1 => {
         alert(error1);
        }
      )
 
  }
 
 
 
 
 
 
 
getUserById(id: number) {
 
 return this._http.get(
   this.host + '/personnes/' + id + '?token=' + (localStorage.getItem('access_token')));
 
 
 
}
 
 
 
 
getResourse() {
 var headers = new HttpHeaders({
   'Content-type': 'application/x-www-form-urlencoded; charset=utf-8',
   'Authorization': 'Bearer '+ this.Cookie.get('token')
 });
 
 let options = {
   headers: headers
 };
 
 return this._http.get(this.host +'/me', options)
   .subscribe(
     data=> {
       console.log('asta');
     },
     error1 => {
       console.log('erreur')
     }
   )
 
 
}
 
 
 
 
checkCredentials(){
 if (!this.Cookie.check('access_token')){
   this.router.navigate(['/login']);
 }
}
 
logout() {
 this.Cookie.delete('access_token');
 this.router.navigate(['/login']);
}
 
 
 
 
}

Merci de m'aider à récupérer et afficher à partir de username