Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

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).

In my gradle setup, I defined this:

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.

But: using the api key only works when username contains my real user 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.

Thing is: in our setup, we have the API key at hand, but not the user id. So I had hoped to be able to define a maven dependency in gradle that works without user names.

But when

  • I leave username blank in my gradle setup, gradle complains about it being null.
  • I try anything else but my real user id, I get a 401 Unauthorized response

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?

question from:https://stackoverflow.com/questions/66061464/how-to-access-a-maven-repo-on-artifactory-in-gradle-using-api-key

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...