简体   繁体   English

如何使用 API 密钥访问 gradle 中工件上的 maven 存储库?

[英]How to access a maven repo on artifactory in gradle using API key?

We have a JFrog artifactory that contains a maven repo (and other elements).我们有一个 JFrog 工件,其中包含一个 maven 存储库(和其他元素)。

In my gradle setup, I defined this:在我的 gradle 设置中,我定义了这个:

repositories {
    maven {
        url = uri("https://eu.artifactory....../whatever/")    
        credentials {
            username = "my-full-user-id"
            password = "my-real-password"
        }
    }
}

Works fine.工作正常。 It also works when I change the password entry to use my JFrog API KEY .当我更改密码条目以使用我的 JFrog API KEY时,它也可以工作。

But: using the api key only works when username contains my real user id.但是:使用 api 密钥仅在username包含我的真实用户 ID 时才有效。

Whereas: when making https requests "directly" to JFrog (for example using a python script), it is not required to provide a user name: simply having the API KEY in the request header is enough. Whereas: when making https requests "directly" to JFrog (for example using a python script), it is not required to provide a user name: simply having the API KEY in the request header is enough.

Thing is: in our setup, we have the API key at hand, but not the user id.问题是:在我们的设置中,我们手头有 API 密钥,但没有用户 ID。 So I had hoped to be able to define a maven dependency in gradle that works without user names.所以我希望能够在没有用户名的情况下在 gradle 中定义一个 maven 依赖项。

But when但当

  • I leave username blank in my gradle setup, gradle complains about it being null.我在 gradle 设置中将用户名留空,gradle 抱怨它是 null。
  • I try anything else but my real user id, I get a 401 Unauthorized response除了我的真实用户 ID 外,我尝试其他任何方法,我得到401 Unauthorized响应

Question: is it possible to use "maven style" repository definition in gradle that works with API KEYS and that doesn't need the corresponding user ids?问题:是否可以在 gradle 中使用与 API KEYS 一起使用且不需要相应用户 ID 的“maven 风格”存储库定义?

Artifactory supports header based authentication where you provide the API key in the header X-JFrog-Art-Api (see Artifactory REST API ). Artifactory supports header based authentication where you provide the API key in the header X-JFrog-Art-Api (see Artifactory REST API ). I do not have an Artifactory instance at hand to test, but Gradle's HTTP header authentication should do the trick:我手头没有要测试的 Artifactory 实例,但 Gradle 的HTTP header 身份验证应该可以解决问题:

repositories {
    maven {
        url = uri("http://repo.mycompany.com/maven2")
        credentials(HttpHeaderCredentials::class) {
            name = "Private-Token"
            value = "TOKEN"
        }
        authentication {
            create<HttpHeaderAuthentication>("header")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM