title: Spring security with Html pages and Rest web service authentication
body: <p>I'm using Spring security and database login in my application (in the future I'll have to implement LDAP authentication).
  Through web all work right, but now when I call web services from external (I have some web service for internal javascript and some for external calls) I receive the HTML code of login page. It's correct, but now how can I make REST call?
  I have to protect them, I thought to use a token or username and password for each web services call, but how can I set username and password in REST call?
  For example with postman. Then I will set the credentials also in </p>
  
  <pre><code>restTemplate.setRequestFactory(requestFactory);
  responseEntity  = restTemplate.getForEntity(serverIp + "ATS/client/file/?filePath={filePath}", byte[].class, filePath); 
  </code></pre>
  
  <p>and in </p>
  
  <pre><code>MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  ContentBody cbFile = new FileBody(file);
  ContentBody cbPath= new StringBody(toStorePath,ContentType.TEXT_PLAIN);
  builder.addPart("file", cbFile);
  builder.addPart("toStorePath",cbPath);
  httppost.setEntity(builder.build());
  CloseableHttpResponse httpResponse = httpClient.execute(httppost);
  HttpEntity resEntity = httpResponse.getEntity();
  </code></pre>
  
  <p>On the web I have even the roles for the user, maybe I'll have to use them also for the web services.
  Thanks for the advices. Regards
  UPDATE:
  As @Gergely Bacso advices me, I have updated my code, but now I have the opposite problems: When I call web services they return all the information without username and password.
  This is security config:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      @Qualifier("userDetailsService")
      UserDetailsService userDetailsService;
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
      }
  
      @Bean
      public PasswordEncoder passwordEncoder(){
          PasswordEncoder encoder = new BCryptPasswordEncoder();
          return encoder;
      }
  
      @Configuration
      @Order(1)
      public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http.csrf().disable()
                      .antMatcher("/client/**")
                      .authorizeRequests()
                          .anyRequest().authenticated()
                          .and()
                      .httpBasic();
          }
      }
  
      @Configuration
      @Order(2)
      public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{
  
          @Override
          public void configure(WebSecurity web) throws Exception {
              web
                      //Spring Security ignores request to static resources such as CSS or JS files.
                      .ignoring()
                          .antMatchers("/static/**");
          }
  
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http.csrf().disable()
                      .authorizeRequests() //Authorize Request Configuration
                          //.antMatchers("/", "/register").permitAll()
                         // .antMatchers("/admin/**").hasRole("ADMIN")
                          .anyRequest().authenticated()
                          .and() //Login Form configuration for all others
                      .formLogin()
                          .loginPage("/login").permitAll();
          }
      }
  
  }
  </code></pre>