Spring Security HTTP basic authentication example

When HTTP basic authentication is configured, web browser will display a login dialog for user authentication. This tutorial show you how to configure HTTP basic authentication in Spring Security.

	<intercept-url pattern="/welcome*" access="ROLE_USER" />
	<http-basic />

Last Spring Security form-based login example will be reused, but switch authentication to support HTTP basic.

1. Spring Security

To enable HTTP basic, just change “form-login” to “http-basic” tag.

<beans:beans xmlns="http://www.springframework.org/schema/security"
	<!-- HTTP basic authentication in Spring Security -->
		<intercept-url pattern="/welcome*" access="ROLE_USER" />
		<http-basic />
		   <user name="mkyong" password="123456" authorities="ROLE_USER" />

Done, that’s all.

2. Demo

When access the secured URL, browser will display a login dialog box automatically.

URL : http://localhost:8080/SpringMVC/welcome

http basic example

Download Source Code


  1. Spring Security hello world example
  2. Spring Security form-based login example
Tags :

About the Author

Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: parking()

  • Pingback: water ionizers()

  • Pingback: alkaline water machine()

  • Pingback: youporn()

  • Pingback: laan penge online nu()

  • Pingback: stop parking()

  • Pingback: penge lan nu()

  • Pingback: TV for Businesses()

  • Pingback: tvpackages.net()

  • Pingback: DIRECTV()

  • Pingback: kangen water()

  • Pingback: kangen()

  • Pingback: water ionizer()

  • Pingback: water ionizer()

  • Pingback: streaming movies()

  • Pingback: watch free movies online()

  • Pingback: tv online, online tv()

  • Pingback: streaming movies()

  • Pingback: Blue Coaster33()

  • Pianista

    Hi mkyong,

    One question, is there a way to suppress this dialog on the browser from the server?

    For example if we want to hide this resource to all the people that is not passing by default the AuthBasic headers on their requests.

    Thank you

  • sooriyah

    where can i find the /welcome usage…can u plz eplain me in detail..i am new to spring

  • Betlista

    Thanks for your post ;-)

    Just small note – if you want to change “The server says:” part, you can specify realm in http tag

    &lt;http realm=&quot;My secured application&quot;&gt;
  • http://www.dsasoluciones.com/ David

    Hi good morning, i want to create a web service and i use spring security, but i want to connect to a database and check for the existence user instead of using an xml. I hope you can tell how to configure that greetings.

  • http://notder BrahmaReddy

    will you please give spring insertion, selection, update of programs for struts, springs.