使用 Typescript JsonServiceClient 跨多个域使用 JWT 令牌 - ServiceStack [英] Use the JWT tokens across multiple domains with Typescript JsonServiceClient - ServiceStack

查看:47
本文介绍了使用 Typescript JsonServiceClient 跨多个域使用 JWT 令牌 - ServiceStack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在获得对 this SO question,我意识到我有一个跨域问题,httponly 标记的 ServiceStack bearerToken Cookie 没有发送到我在不同域上的资源微服务.

After getting answers to this SO question, I realized that I have a cross-domain issue with the httponly flagged ServiceStack bearerToken Cookie not being sent to my resource microservices on different domains.

ServiceStack 的文档在他们的文档中解释了如何解决这个问题 这里.

ServiceStack's documentation explains how to solve this problem in their documentation here.

我在实现 ServiceStack 文档中代码示例的 Typescript 版本时遇到问题.我猜文档中的示例代码是 C#.

I having trouble implementing a Typescript version of the code sample in ServiceStack's documentation. I am guess that the sample code in the documentation is C#.

这是我的带有注释的 Typescript 代码:

Here is my Typescript code with comments:

import { JsonServiceClient, IReturn } from 'servicestack-client';
import { AuthService } from './auth.service';
//dtos generated by myauthservice/types
import { GetAccessToken, ConvertSessionToToken, ConvertSessionToTokenResponse } from './dtos'
import { AppModule } from '../../app.module';
import { Router } from '@angular/router';

export class JsonServiceClientAuth extends JsonServiceClient {

    private router: Router;
    private authClient: JsonServiceClient;

    constructor(baseUrl: string) {
        console.log('JsonServiceClientAuth.baseUrl', baseUrl);
        super(baseUrl);
        //Router is not injected via the contructor because clients of JsonServiceClientAuth need to create instances of it
        this.router = AppModule.injector.get(Router);
        this.authClient = new JsonServiceClient("http://localhost:5006");

        this.onAuthenticationRequired = async () => {
            console.log('JsonServiceClientAuth.onAuthenticationRequired()');
            console.log('An API is not receiving the bearerToken being redirected to Login');
            this.router.navigate(['/login']);
        };
    }

    get<T>(request: IReturn<T> | string, args?: any): Promise<T> {
        return new Promise<T>((resolve, reject) => {

            //cross domain issue here, ss-tok httponly Cookie will not be sent to the Resource Service
            //ConvertSessionToToken per Service Stacks documentation: http://docs.servicestack.net/jwt-authprovider#converting-an-existing-authenticated-session-into-a-jwt-token

            //but send expects 2-4 argumments, one being the method name
            //the Typecript send will not use the dto ConvertSessionToToken request and get the url
            //var tokenResponse = this.send(new ConvertSessionToToken())

            console.log('this.bearToken', this.bearerToken); //undefined
            //so try JsonServiceClient.get
            this.authClient.get(new ConvertSessionToToken())
                .then(res => {
                    console.log('ConvertSessionToToken.res', res); 
                    console.log('this.bearToken', this.bearerToken); //undefined
                    //next problem there is no function getTokenCookie in Typescript that I can find, not in the dtos and not in JsonServiceClient
                    //var jwtToken = this.getTokenCookie(); //From ss-tok Cookie

                    //maybe this will work?
                    //super.setBearerToken(this.bearerToken)
                    super.get(request).then(res => {
                        console.log('suger.get.res', res);
                        resolve(res);
                    }, msg => {
                        console.log('suger.get.msg', res);
                        reject(msg)
                    })
                    .catch(ex => {
                        console.log('suger.get.ex', ex);
                        reject(ex);
                    });
                }, msg => {
                    console.log('ConvertSessionToToken.msg', msg);
                    reject(msg);
                })
                .catch(ex =>{
                    console.log('ConvertSessionToToken.ex', ex);
                    reject(ex);
                });
        });
    }

}

推荐答案

为什么要实现自定义 JsonServiceClient?只需在现有的 JsonServiceClient 上使用现有的 bearerTokenrefreshToken 属性,这些属性已经内置支持发送 JWT 承载令牌和使用刷新令牌重新获取过期的 JWT 令牌.

Why are you implementing a Custom JsonServiceClient? Just use the existing bearerToken and refreshToken properties on the existing JsonServiceClient which already has built-in support for sending JWT Bearer Tokens and using Refresh Tokens to re-fetch expired JWT Tokens.

您可以查看client.auth.spec.ts 获取支持行为的示例.

You can check out client.auth.spec.ts for examples of supported behavior.

这篇关于使用 Typescript JsonServiceClient 跨多个域使用 JWT 令牌 - ServiceStack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