In this guide, we will build a working app who will create users and authenticate/authorize users and clients in a microservices architecture using Spring Boot, Spring Cloud, and MongoDB.

I won’t use the inMemory configuration that we always see in the most guides.

This guide will be composed of 3 parts:

First part: Creating the config, registry and gateway services;

Second part: Create the auth service, the one who will authenticate the users;

Third part: Create the account service, the one who will register new users;

I’ll assume you already have some familiarity with microservices and Spring Boot.

Creating the Config Service

The config application will be responsible to serve the spring configuration to every service. With it, you can centralize your configuration files and separate them by the environment that you are working, like dev, QA or production. Here is a more detailed explanation of the config server.

I’ll use the Spring Initializr to generate the project, and it will be available in the GitHub repo. The configuration will be like this:

Generating the config-service

We will need to add the annotation @EnableConfigServer in ConfigServerApplication class.

@SpringBootApplication

@EnableConfigServer

public class ConfigServerApplication {

public static void main(String[] args) {

SpringApplication.run(ConfigServerApplication.class, args);

}

}

This annotation tells Spring that this application will act as a config server.

Now we will rename the application.properties file to application.yml and add these configurations to it:

spring:

cloud:

config:

server:

native:

search-locations: classpath:/shared #tells spring where to locate the config files

profiles:

active: native #we are using native because the config server uses a git repo by default

security:

user:

password: 1234 #the password which apps will use to access its configurations



server:

port: 8888 #the port where the config-server will be acessible

Create the folder shared inside the resources folder. Create another application.yml in the folder you have just created with this content:



eureka:

instance:

prefer-ip-address: false

client:

serviceUrl:

defaultZone:



security:

oauth2:

resource:

user-info-uri: #This configuration file will hold common configs to be shared among all files: false http://localhost:8761/eureka/ #where our registry server is located http://auth-service/uaa/users/current #which uri will be used to find the user

Let’s run the application and access http://localhost:8888/actuator/health. You should see a message like this:

{“status”:”UP”}