java操作 harbor api 创建用户 用户管理



import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author: li
 * @Date: 2020/12/2/002 15:15
 * @describe
 */

public class HarborUtil {

    private final static String harborUsername = "admin";
    private final static String harborPassword = "Aa123456";
    private final static String harborUrl = "http://XXXXX:XXX/api/v2.0";

    public static Map getUser() {
        long userId = System.currentTimeMillis();
        Map userMap = new HashMap();
        userMap.put("username", "lma-" + userId);
        userMap.put("password", userId + RandomString.generateString(new Random(), 10));
        userMap.put("realname", "lma");
        userMap.put("role_id", 2);
        userMap.put("email", userId + "@zhuojianchina.com");

        RestTemplate template = new RestTemplate();
        HttpHeaders headers = createHeaders(harborUsername, harborPassword);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        HttpEntity<String> entity = new HttpEntity<>(JSONObject.toJSONString(userMap), headers);
        ResponseEntity<String> response = template.postForEntity(harborUrl + "/users", entity, String.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            return userMap;
        }
        return null;
    }

    public static void deleteUser(String userId) {
        RestTemplate template = new RestTemplate();
        HttpHeaders headers = createHeaders(harborUsername, harborPassword);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        HttpEntity<String> entity = new HttpEntity<>(headers);
        template.exchange(harborUrl + "/users/" + userId, HttpMethod.DELETE, entity, String.class);
    }

    public static String getUserId(Map user) {
        RestTemplate template = new RestTemplate();
        HttpHeaders headers = createHeaders(user.get("username").toString(), user.get("password").toString());
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        HttpEntity<String> entity = new HttpEntity<>(headers);
        HttpEntity<String> response = template.exchange(harborUrl + "/users/current", HttpMethod.GET, entity, String.class);
        return JSONObject.parseObject(response.getBody()).getString("user_id");
    }

    public static HttpHeaders createHeaders(String username, String password) {
        return new HttpHeaders() {
            {
                String auth = username + ":" + password;
                String authHeader = "Basic " + Base64.getEncoder().encodeToString(auth.getBytes());
                set("Authorization", authHeader);
            }
        };
    }


    public static void main(String[] args) {
        Map user = getUser();
        String user_id = getUserId(user);
        deleteUser(user_id);
    }
}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值