In Spring Security, we will discuss about the Spring security HTTP basic authentication. When HTTP basic authentication is configured, web browser will display a login dialog for user authentication.
Required Tools used for this Application:
- Spring MVC 3.0.1
- Spring Security 3.1.0
- STS 2.8.1.RELEASE
- Tomcat 7
- Jdk 1.7
Popular Tutorials
You can configure HTTP basic authentication in sdnext-security.xml as follows :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <security:http auto-config="true" > <security:intercept-url pattern="/index*" access="ROLE_USER" /> <security:http-basic></security:http-basic> <security:logout logout-success-url="/logout" /> </security:http> <security:authentication-manager> <security:authentication-provider> <security:user-service> <security:user name="dineshonjava" password="sweety" authorities="ROLE_USER" /> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans>
To enable HTTP basic, just change “form-login” to “http-basic” tag. As follows on the above file
<security:http auto-config="true" > <security:intercept-url pattern="/index*" access="ROLE_USER" /> <security:http-basic></security:http-basic> <security:logout logout-success-url="/logout" /> </security:http>
And all remaining code same as the previous example Spring Security form-based login example ,
After that we will run the example.
Running the example
Export the example as war and deploy it Tomcat 7 server. While browsing the project you will get the following screen for login:
Access URL “http://localhost:8080/sdnext/index“, Spring will redirect to your custom login form.
URL : http://localhost:8080/sdnext/index
If username/password is correct, authentication success, display requested page.
URL : http://localhost:8080/sdnext/index
Download Source Code + Libs
SpringSecurityHttpBasic.zip
References-
https://www.dineshonjava.com/spring-security-form-based-login-example/
Spring Security
Spring Security documentation
Spring Security Related Posts
- Spring Security Interview Questions and Answers
- Spring Security Java Based Configuration with Example
- Spring Security XML Namespace Configuration Example
- Spring Security XML Based Hello World Example
- Spring Security form-based login example
- Spring Security Login Form Based Example Using Database
- Spring Security Authentication Example Using HTTP Basic
- Spring Security Authorized Access Control Example
- Spring Security Customized Access Denied Page
- Spring Security Custom Error Message
- Spring Security Logout Example
- Spring Security Fetch Logged in Username
- Spring Security Password Hashing
thanks