In this post, we will learn about spring-boot-security using http basic authentication
In this tutorial we will use spring security where we can enable security for our spring applications
Dependency
Add above dependency in your pom.xml
In this example, we are creating two rest endpoints, which I named as hello and hi
Now we can create new class SecurityConfig.java to configure our security
we need to add @Configuration
@EnableWebSecurity
annotations in our SecurityConfig.java and also we need to extend WebSecurityConfigurerAdapter
And also we need to override two configure methods in SecurityConfig.java
In the first configure method we need to configure user and role details, Here I added two users with two roles
In the above snippet, we allowed /hello
endpoint and remaining endpoints are authenticated with httpBasic authentication
We are using PasswordEncoder to encode the password
Run the application as spring boot application
Testing
Conclusion
The above example is a simple spring security authentication using http basic. Here we added user details in-memory authentication