【问题标题】:Update expiration date of a OAuth Token in a JHipster App更新 JHipster 应用程序中 OAuth 令牌的到期日期
【发布时间】:2018-06-14 07:45:21
【问题描述】:

我使用 Angular4/Spring 开发 JHipster 生成的应用程序。

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

在我的表 oauth_client_details 中,我的字段 access_token_validityrefresh_token_validity 各有 1800 个。

是否需要进行其他设置才能正确更新令牌?

【问题讨论】:

  • 这不是 Oauth 的工作方式。您的访问令牌将在给定的时间内工作,然后您将需要使用刷新令牌来请求新的访问令牌。
  • 哦,好吧,我明白了,但我很惊讶 jhipster 中没有设置这个过程。我可以在数据库中看到刷新令牌,但它似乎未使用
  • 您使用的是什么版本的 JHipster?单体还是微服务?这一切都应该由 Spring Security OAuth 处理(因为我们在 2017 年 9 月重构了所有内容)。
  • 我使用 4.7(从 2017 年 9 月开始!)生成一个 Monolith 应用程序。前端没有使用刷新令牌来延长会话的持续时间。我发布了我用来解决问题的代码,也许它在最新版本中不再相关。

标签: oauth oauth-2.0 token jhipster access-token


【解决方案1】:

我使用 JHipster 生成器 4.6.0,如果这适用于某人,我在 application.yml 中进行了这些更改,并且适用于我。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 2015-11-26
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 2017-12-01
      • 2015-02-25
      • 2011-03-05
      相关资源
      最近更新 更多