Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform JWT令牌谷歌云运行_Google Cloud Platform_Jwt_Google Authentication_Google Cloud Run_Google Api Gateway - Fatal编程技术网

Google cloud platform JWT令牌谷歌云运行

Google cloud platform JWT令牌谷歌云运行,google-cloud-platform,jwt,google-authentication,google-cloud-run,google-api-gateway,Google Cloud Platform,Jwt,Google Authentication,Google Cloud Run,Google Api Gateway,我正在谷歌云平台上开发一个具有JWT认证的应用程序。服务器端我通过云API网关向云运行后端添加了身份验证。现在我正在创建一个客户端来生成JWT令牌并在调用中传递它。为此,我正在创建一个必须部署在CloudRun上的应用程序,并遵循以下文档:。我的问题是,我不知道如何指出它作为saKeyfile需要什么。我试图只将文件名放在src/main/resources/filetest.json下,但一旦我尝试调用该方法,它就会告诉我找不到文件。我还试图指出完整的路径。有人能帮我吗 PS我正在使用Java

我正在谷歌云平台上开发一个具有JWT认证的应用程序。服务器端我通过云API网关向云运行后端添加了身份验证。现在我正在创建一个客户端来生成JWT令牌并在调用中传递它。为此,我正在创建一个必须部署在CloudRun上的应用程序,并遵循以下文档:。我的问题是,我不知道如何指出它作为saKeyfile需要什么。我试图只将文件名放在src/main/resources/filetest.json下,但一旦我尝试调用该方法,它就会告诉我找不到文件。我还试图指出完整的路径。有人能帮我吗

PS我正在使用Java

编辑: 这是我的代码,与文档相同

 public void makeCall() {
    String fullPath="src/main/resources/TEST1-id.json";
    String saEmail="testsa@projectID.iam.gserviceaccount.com";
    String audience="auth";
    int expiryLenght=600;
    String token;
    try {
        token=generateJwt(fullPath,saEmail,audience,expiryLenght);
        System.out.println("Token generated: "+token);
        URL url = new URL("apigatewayurl");
        makeJwtRequest(token, url);
        System.out.println("Call performed");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static String generateJwt(final String saKeyfile, final String saEmail,
                                  final String audience, final int expiryLength)
        throws FileNotFoundException, IOException {

    Date now = new Date();
    Date expTime = new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expiryLength));

    // Build the JWT payload
    JWTCreator.Builder token = JWT.create()
            .withIssuedAt(now)
            // Expires after 'expiraryLength' seconds
            .withExpiresAt(expTime)
            // Must match 'issuer' in the security configuration in your
            // swagger spec (e.g. service account email)
            .withIssuer(saEmail)
            // Must be either your Endpoints service name, or match the value
            // specified as the 'x-google-audience' in the OpenAPI document
            .withAudience(audience)
            // Subject and email should match the service account's email
            .withSubject(saEmail)
            .withClaim("email", saEmail);

    // Sign the JWT with a service account
    FileInputStream stream = new FileInputStream(saKeyfile);
    ServiceAccountCredentials cred = ServiceAccountCredentials.fromStream(stream);
    RSAPrivateKey key = (RSAPrivateKey) cred.getPrivateKey();
    Algorithm algorithm = Algorithm.RSA256(null, key);
    return token.sign(algorithm);
}
我尝试使用示例中的完整路径,并且只使用/TEST1-id.json

这里是项目结构。是一个springboot应用程序,我将在云运行中部署它


OP通过这种方式解决了这个问题

最后,我将文件放在根目录中,并将其复制到docker映像中,然后在cloud run中将其作为环境变量恢复


你能分享更多关于你的代码、你的架构和你执行的测试吗?嗨@guillaumeblaquiere,我编辑了帖子:)如果你部署在云上运行,为什么你需要SA文件?我需要SA文件来检索和生成jwt令牌,以验证到另一个api网关,该网关只能从该服务帐户访问。如果有别的办法,请告诉我是的,我试过了。最后,我将文件放在根目录中,并将其复制到docker映像中,然后在cloud run中将其作为环境变量恢复