Harbor 2.0 API JAVA版本调用demo

全网搜索了下Harbor 2.0 API,几乎全是Linux curl或者python版本,java版本的很少,至少我找到的没调用成功,因为工作需要,将jar包或者前端静态文件制作成docker镜像。大厂主流早期harbor并未提供这种支持,历经一番手动传输到虚拟机,再用linux命令上传,完全是拼体力。为解决这种低效率问题,我写了个web版本的工具用来替代繁琐的手动敲命令制作、上传镜像。

1、核心Maven 依赖

<dependencies>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>

       <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>fastjson</artifactId>
           <version>1.2.38</version>
       </dependency>

       <dependency>
           <groupId>org.apache.httpcomponents</groupId>
           <artifactId>httpclient</artifactId>
      
### HarborJava API交互 Harbor作为企业级容器镜像仓库,支持多种编程语言与其API进行交互。对于Java开发者而言,可以通过调用RESTful风格的HTTP接口来实现对Harbor操作。 #### 准备工作 为了能够顺利地使用Java代码访问Harbor服务端口,默认情况下为`https://<harbor-server>:443/api/v2.0/...`,需要先完成如下准备工作: - 获取管理员账号或具有相应权限用户的用户名密码; - 如果启用了HTTPS,则需确认客户端信任服务器证书;如果采用自签名SSL/TLS证书,在发起请求前应设置忽略SSL验证逻辑[^1]。 #### Maven依赖引入 为了让项目具备发送HTTP请求的能力,推荐添加Apache HttpClient库的支持。可以在项目的pom.xml文件中加入以下依赖项: ```xml <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 --> <dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.1</version> </dependency> <!-- JSON解析工具 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency> ``` #### 登录获取Token 由于Harbor的安全机制要求每次请求都携带有效的认证信息,因此首先要执行登录动作并保存返回的身份令牌(token)。下面给出了一段简单的示例代码展示如何利用Basic Auth方式向Harbor提交凭证从而获得JWT token: ```java import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.io.entity.EntityUtils; public class LoginExample { public static void main(String[] args) throws Exception{ String url = "https://your-harbor-domain/service/token"; CloseableHttpClient client = HttpClients.createDefault(); try { HttpPost post = new HttpPost(url); // 设置header参数 post.setHeader("Content-Type", "application/json"); post.addHeader("Authorization","Basic "+ Base64.getEncoder().encodeToString(("admin:password").getBytes())); // 发送post请求 CloseableHttpResponse response = client.execute(post); System.out.println(EntityUtils.toString(response.getEntity())); } finally { client.close(); } } } ``` 请注意替换上述代码中的`your-harbor-domain`, `admin`, 和 `password`字段为你自己的实际值。这段程序会打印出由Harbor颁发给合法用户的JSON Web Token (JWT),后续其他API调用都需要带上这个token作为身份证明。 #### 查询项目列表 一旦成功获得了token之后就可以开始查询数据了。这里提供了一个例子说明怎样读取所有的project对象: ```java // 假设已经得到了之前提到的有效token字符串变量名为accessToken String getUrl = "https://your-harbor-domain/api/v2.0/projects"; CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom() .setConnectTimeout(5000).build()) .build(); HttpGet httpGet = new HttpGet(getUrl); httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken ); try(CloseableHttpResponse httpResponse = httpClient.execute(httpGet)){ int statusCode = httpResponse.getStatusLine().getStatusCode(); if(statusCode == HttpStatus.SC_OK){ String resultJsonStr = EntityUtils.toString(httpResponse.getEntity()); ObjectMapper mapper = new ObjectMapper(); List<ProjectDTO> projectsList = Arrays.asList(mapper.readValue(resultJsonStr , ProjectDTO[].class)); for(ProjectDTO p :projectsList ){ System.out.printf("%s\n",p.getName()); } } } catch(Exception e){ logger.error(e.getMessage(),e); } static class ProjectDTO{ // DTO类定义省略... } ``` 以上就是基于Java应用程序连接到Harbor实例的一些基本指导。当然还有更多高级功能等待探索,比如上传下载image、管理成员角色分配等等。具体细节可以参阅官方文档了解更全面的信息。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kakahu2015

只发干货,如果你觉得不干请别打

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值