Wednesday, 28 August 2013

Passing basic authentication details in spring security using http headers in java

Passing basic authentication details in spring security using http headers
in java

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-
<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">
<http auto-config="true" create-session="stateless">
<intercept-url pattern="/**" access="ROLE_USER" />
<http-basic />
</http>
<authentication-manager>
<authentication-provider>
<!-- <user-service>
<user name="yoman" password="123456" authorities="ROLE_USER" />
</user-service> -->
<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 =? "
/>
</authentication-provider>
</authentication-manager>
</beans:beans>
and I am trying to pass the username and password using authorization
header like this-
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Authorization", "Basic "
+ "username:password");
But still everytime when I access the service its asking me for username
and password.
What am I doing wrong and whats the correct way here. I will be really
grateful for any help provided.

No comments:

Post a Comment