更新 JHipster 应用程序中 OAuth 令牌的到期日期
Update expiration date of a OAuth Token in a JHipster App
我使用 Angular4/Spring 开发 JHipster 生成的应用程序。
当我登录应用程序时,我可以调用 API 1800 秒。
然而,当我 运行 一个请求时,我的令牌的到期日期应该被重置,我不应该在这个时间之后断开连接。
在我的 table oauth_client_details
中,我的字段 access_token_validity
和 refresh_token_validity
都在 1800 上。
是否还有其他设置可以正确更新令牌?
这里有一个使用刷新令牌刷新会话持续时间的技巧。
在auth-oauth2.service.ts中,替换authSuccess()函数并添加一个refresh() 一个。
authSuccess(resp) {
const response = resp.json();
const expiredAt = new Date();
expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in);
response.expires_at = expiredAt.getTime();
this.$localStorage.store('authenticationToken', response);
if (this.refreshSubcription !== null) {
// cancel previous refresh
this.refreshSubcription.unsubscribe();
}
// refresh token 5 seconds before expiration
this.refreshSubcription = Observable
.timer((response.expires_in - 5) * 1000 )
.take(1)
.subscribe(() => this.refresh());
return response;
}
refresh() {
const data = 'refresh_token=' + this.getToken().refresh_token + '&grant_type=refresh_token&scope=read%20write&' +
'client_secret=<SECRET-TOKEN>&client_id=<CLIENT-ID>';
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': 'Bearer ' + this.getToken().access_token
});
this.http
.post('oauth/token', data, {headers})
.map(this.authSuccess.bind(this))
.subscribe();
}
记得相应地修改 logout() 和 login() 方法。
login(credentials): Observable<any> {
const data = 'username=' + encodeURIComponent(credentials.username) + '&password=' +
encodeURIComponent(credentials.password) + '&grant_type=password&scope=read%20write&' +
'<SECRET-TOKEN>&client_id=<CLIENT-ID>';
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': 'Basic ' + this.base64.encode('<CLIENT-ID>' + ':' + '<SECRET-TOKEN>')
});
return this.http
.post('oauth/token', data, {headers})
.map(this.authSuccess.bind(this));
}
logout(): Observable<any> {
if (this.refreshSubcription !== null) {
// cancel previous refresh
this.refreshSubcription.unsubscribe();
}
return new Observable((observer) => {
this.http.post('api/logout', {});
this.$localStorage.clear('authenticationToken');
observer.complete();
});
}
我使用 JHipster 生成器 4.6.0,如果这对我有用,我在 application.yml 中做了这些更改,并为我工作。
jhipster:
security:
authentication:
oauth:
# Token is valid 1 day
token-validity-in-seconds: 86400
我使用 Angular4/Spring 开发 JHipster 生成的应用程序。
当我登录应用程序时,我可以调用 API 1800 秒。 然而,当我 运行 一个请求时,我的令牌的到期日期应该被重置,我不应该在这个时间之后断开连接。
在我的 table oauth_client_details
中,我的字段 access_token_validity
和 refresh_token_validity
都在 1800 上。
是否还有其他设置可以正确更新令牌?
这里有一个使用刷新令牌刷新会话持续时间的技巧。
在auth-oauth2.service.ts中,替换authSuccess()函数并添加一个refresh() 一个。
authSuccess(resp) {
const response = resp.json();
const expiredAt = new Date();
expiredAt.setSeconds(expiredAt.getSeconds() + response.expires_in);
response.expires_at = expiredAt.getTime();
this.$localStorage.store('authenticationToken', response);
if (this.refreshSubcription !== null) {
// cancel previous refresh
this.refreshSubcription.unsubscribe();
}
// refresh token 5 seconds before expiration
this.refreshSubcription = Observable
.timer((response.expires_in - 5) * 1000 )
.take(1)
.subscribe(() => this.refresh());
return response;
}
refresh() {
const data = 'refresh_token=' + this.getToken().refresh_token + '&grant_type=refresh_token&scope=read%20write&' +
'client_secret=<SECRET-TOKEN>&client_id=<CLIENT-ID>';
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': 'Bearer ' + this.getToken().access_token
});
this.http
.post('oauth/token', data, {headers})
.map(this.authSuccess.bind(this))
.subscribe();
}
记得相应地修改 logout() 和 login() 方法。
login(credentials): Observable<any> {
const data = 'username=' + encodeURIComponent(credentials.username) + '&password=' +
encodeURIComponent(credentials.password) + '&grant_type=password&scope=read%20write&' +
'<SECRET-TOKEN>&client_id=<CLIENT-ID>';
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': 'Basic ' + this.base64.encode('<CLIENT-ID>' + ':' + '<SECRET-TOKEN>')
});
return this.http
.post('oauth/token', data, {headers})
.map(this.authSuccess.bind(this));
}
logout(): Observable<any> {
if (this.refreshSubcription !== null) {
// cancel previous refresh
this.refreshSubcription.unsubscribe();
}
return new Observable((observer) => {
this.http.post('api/logout', {});
this.$localStorage.clear('authenticationToken');
observer.complete();
});
}
我使用 JHipster 生成器 4.6.0,如果这对我有用,我在 application.yml 中做了这些更改,并为我工作。
jhipster:
security:
authentication:
oauth:
# Token is valid 1 day
token-validity-in-seconds: 86400