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

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 .::

- 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.


2 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.


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 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.