Spring Security without the WebSecurityConfigurerAdapter

In Spring Security 5.7.0-M2 we deprecated the WebSecurityConfigurerAdapter , as we encourage users to move towards a component-based security configuration. To assist with the transition to this new style of configuration, we have compiled a list of common use-cases and the suggested alternatives going forward. In the examples below we follow best practice by using the Spring Security lambda DSL and the method HttpSecurity#authorizeHttpRequests to define our authorization rules. If you are new to the lambda DSL you can read about it in this blog post.

Spring Cloud Introduction to Service Discovery Netflix Eureka

Micro-services are all good until they come with their distributed challenges which normally we don’t face in monolithic based applications. Just like this one! Understanding the problem In order to fully get the main purpose of the solution let us first understand the underlying problem. As the diagram above shows, we have to micro-services communicating via a certain network: Micro-service I with the IP address 10.10.10.10 , and port 8080 Micro-service II with the IP address 20.

Messaging with RabbitMQ in Spring Boot Application

In this article, we will learn about RabbitMQ and explore its common use-cases. We will also walk through a step-by-step guide to implement messaging using RabbitMQ in a Spring Boot Application and will see how to publish and consume messages in a queue using RabbitMQ. So, let’s begin learning! What is a RabbitMQ? RabbitMQ is an open-source message broker that allows enterprise applications to communicate with each other. It’s a popular AMQP (Advanced Message Queuing Protocol)broker.

Load Balanced Websockets with Spring Cloud Gateway

The ability to have real-time two-way communication between the client and the server is a key feature in most modern web apps. A simple approach to setting up WebSockets in Spring Boot is covered in Simple WebSockets with Spring Boot, which uses an in-memory message broker. This approach falls short, though, when you scale up and add additional servers. Users connected to different servers would have no way of communicating or getting updates pushed to them for something that’s happened on another server.

Spring Cloud Config Server - Encryption and Decryption

While using Spring Cloud Config Server, we also have the feature to encrypt sensitive information that is otherwise stored as plain text in our external git repository. This prevents anyone who has read access to the external repository from accessing confidential information and is highly highly recommended if you are using a Spring Cloud Config Server. Note: If you haven’t set up Spring Cloud Config Server yet check out my other article on how to set up a spring cloud config server.

Spring Cloud Config Server: Step by Step

Spring Cloud Config Server is used to provide server-side and client-side support for externalized configuration in a distributed system. So when you have multiple microservices, and you want to easily control the configuration for all of them at one go - you’ll mostly be looking at Spring Cloud Config Server. So how do we do this? We will create a git project which contains all your properties files for the multiple microservices that you have (easy enough).

Spring Data JPA Query Method by Multiple Columns Example

In this tutorial, we will learn how to write a Spring Data JPA query method or finder method for multiple columns/fields. Consider the following Product entity class and if we want to retrieve products by their name OR description fields then here is the Spring data JPA query method: 1 public List<Product> findByNameOrDescription(String name, String description); Let’s create a complete example to understand end to end. Maven Dependencies Create a Spring boot project and add the following maven dependencies to it:

Spring Boot Integration Testing MySQL CRUD REST API Tutorial

In this tutorial, we will learn how to perform Integration testing Spring boot application using @SpringBootTest annotation. We use MySQL database to store and retrieve the data. Let’s first take a look at the overview of @SpringBootTest annotation. @SpringBootTest Annotation Spring Boot provides @SpringBootTest annotation for Integration testing. This annotation creates an application context and loads the full application context. @SpringBootTest will bootstrap the full application context, which means we can @Autowire any bean that’s picked up by component scanning into our test.

URL Rewriting With Spring Cloud Gateway

1. Introduction A common use case for the Spring Cloud Gateway is to act as a facade to one or more services, thus offering clients a simpler way to consume them. In this tutorial, we’ll show different ways to customize the exposed APIs by rewriting the URLs before sending the request to the backends. 2. Spring Cloud Gateway Quick Recap The Spring Cloud Gateway project is built on top of the popular Spring Boot 2 and Project Reactor, so it inherits its main treats:

Automatic decoding of request bodies via RequestBodyAdvice

The client encodes or encrypts the request body (e.g. AES encryption, Base64 encoding), and the server decodes the request body after receiving it. This is a very common requirement. Thanks to the RequestBodyAdvice interface provided by spring mvc. We can do this very easily and without modifying any code in the Controller. Practice The client’s request body is encoded using Base64 and the server automatically decodes it via RequestBodyAdvice. This

