title: How to set Authorization header in Rest API Basic Authentication
body: <p>Currently I am working in REST API in Java. My RestAPI is in separate maven project, And web application is in separate maven project. I have handled Basic Authentication in RestAPI. I want to set <strong>Authorization in response header and also need to get it from HttpRequest in each request(Is this best practice?)</strong></p>
  
  <p>Once I login into the application, login Username and password will forward to API, If once login credentials are valid, need to set Authorization header in Response. Each and every time I want to check the header whether the credentials are valid.</p>
  
  <p>My Question is, </p>
  
  <ol>
  <li>How to set Authorization Header, and where to set?</li>
  <li>Either I want to set header in every response or only once?</li>
  </ol>
  
  <p>My Web.xml(RestAPI)</p>
  
  <pre><code>&lt;http realm="MY APP REALM"&gt;
      &lt;csrf disabled="true" /&gt;
      &lt;intercept-url pattern="/rest/**"
          access="hasAnyRole('ROLE_ADMIN','ROLE_USER')" /&gt;
      &lt;http-basic entry-point-ref="basicAuthenticationEntryPoint" /&gt;
  
  &lt;/http&gt;
  &lt;beans:bean name="bcryptEncoder"
      class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" /&gt;
  &lt;beans:bean name="ser"
      class="com.test.api.config.SampleUserDetailsService" /&gt;
  &lt;beans:bean name="userService" class="com.test.api.service.impl.UserServiceImpl" /&gt;
  
  &lt;beans:bean name="basicAuthenticationEntryPoint"
      class="com.test.api.config.MyAppBasicAuthenticationEntryPoint" /&gt;
  
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider user-service-ref="ser"&gt;
          &lt;password-encoder ref="bcryptEncoder" /&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>REST API CONTROLLER</strong></p>
  
  <pre><code>@RestController
  @RequestMapping("/rest")
  public class RestServiceController {
  
  @Autowired
  IUserService userService;
  
  
  @RequestMapping(value = "/user", method = RequestMethod.GET)
  public ResponseEntity&lt;List&lt;Users&gt;&gt; listAllUsers() {
      List&lt;Users&gt; users = userService.findAllUsers();
      if (users.isEmpty()) {
          return new ResponseEntity&lt;List&lt;Users&gt;&gt;(HttpStatus.NO_CONTENT);// You many decide to return
                                                                          // HttpStatus.NOT_FOUND
      }
      return new ResponseEntity&lt;List&lt;Users&gt;&gt;(users, HttpStatus.OK);
  }
  
  }
  </code></pre>
  
  <p>Help me to resolve this?</p>