In this post we are going to look at setting up basic security for your Spring Boot app.
Any Spring Boot app should have some kind of security in place as a general best practice, especially while being deployed in a production environment.
That being said it is actually pretty straight forward to setup a basic authentication mechanism using one of Spring Boot’s starter mechanism.
We are going to build on our existing app and add basic security. Add the following to the pom.xml file.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>1.3.3.RELEASE</version> </dependency>
This would basic security to all the end points of this app.
Add the following properties to the application.properties file.
management.security.enabled=true security.basic.enabled=true security.user.name=admin security.user.password=admin
Here we are setting up the user name and password in the properties file itself and enable the basic security.
Now restart the application and try to access any end point and you would get the Authentication Required screen as shown below.
Enter admin, test as the user name and password and you would be prompted to enter the user name and password again. Enter admin, admin as the username and password and you would be able to access the service.
Basic security is now enabled in your Spring Boot app.