Monitoring spring boot application logs online via WebSocket

This article will teach you how to monitor spring boot application’s live logs online via websocket. Create a project poml The spring-boot-starter-websocket dependency needs to be added. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 <project xmlns="http://maven.

Use Aop + SpEl to record more detailed request logs

Spring powerful expressions (SpEL) , you can dynamically access the properties of Java objects , call the object’s methods . In the request logging through Aop with SpEL can make the log information more detailed and flexible. This article will not explain the detailed techniques related to spel and aop. If you are not familiar with them, you can visit the official documentation to learn them. Practicing OperationLog Handler methods annotated with @OperationLog will be logged with detailed access logs.

How to Record Request and Response Bodies in Sping Boot Applications

In some scenarios, we need to log every client request and response log (including detailed body information). As you know, streams can only be read once. If we read the request body first in order to log it, then spring’s HttpMessageConverter will throw an exception when it reads the request body again: org.springframework.http.converter. HttpMessageNotReadableException: Required request body is missing: .... The following example demonstrates this situation. The case of HttpMessageNotReadableException Filter for logging 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package io.

Improve file download efficiency with Zero Copy

Zero-Copy The “zero” of zero-copy means that the number of times data is copied between user and kernel states is zero. Traditional data copying (file-to-file, client-to-server, etc.) involves four user-state to kernel-state switches and four copies. two of the four copies between user-state and kernel-state require CPU participation, while two copies between kernel-state and IO devices do not require CPU participation in DMA mode. Zero-copy avoids copying between user and kernel states (2 times) and reduces the switching between user and kernel states twice, so the data transfer efficiency is high.

Dynamically modifying Logger log levels in Spring Boot applications

Spring Boot uses logback as the logging framework by default. logback provides an interface to get logger and modify logger logging levels. Actually actuator also provides endpoint for managing logs. You can check the official documentation for details. LogController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 package io.

Using commons-exec to backup MYSQL database in spring boot application

mysqldump is a database backup program provided by mysql. It dumps the data stored in the database into SQL statements that are output to standard output. Basic Usage: 1 mysqldump -u[username] -p[password] [database] > [sql file] The > symbol is a redirector under linux, redirecting the standard output to a file. mysqldump You can refer to the official documentation for more detailed usage. Use in spring boot applications Start a new subprocess via commons-exec to execute mysqldump to complete the backup.

Get all uploaded files in SpringBoot application

File uploads generally use a request body in multipart/form-data format. The code to handle uploading files in Spring Boot is as follows: 1 2 3 4 5 @PostMapping(path = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public void upload(@RequestParam("file1") MultipartFile file1, @RequestParam("file2") MultipartFile file2) throws IOException, ServletException { // Processing of uploaded files... } The code above the upload method declares 2 parameters (including the form name of the file), representing 2 files. If we don’t know how many files are uploaded and the name of the file form.

Deep Dive Into Saga Transactions With Kafka Streams and Spring Boot

In this article, you will learn how to use Kafka Streams and Spring Boot to perform transactions according to the Saga pattern. To be honest, I was quite surprised by a great deal of attention to my last article about Kafka. I got some questions about streams, transactions, and support for Kafka in Spring Boot. In this article, I’ll try to answer a few of them. I will also show how you can easily set up a cloud-managed Kafka on the Upstash.

A Guide to JUnit5 Lifecycle Methods

In this tutorial, you will learn about JUnit5 Lifecycle methods. A Lifecycle Method is any method that is directly annotated or meta-annotated with @BeforeAll, @AfterAll, @BeforeEach, or @AfterEach. Let’s have a look at these annotations one by one. @BeforeAll When a method is annotated with @BeforeAll it means that the method should be executed before all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class. @BeforeEach When a method is annotated with @BeforeEach it means that the method should be executed before each @Test, @RepeatedTest, @ParameterizedTest, or @TestFactory method in the current class.

Spring Boot Quick Integration With Mybatis Framework

Mybatis is a popular orm framework. It can maintain SQL statements in XML and is very flexible. This is the key for many developers to choose it as an orm framework. Official Website: https://mybatis.org/mybatis-3/ Github: https://github.com/mybatis/mybatis-3 Mvnrepository: https://mvnrepository.com/artifact/org.mybatis/mybatis 1 2 3 4 5 6 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> Using mybatis in spring boot applications is easy. Only a few steps are required. This article focuses on “How to integrate mybatis in spring boot”.