代码之家  ›  专栏  ›  技术社区  ›  Lewis Godgiven

更新JHipster应用程序中OAuth令牌的过期日期

  •  0
  • Lewis Godgiven  · 技术社区  · 8 年前

    我使用Angular4/Spring处理JHipster生成的应用程序。

    当我登录应用程序时,我可以调用API 1800秒。 然而,当我运行一个请求时,我的令牌的到期日期应该重置,并且在这之后我不应该断开连接。

    在我的桌子上 oauth_client_details ,我有字段 access_token_validity refresh_token_validity 每台1800。

    是否有其他设置以正确更新令牌?

    2 回复  |  直到 8 年前
        1
  •  0
  •   Lewis Godgiven    8 年前

    下面是使用刷新令牌刷新会话持续时间的技巧。

    在里面 auth-oauth2。服务ts 代替 authSuccess() 函数并添加 刷新()

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

    请记住修改 注销() 和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();
        });
    }
    
        2
  •  0
  •   hizmarck    5 年前

    我和JHipster发电机一起工作 4.6.0 ,如果对我进行这些更改的人有效 应用yml公司 ,并为我工作。

    jhipster:
        security:
            authentication:
                oauth:
                    # Token is valid 1 day
                    token-validity-in-seconds: 86400
    
    推荐文章