02 Securing Rest Api
02 Securing Rest Api
responsible for putting the result (the Authentication object) into the
SecurityContextHolder.
Spring Boot Security Starter
The goal of Spring Boot is to ease application development. Just like with every
other feature of Spring Boot, by adding the matching starter POM, the Spring Boot
starter called Security creates the basic configuration setup for the developer,
including HTTP Basic Authentication and an AuthenticationManager bean with an
in-memory default user when building an application on top of Spring Boot.
We will now configure Spring Security in our UserRegistrationSystem application.
To add Spring Security to our Spring Boot application, we need to add the Spring
Security dependency in the Maven pom.xml file.
Once we add the Spring Security dependency to the Maven pom.xml file, our entire
application is protected via HTTP Basic Authentication for all resources except
common static resources (CSS files, JavaScript files, and so on), and an
AuthenticationManager bean with an in-memory default user is created for your
application.
The default username is user, and the password is generated in the STS IDE
console log when we run the UserRegistrationSystem application as a Spring Boot
application. Once we start our Spring Boot application, we will see the generated
default user’s password in the logs, as follows:
Using default security password: 72baa813-dab9-47bf-8329-78987485785b
The password generated here is random and will change every time we start our
Spring Boot application, whereas the username will be always same (here it’s user).
When we start our Spring Boot application after adding a Spring Security
dependency, the log is printed in the Postman as shown below:
Customer Self Care Web Portal (CSWP)
When we hit the same above url in the browser, we will get the input challenge
thrown by spring as shown below:
We must supply a username and password along with a request to authorize. So, we
will supply the username user and a password after copying the generated user’s
default password from the STS console to authenticate the user while accessing the
RESTful API.
Launch Postman, click the Authorization tab, and then set Type as Basic Auth.
Provide a username and password and click Update Request so that the request
header will get a value for authorization. Finally, visit
http://localhost:8080/api/user/, as shown in below. Since the user list is empty in the
UserRegistrationSystem application, the response will show an empty list.
Customer Self Care Web Portal (CSWP)
It is hardly practical to copy a password from a log every time we restart the
application. We can customize this default credential by setting some properties in
the application.properties file.
Overriding the Spring Security Defaults
Spring Boot allows developers to easily override the security defaults (username,
password, and role) by specifying security properties in the application.properties
file located in the src\main\resources\ folder of the UserRegistrationSystem
application, as shown below: