In this post, you will see code samples for how to add basic authentication to Spring Boot API. The code samples in this post will show how to secure certain endpoints with Spring security such that they can be only accessed via the correct username and password. This post will also include links to the Github repository that has the full source code for this API along with the Github action to build it every time you commit the code.

The sample API

The API you will create will be a simple REST API with the following endpoints,

# Hello endpoints

# User endpoints

Your goal is to secure all of the above API endpoints except for /say/helloNoAuth and /api/user/ping.

How to add basic authentication to Spring Boot API

You can start by adding controllers,

public class HelloController {
    private HelloServiceImpl helloService;

    @GetMapping(value = "/hello")
    public String sayHello() {
        return "Hello World";
    @GetMapping(value = "/helloNoAuth")
    public String helloNoAuth() {
        return "Hello World with no auth";
    @GetMapping(value = "/hello/service")
    public ResponseEntity<String> helloFromService() {
        return new ResponseEntity<>(helloService.greeting(), HttpStatus.OK);

p.s. If you copy and paste this code in your IDE and notice compile time errors, you can auto-resolve most of them. In case you are not sure about the packages the imports are from you can checkout the full source code in the Github repository link in the conclusion section.

The user controller

public class UserController {

    @GetMapping(value = "/ping")
    private ResponseEntity<CustomUser> pingUser() {
        List<GrantedAuthority> ROLE_USER = Collections
        CustomUser cUser = new CustomUser("bsoni", "1234", ROLE_USER);
        return new ResponseEntity<>(cUser, HttpStatus.OK);
    public ResponseEntity<Object> createUser(@RequestBody CreateUserRequest createUserRequest) {
        String ROLE_PREFIX = "ROLE_";;"About to create user");
        if(ObjectUtils.isEmpty(createUserRequest)) {
            return new ResponseEntity<>("Please supply create user body", HttpStatus.BAD_REQUEST);
        List<GrantedAuthority> auths = new ArrayList<>();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        for(String role: createUserRequest.getRoles()) {
            auths.add(new SimpleGrantedAuthority(ROLE_PREFIX + role.toUpperCase()));

        CustomUser cUser = new CustomUser(createUserRequest.getUsername(), bCryptPasswordEncoder.encode(createUserRequest.getPassword()), auths);;
        InMemoryUserDetailsManager inMemoryUserDetailsManager = CustomUserDetailsManager.instance.getInMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(cUser);"Successfully created the user");
        return new ResponseEntity<>(cUser, HttpStatus.CREATED);
    public ResponseEntity<String> test2() {
        return new ResponseEntity<>("Doing another test", HttpStatus.OK);

As you can see above there is a lot happening in the UserController, mostly because to demonstrate certain concepts for this post, the code hasn’t been segregated into a Service layer. In order to create a user, with the above code, you are just re-using the InMemoryUserDetailsManager class from Spring Security. As the name implies, the class maintains a list of users in memory when the application runs. This means the users need to be added to the in memory user store every time the application starts, for the users to be able to login. Next, you will write one of the most important aspects of this API.

Security Configuration (or security config)

public class SecurityConfig {

    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // setup basic authentication
                    .authorizeHttpRequests(auth -> {
                        try {
                        } catch (Exception e) {
                            throw new RuntimeException(e);
        // configure the csrf token to ignore the post request to create new users
        http.csrf(httpSecurityCsrfConfigurer ->

    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager inMemoryUserDetailsManager = CustomUserDetailsManager.instance.getInMemoryUserDetailsManager();
        UserDetails userDetails = User.withUsername("bsoni")
                .roles("ADMIN", "USER")
        return inMemoryUserDetailsManager;
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();


Remember your goal was to secure all the API endpoints, except for /say/helloNoAuth and /api/user/ping. You are doing exactly that from line 10 to line 18.

All the logic does is, allow the endpoints without any authentication whereas, require the user to be authenticated for any other API calls.


If you want to see a full working project for the code shared in this blog post, you can access it all on the Github repo custom-spring-boot-basic-auth.

If you have any questions on this, feel free to leave a comment on this post or send me an email directly.

If you find any of my posts useful and want to support me, you can buy me a coffee 🙂

While you are here, maybe try one of my apps for the iPhone.

Products – My Day To-Do (

Here are some of my other bloposts on Java

How to build a full stack Spring boot API with ReactJS frontend – My Day To-Do (

How to call REST API with WebClient – My Day To-Do (

How to build a jokes client in Java Spring Boot with RestTemplate – My Day To-Do (

Have a read of some of my other posts on AWS

Upload to AWS S3 bucket from Java Spring Boot app – My Day To-Do (

Deploy NodeJS, Typescript app on AWS Elastic beanstalk – (

How to deploy spring boot app to AWS & serve via https – My Day To-Do (


Leave a Reply

Avatar placeholder
Verified by MonsterInsights