Spring Boot + Spring Security – RESTful Web Service with basic Authentication

Spring-Boot-Tutorials » on May 8, 2018 { 5 Comments } By Sivateja

In this article, I am going to explain you how to implement basic authentication for RESTful web services using Spring Boot and Spring Security. We will need to create a java file with spring security configurations in it, that’s it 🙂

Required Dependency


Final pom.xml

    xmlns:xsi="" xsi:schemaLocation="">


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootApp {
    public static void main(String[] args) {, args);


import org.springframework.context.annotation.Configuration;

public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    // Authentication : set user/password details and mention the role
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            .withUser("admin").password("pass").roles("USER", "ADMIN");

    // Authorization : mention which role can access which URL
    protected void configure(HttpSecurity http) throws Exception {


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class SpringJava4sController {

    public String userValidation() {
        return "User: Successfully logged in!";


    public String adminValidation() {
        return "Admin: Successfully logged in!";



Now run the application, if you hit /springbootwithsecurity/userlogin you should provide user/pass as credentials and for /springbootwithsecurity/adminlogin admin/pass, give a try 😉 I am not going to explain the as its easily understandable.


In the next article, I will explain how to implement the Authentication using database.



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


5 Responses to “Spring Boot + Spring Security – RESTful Web Service with basic Authentication”
  1. indra reddy says:

    Hi, please explain SpringSecurityConfig class also. most of the people like me don't know about Security's.
    if you explain that it's very helpful tu us

  2. springboot says:

    Please add few lines lines of explanations to below each block of code. Example: explaining WebSecurityConfigurerAdapter in one or two lines.

  3. Apparao says:

    For me application not working means it's not showing login to enter username and password directly showing output help me in that

  4. vamsi says:

    I have tried this logic, it works for the first time but if my springboot application is restarted, it is not showing login dialog box. Could you please advice me how to fix it. I want this logic to run each time I start the application

  5. Padma says:

    So whenever there is spring-boot-starter-security dependency , how the Spring's dispatcher servlet will route the request for authentication and authorization since after authentication only the requests will be routed back to the controller. Could you elaborate on this please. Thanks!.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.