How to make a simple One-Time-Password service with Spring Boot.

Anton Smirnov
ITNEXT
Published in
5 min readNov 9, 2021

--

Photo by Lee Campbell

After publishing the previous article, I have received messages complaining that the article provides a complex example. And if I can describe an easier way to create a One-Time-Password Service.

In this article, I decided to partially repeat my article about creating a one-time password and implement a different approach.

In this article, I will consider creating a One-Time-Password microservices using Spring boot. We will also analyze the implementation of interaction with this service using Selenium WebDriver 4.

Initially, we need to figure out what Spring boot is.

Spring Boot is a useful project that aims to simplify the creation of applications based on Spring. It allows you to create a web application most simply, requiring a minimum of effort from developers to configure it and write code.

Due to the cumbersome configuration of dependencies, configuring Spring for enterprise applications has become a very tedious and error-prone task. This is especially true for applications that also use several third-party libraries.

Every time you create another corporate Java application based on Spring, you need to repeat the same routine steps to configure it:

  • Depending on the type of application being created (Spring MVC, Spring JDBC, Spring ORM, etc.), import the necessary Spring modules.
  • Import a library of web containers.
  • Import the necessary third-party libraries (for example, Hibernate, Jackson).
  • Configure DAO components, such as data sources, transaction management, etc.
  • Configure web layer components.
  • Define a class that will load all the necessary configurations.

The authors of Spring decided to provide developers with some utilities that automate the configuration procedure and speed up the process of creating and deploying Spring applications, under the general name Spring Boot

Spring Boot has a lot of functionality, but its most significant features are dependency management, automatic configuration, and built-in servlet containers.

What you will need.

As I described above, we will create a project to simulate the operation of the OTP service using Spring Boot and Gradle. Thus, for our project, we must have the following tools and technologies:

  • Spring Boot version 2.5.6+
  • Java8+
  • Gradle 7.2+
  • Any IDE of your choice. (I prefer IntelliJ IDEA)

Creating any project using Spring Boot must start with:

https://start.spring.io /

This portal was created by Spring boot developers to enable the creation of projects based on Spring boot in record time.

I recommend that you add several dependencies at the project creation stage:

  • Spring Boot DevTools. Spring Boot provides a module called Spring Boot DevTools. DevTools stands for Developer Tool. The aim of the module is to try and improve the development time while working with the Spring Boot application. Spring Boot DevTools pick up the changes and restart the application.
  • Spring Web. This group comprises Web, Web-Servlet, Web-Struts, and Web-Portlet. These modules provide support to create a web application.

After creating the project, you can open it in your IDE. All necessary dependencies will be installed automatically.

For full-fledged work, we need to create a controller class. In this class, all the logic for processing HTTP requests will be concentrated. Let’s call it Home Controller.

The RestController annotation must be added to this class. We use @RestContoller annotation to tell Spring that this class is a controller for Spring REST applications.

The second important annotation in Spring MVC is the @RequestMapping. We use this annotation to map client requests with the appropriate method to serve the request. In the brackets of the RequestMapping annotation, we specify the URL path. For example @RequestMapping(“/api/otp”)

An example of such a controller may look like this:

After launching the service on the URL: http://localhost:8080/api/otp, we receive in response:

{"is_successful":"true","code":"1234","parameter_errors":"null","server_datetime":"Sun Nov 07 13:12:29 MSK 2021"}

Our One-Time-Password works fine and returns the actual result.

But in this implementation, there is a problem in the decomposition of logic. The controller should only work with requests. There should be no extraneous logic in the HomeController class. To separate our business logic from the controller, it is necessary to create a Service. There must be a separate service for each entity. Let’s create a Home Service class.

Service Components are the class file that contains @Service annotation. These class files are used to write business logic in a different layer, separated from the @RestController class file.

The Service class in our example Home Service is a separate abstraction layer that works with our business logic. The logic for creating a service component class file is shown here:

After we moved the logic to a separate service, the controller count looks like this:

Let’s open on the URL again: http://localhost:8080/api/otp and make sure that the result has not changed and the service continues to work correctly:

{"is_successful":"true","code":"1234","parameter_errors":"null","server_datetime":"Sun Nov 07 13:12:29 MSK 2021"}

Selenium Project.

To check how to work with service the One-Time-Password you must make an example Login Page with the OTP field. For example, this HTML page looks like this:

The JavaScript code for processing data entered by the user on the Login Page may look like this:

To work with the One-Time-password service on the UI side of the framework, we need to create a Service Helper that looks like this:

Login Page class for Selenium may look like this:

And automation script looks like this:

Conclusion.

Congratulations on completing your first Spring boot project. In this article, you learned about the basics of Spring boot, how to set up your project, add project dependencies, launch your first REST API service, and how to create your simple One-Time Password service. In this article, I tried to focus on creating a simple OTP Service. The main purpose of this article is to learn the initial capabilities of Spring Boot. We also looked at an example of a solution for the interaction of Selenium Webdriver with the REST API service. In the next article, I plan to give an example of creating a full-fledged One-Time-Password on SpringBoot and interacting with it at the FrontEnd level.

https://test-engineer.site/

Author Anton Smirnov

--

--

I’m a software engineer who specializes in testing and automation. My top languages are Java and Swift. My blog is https://test-engineer.tech/