Multipart/form-data in a POST call with BASIC AUTH in Spring Boot

Viewed 243

I need to do a POST call that sends a file (Multipart/form-data) to an API with basic auth, from a process as client, not web application, with this code:

 public static void sendFile(){
    
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders httpHeaders = createHeaders("admin","admin");
    HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders);

    ResponseEntity<String> responseEntity = restTemplate.exchange(REST_SERVICE_URL_ACCEPT_FILE,
            HttpMethod.POST, httpEntity, String.class);

But I alwais get a 401 response, the code in the server side is:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
      .antMatchers(HttpMethod.GET, "/healthCheck").permitAll()      //Fails with POST
      .antMatchers(HttpMethod.POST, "/loginOnly").authenticated()   //Works with GET
      .antMatchers(HttpMethod.POST, "/processFile").authenticated() //Cant with GET
      .anyRequest().authenticated()
      .and()
      .httpBasic();
}


@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
      .withUser("admin").password(passwordEncoder().encode("admin"))
      .authorities("ROLE_ADMIN");
}

How could I call the API? Testing with Postman, I also get the 401 response. Can I be able to make the basic auth request or is better switch the auth method? The client should have the lesser number of libraries as possible.

Thanks for reading and time.

0 Answers
Related