Why Can not I log in to hikvision ip camera with spring boot

38 Views Asked by At

We have an IP address for a camera by providing its IP address, along with a username and password on a HikVision web page each time we access the camera. I've been tasked with finding an alternative solution using Spring Boot to interact with HikVision. In this approach, we'll send the username and password with each request when making a call via spring boot so normal user will not send username and password our spring boot code will do it . However, I'm currently struggling to find a way to make this happen i tried Chatgpt to find a solution with no luck

so here is my code

   @RestController
@Log4j2
public class HikVision {
    @Autowired
    private RestTemplate restTemplate;

 @GetMapping("/camera/live-feed")
    public ResponseEntity<byte[]> getLiveFeed() {
        HttpHeaders headers = new HttpHeaders();
        headers.setBasicAuth(USERNAME, PASSWORD);
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);

        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(HIKVISION_CAMERA_URL, HttpMethod.GET, requestEntity, byte[].class);
        return ResponseEntity.status(responseEntity.getStatusCode()).body(responseEntity.getBody());
    }

@Configuration
public class RestTemplateConfig {

  

    @Bean
    public RestTemplate restTemplate() {
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(HIKVISION_CAMERA_IP, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(USERNAME, PASSWORD));

        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

        return new RestTemplate(factory);
    }



 @RestController
@Log4j2
public class HikVision {
    @Autowired
    private RestTemplate restTemplate;

 @GetMapping("/camera/live-feed")
    public ResponseEntity<byte[]> getLiveFeed() {
        HttpHeaders headers = new HttpHeaders();
        headers.setBasicAuth(USERNAME, PASSWORD);
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);

        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(HIKVISION_CAMERA_URL, HttpMethod.GET, requestEntity, byte[].class);
        return ResponseEntity.status(responseEntity.getStatusCode()).body(responseEntity.getBody());
    }

@Configuration
public class RestTemplateConfig {

  

    @Bean
    public RestTemplate restTemplate() {
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(HIKVISION_CAMERA_IP, AuthScope.ANY_PORT),
                new UsernamePasswordCredentials(USERNAME, PASSWORD));

        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultCredentialsProvider(credentialsProvider)
                .build();

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

        return new RestTemplate(factory);
    }`your text`

expected to get live feed of camera

0

There are 0 best solutions below