I'm always excited to take on new projects and collaborate with innovative minds.

Phone

+1 234 567 890

Email

contact@botble.com

Website

https://botble.com

Address

123 Main Street, New York, NY 10001

Social

Spring Security

Basic Authentication setup code for Spring Boot, focusing only on implementing Basic Authentication without additional features:

1. Create Project in IntelliJ IDEA

  1. Open IntelliJ IDEA and create a new Spring Boot project.
  2. Add the required dependencies:
    • Spring Boot Starter Security
    • Spring Boot Starter Web
    • <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-security</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
      </dependencies>

2. Create Package Structure

Create the following packages:

  1. org.niteshsynergy.security (For Security Configuration)
  2. org.niteshsynergy.controller (For Controller)

3. Write the Security Configuration

  1. Create a new class SecurityConfig in the package org.niteshsynergy.security.
  2. Add the following code:

    SecurityConfig.java:

package org.niteshsynergy.security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
   @Bean
   public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
       http
           .csrf().disable() // Disable CSRF for simplicity.
           .authorizeRequests()
               .anyRequest().authenticated() // Require authentication for all endpoints.
               .and()
           .httpBasic(); // Enable Basic Authentication.
       return http.build();
   }
}

 

4. Set Up Authentication Credentials

  1. Open the application.properties file located in src/main/resources.
  2. Add the username, password, and role:

    application.properties:

spring.security.user.name=admin
spring.security.user.password=password
spring.security.user.roles=USER
 

 

5. Create a Test Controller

  1. Create a new class HelloController in the package org.niteshsynergy.controller.
  2. Add the following code:

    HelloController.java:

package org.niteshsynergy.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
   @GetMapping("/hello")
   public String sayHello() {
       return "Hello, Authenticated User!";
   }
}

 

6. Run the Application

  1. Run the application using the Run button in IntelliJ IDEA or from the main method in the Spring Boot application class.
  2. Access the endpoint:
    • URL: http://localhost:8080/hello

7. Test the Authentication

  1. Open a browser, Postman, or curl.
  2. Make a GET request to http://localhost:8080/hello.
  3. Enter the credentials:
    • Username: admin
    • Password: password.

 

Expected Behavior

  1. Without Credentials:
    • Response:
    • {
         "timestamp": "2024-11-23T12:00:00.000+00:00",
         "status": 401,
         "error": "Unauthorized",
         "message": "Unauthorized",
         "path": "/hello"
      }
       
  2. With Credentials:
    • Response:

      Hello, Authenticated User! ser!


      Note: Even if your not adding 3. Write the Security Configuration code then also username & password will ask while calling hello api.

2 min read
नव. 23, 2024
By Nitesh Synergy
Share