如何在插件 REST Spring Atlassian Connect API 中实现 JWT 授权?

How to implement JWT authorization in addon REST Spring Atlassian Connect API?

我使用

创建了插件
mvn archetype:generate -DarchetypeGroupId=com.atlassian.connect -DarchetypeArtifactId=atlassian-connect-spring-boot-archetype -DarchetypeVersion=1.5.1

atlassian-connect.json

{...
 "scopes": [
 "read", "write"
],
"authentication": {
 "type": "jwt"
 },
 "lifecycle": {
 "installed": "/installed",
 "uninstalled": "/uninstalled"
 },
 "enableLicensing": false,
 "modules": {
  "generalPages": [

  {
    "key": "comments",
    "location": "system.top.navigation.bar",
    "name": {
      "value": "Comments"
    },
    "url": "/rest/api",
    "conditions": [{
      "condition": "user_is_logged_in"
    }]
  }
]
  }
}

我正在尝试访问我的 API

 AJS.$.ajax({
                    url: "https://X.ngrok.io/rest/api",
                    type: "GET",
                    dataType: "json",
                    contentType: "application/json",
                    async: false, headers: {
    'Authorization' : "JWT {{sessionToken}}"
},
                    success: function (data) {
                        console.log(e);
                    },
                    error: function(response) {

                        console.log(response);
                    }
                    })

如何在JS端获取jwt(最好描述一下

简单的插件一步一步,如果你可以的话(我检查了很多链接的例子,但是......))?

您正在 atlassian-connect.json 文件中声明一个 HTML 页面,即 'comments' 通用页面。因此,如果有人打开此页面,您可以生成一个 JWT 并将其注入 HTML 文档 ,然后 将其返回给 user/requester。然后你可以在你的 JavaScript 代码中使用这个 JWT。由于您似乎正在使用 Spring 引导模板,因此您应该查看 repositories readme file 中的 "Authenticating requests from iframe content back to the add-on" 部分。这正是描述你的情况。

另一种选择是根据您从 Jira 或 Confluence 等 Connect 应用程序接收的 JWT 生成 JWT。但是,这需要做更多的工作。我可以推荐阅读有关如何获得 a valid JWT from the Connect application within JavaScript and also how the installation handshake phase 作品的信息。基于这些,您可以生成自己的 JWT。

但是,在所有情况下,请注意您不会为每个请求生成新令牌,而是为会话生成新令牌,因此您可以将令牌用于多个请求。我建议选择选项 a),因为 Sprint Boot Connect 模板已经支持它。