title: Passing basic authentication details in spring security using http headers in java
body: <p>I am implementing spring security in my restful web service. I am using http-basic authentication, right now when I try to access my service a windows security dialogue appears asking for user name and password, now I want to pass the user name and password via http header so that I don't need to enter the details exclusively in the security dialogue. My security config file looks like-</p>
  
  <pre><code>&lt;beans:beans xmlns="http://www.springframework.org/schema/security"
      xmlns:beans="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt;
  
      &lt;http auto-config="true" create-session="stateless"&gt;
          &lt;intercept-url pattern="/**" access="ROLE_USER" /&gt;
          &lt;http-basic /&gt;
      &lt;/http&gt;
  
      &lt;authentication-manager&gt;
        &lt;authentication-provider&gt;
         &lt;!--  &lt;user-service&gt;
          &lt;user name="yoman" password="123456" authorities="ROLE_USER" /&gt;
          &lt;/user-service&gt; --&gt;
          &lt;jdbc-user-service data-source-ref="dataSource"
  
             users-by-username-query="
                select username,password, enabled 
                from users where username=?" 
  
             authorities-by-username-query="
                select u.username, ur.authority from users u, user_roles ur 
                where u.user_id = ur.user_id and u.username =?  " 
  
          /&gt;
        &lt;/authentication-provider&gt;
      &lt;/authentication-manager&gt;
  
  &lt;/beans:beans&gt; 
  </code></pre>
  
  <p>and I am trying to pass the username and password using authorization header like this-</p>
  
  <pre><code>HttpHeaders requestHeaders = new HttpHeaders();
  requestHeaders.set("Authorization", "Basic "
          + "username:password");
  </code></pre>
  
  <p>But still everytime when I access the service its asking me for username and password.</p>
  
  <p>What am I doing wrong and whats the correct way here. I will be really grateful for any help provided.</p>