java经过RestTemplate接口调用Harbor接口的Basic认证

package harbor.harbor;java

import java.nio.charset.Charset;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;web

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;spring

import com.alibaba.fastjson.JSONObject;json

@SpringBootApplication
public class HarborApplication {api

    public static void main(String[] args) {
        SpringApplication.run(HarborApplication.class, args);
        
        RestTemplate template = new RestTemplate();
        
        //1./health(GET)
        String health = template.getForObject("http://172.27.137.241/api/health",String.class);        
        //System.out.println("harbor状态:"+health);
        
        
        //2./search  Search for projects, repositories and helm charts(GET)
        Map proSearchMap = new HashMap<>();
        proSearchMap.put("q","emcass");
        
        String search = template.getForObject("http://172.27.137.241/api/search",String.class,proSearchMap);                    
        //System.out.println("根据项目的名称查找项目的相关信息:"+search);
        
        //3./projects List project(GET)
        String proList = template.getForObject("http://172.27.137.241/api/projects",String.class);                    
        //System.out.println("列出全部项目的列表:"+proList);
        
        //4.​/projects 判断项目是否存在
        Map proExistMap = new HashMap<>();
        proExistMap.put("project_name","emcass");
        String proExist = template.getForObject("http://172.27.137.241/api/projects",String.class,proExistMap);                    
        //System.out.println("判断项目是否存在:"+proExist);
    
        
        //5.建立项目
        HashMap<String, Object> createProMap = new HashMap<>();
        
        JSONObject proObj = new JSONObject();
        proObj.put("project_name", "emcasb");
        JSONObject metaObj = new JSONObject();
        metaObj.put("public", "true");
        metaObj.put("enable_content_trust", "true");
        metaObj.put("prevent_vul", "true");
        metaObj.put("severity", "low");
        metaObj.put("auto_scan", "true");
        proObj.put("metadata", metaObj);
        createProMap.put("project", "{ \"project_name\": \"emcasb\", \"metadata\": { \"public\": \"true\", \"enable_content_trust\": \"true\", \"prevent_vul\": \"true\", \"severity\": \"low\", \"auto_scan\": \"true\" }}");
        
        /*HttpHeaders headers = createHeaders("admin","Harbor12345");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");          
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, headers);*/
        
        MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<String, String>();
        requestBody.add("project", proObj.toJSONString());
       
      
        HttpHeaders headers = createHeaders("admin","Harbor12345");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");app

        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<MultiValueMap<String, String>>(requestBody, headers);
        
        HttpEntity<String> entity = new HttpEntity<String>(proObj.toJSONString(), headers);
        ResponseEntity<String> response = template.postForEntity("http://172.27.137.241/api/projects", entity, String.class);post

        //ResponseEntity<String> response = template.exchange("http://172.27.137.241/api/projects",HttpMethod.POST, httpEntity, String.class);
        if(response.getStatusCode().is2xxSuccessful()){
            
            System.out.println("想项目建立成功");                    
        
        }else if(response.getStatusCode().is5xxServerError()){
            
            System.out.println("服务端异常");    
        }else if(response.getStatusCode().is4xxClientError()){
            
            System.out.println("客户端异常");    
        }
    
    }
    
     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);                                 
                }
            };        
     }
}
 code