Enabling Spring Boot Authorization using our own Credential.

Enabling Spring Boot Authorization using our own Credential.

Welcome back, friends! We have learned how to enable the Spring Boot default Authorization Previous Story. Now we will be going to learn how to enable Spring Boot Authorization using our own Credentials.

In order to configure our own Credential, we have to create a Spring Security configuration class which extend from “WebSecurityConfigurerAdapter” 

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {}

As above create a new class which extends from WebSecurityConfigurerAdapter then we need to override the following configure Method. 

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}

In this method, we need to configure our own credentials As of now I will be going to use in-memory authentication i.e we are not fetching the user details from Database instead we are keeping the Credentials in Spring Boot memory.

Method implementation

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("joe").password("joe123").roles("USER")
            .and().withUser("mark").password("123").roles("ADMIN");
}

This method accepts one parameter called AuthenticationManagerBuilder using this class we can configure In-Memory Authentication. In the above method body, I am calling inMemoryAuthentication().withUser(“Joe”).password(“joe123”)

auth.inMemoryAuthentication().withUser("joe").password("joe123").roles("USER")

This says to Spring Boot, create a new user called “joe” and set a password as “joe123” and assign User Role to “joe”. If you want to add one more user, just we need to use and() method like below.

auth.inMemoryAuthentication().withUser("joe").password("joe123").roles("USER")
            .and().withUser("mark").password("123").roles("ADMIN");

Again I am adding a new user using and() method and calling withUser() for creating new user “mark” and setting the password as “123” then assigning ADMIN Role.

The complete method
The below method creates two users when starting the Server. Since we are using @Configuration annotation. This class act as configuration classes so, Spring Boot Automatically calls this class and creates two user joe and mark respectively.

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("joe").password("joe123").roles("USER")
            .and().withUser("mark").password("123").roles("ADMIN");
}

In order to work this Authorization, we need to say what type of password will be going to use. Using getPasswordEncoder(), I am saying to Spring Boot i want to use a plain password.

@Bean
PasswordEncoder getPasswordEncoder() {
    return NoOpPasswordEncoder.getInstance();
}

Now, we are done configuring for our own Credential in Spring Boot App. Now Time to write a new Webservice endpoint. Following is my Simple Webservice Controller class.

@RestController
public class WelcomeController {

    @GetMapping("welcome")
    public String welcome() {
        return "Welcome to Spring Security";
    }
}

If you want to call this Webservice endpoint. you have to log in with either one of the following users otherwise it will call Spring Boot default Login Form where you have to log in with either one of Credential

User = joe
Password = joe123

User = mark
password = 123
For complete demo 

Try to access the rest endpoint using the following endpoint
http://localhost:8080/welcome
This will throw following Spring Boot default login form where we need to enter above one of the user name and password.

Spring Boot login form

Once you enter the user name and password it will return the following screen.
Spring Boot Security

Download the code from the below link
Spring Boot Security sample code
Watch this full example in the following YouTube video with Demo
https://youtu.be/v1q5ikGyybo

Now you have successfully configured your own user name and password. Soon we will be going to see how to configure the user name and password which is coming for DB. 


Please follow our ITGARDEN YouTube channel for Billing System Project development work using Spring Boot. The Project source code absolutely free. This project is very good for who is looking for real-time project experience in Spring Boot. To understand the Billing System Project code, watch all Billing System-related YouTube videos from this link. Click Here
Our YouTube Channel
https://www.youtube.com/channel/UChj5CeuWaHIFr4RkXoG3iJA?view_as=subscriber
Please follow me on Medium.com for all billing system related stories and Java-based technical Articles
https://suresh-stalin.medium.com/
Note: To find all billing System-related stories find “billing system itgarden” in medium.com

Close Menu
×
×

Cart