Solve the problem of static files in SpringBoot applications garbled

For text resources in the SpringBoot static directory, if they contain Chinese, the Chinese in the content will be garbled when accessed directly through the browser. This is despite the fact that the files themselves are UTF8 encoded. Demo Prepare a static resource file - foo.js The content of the file contains Chinese 1 2 3 4 5 /* SpringBoot中

Running the Spring Boot application as a Linux system service

I’m working on a legacy project today. There is a Spring Boot project deployed on a physical server on the customer’s intranet, and recently, for some reason, the server has been rebooting frequently. It’s a pain to manually start the Spring Boot project after each reboot, so I’m going to configure it to turn on a self-starting service. This was done with the help of systemd. It provides a solution

SpringBoot 2.6.x coping strategies after disabling cyclic dependencies by default

1. Preface SpringBoot 2.6.x does not recommend the use of circular dependencies, which is good news, SpringBoot is gradually guiding developers to write standard code from the bottom up, but also sad news, circular dependencies are too widely used. If you upgrade from a low version to 2.6.x, then the first problem you are likely to encounter is a circular dependency problem. 2. Problem recovery 2.1. Code description The following style of code is more common: both classes have the need to call each other’s methods, so it is easy to write a circular reference.

Spring Security will soon deprecate the configuration class WebSecurityConfigurerAdapter

Anyone who has used WebSecurityConfigurerAdapter knows that it is very important for Spring Security, it manages the whole Spring Security configuration system. But soon this class will be obsolete, you read it right, this class will be marked by @Deprecated in version 5.7 and this class will be removed in the future(#10822). Since this configuration class will be deprecated soon, there must be a transition plan. I’ve actually written an

How Spring Security's built-in filters are maintained

How is the order of built-in filters maintained in Spring Security? I think many developers are interested in this question. In this article, I will discuss this issue with you. HttpSecurity contains a member variable FilterOrderRegistration and this class is a built-in filter registry. As for the role of these filters, not the focus of this article, interested to see the FilterOrderRegistration source code. Order of built-in filters The FilterOrderRegistration

Environment in Spring detailed explanation

Environment represents the environment information for the entire spring application runtime, which contains two key elements. profiles properties profiles The concept of profiles, which I believe we all understand, is most commonly used in different environments to determine different configuration contexts for solutions in the current spring container. For example, for the development environment, test environment, production environment, build different application.properties configuration items, this time we can determine the current spring application context in effect through the property profiles.

Version control for restful api

1. Preface Software iteration is a problem that developers must face, and one issue that is easily overlooked these days is API version control. Not all users are keen on the latest version of software, and business is fluid. So when a new version is released to ensure backward compatibility, API version control is needed. Today we will discuss the common Restful API version control. 2. API Version Control Restful

Mock testing of Spring MVC interfaces

1. Preface Most of the developers contacted in Java development do not pay much attention to testing the interface, resulting in various problems in the docking. Some also use tools such as Postman for testing, although there is no problem in the use of the interface, if the interface increases the permissions testing is more disgusting. So it is recommended to test the interface in unit testing to ensure the

How to add Json Web Token in Swagger2 or Swagger3

1. Preface Swagger 3.0 has been released for some time now, and has been used by more and more projects as a very useful documentation tool. And JWT is also the most commonly used security technology for front-end and back-end separation. So how do you add JWT Token to Swagger 3.0? 2. Adding JWTs in Swagger2 Let’s first review how JWT was added in Swagger2. In Swagger2 we declare the

Spring Cloud 2021.0.1 Released

Spring Cloud 2021.0.1 is released, the first bugfix release of Spring Cloud 2021. Support for 2021.0.0 Smooth Upgrade is now available from the central repository at the following coordinates. maven: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> .

Powerful Configuration Binding Classes in Spring Boot 2.0 - Bindable

1. Preface When developing Spring Boot applications, you can inject Bean into the Spring IoC container based on conditions. For example, if a configuration property exists in the configuration file, then the Bean is injected. The red part of the diagram means that the class tagged with @Configuration can only be injected into Spring IoC if ali.pay.v1.app-id is present in the environment configuration of Spring. The @ConditionalOnProperty in this case

Distributed Objects in Spring Security - SharedObject

1. Preface We previously analyzed the details of the initialization of AuthenticationManager, and there was a piece of code in it that caught the attention of many people. 1 2 ApplicationContext context = http.getSharedObject(ApplicationContext.class); CaptchaAuthenticationProvider captchaAuthenticationProvider = context.getBean("captchaAuthenticationProvider", CaptchaAuthenticationProvider.class); How does the above get the Spring application context object ApplicationContext directly from the HttpSecurity object? And what is the concept of SharedObject? Let’s figure this out today. 2. SharedObject In Spring Security SharedObject is neither an object nor an interface, but a generic term for a class of “sharable” objects.

SpringBoot + FFmpeg to implement a simple M3U8 slicing transcoding system

The client uploads the video to the server, the server slices the video, AES encrypts it, and returns the m3u8 index file, cover and other information. It can be played online. The server can do some simple processing of the video, such as cropping, cover interception time. Video folder layout 1 2 3 4 5 6 mymovie # The folder name is the video title |-index.m3u8 #the main m3u8 file,

Get the process id of the SpringBoot application after it starts

After springboot starts, its process id needs to be recorded and written to a disk file. The bash script can easily stop the program by PID. ApplicationPidFileWriter A listener provided by springboot is very simple. Just add the listener instance to the SpringApplication before starting the springboot application. 1 2 3 SpringApplication springApplication = new SpringApplication(MyApplication.class); springApplication.addListeners(new ApplicationPidFileWriter()); // Pid Listener springApplication.run(args); Configure the write file for the process ID.

Monitoring Applications with Prometheus + Grafana + Spring Boot Actuator

Monitoring is often critical in enterprise-level applications. Monitoring can help us prevent failures, predict trends, alert when thresholds are reached, and provide more information to troubleshoot production problems. If we don’t know how our program is running, it will take more time to troubleshoot when there is an accident in the online system. If we can monitor in advance, we can prepare early so that we don’t get confused after

A Study of Graceful Shutdown for Spring Boot Applications

Recently, I took a look at the restart scripts of the project and found that Ops has been using kill-9<pid> to restart springboot embedded tomcat, in fact, we almost unanimously agree that kill-9<pid> is a more violent way, but few people can analyze what problems it will bring. This article mainly records my own thinking process. What is the difference between kill -9 and kill -15? In the old days,

A brief analysis of the circular reference problem of returning objects in SpringMVC

Problem Discovery Today this topic is still relatively easy, and many of you may have encountered this problem. The @RestController, @ResponseBody and other annotations are the ones we deal with most when writing web applications, and we often have the need to return an object to the client, which SpringMVC helps us serialize into JSON objects. And today I want to share the topic is not something profound, it is

Be Aware of Security Issues Raised by Spring Boot Actuator

Recently, we have been particularly disturbed by various security vulnerabilities, and we receive dozens of emails a week from security teams scanning for vulnerabilities. One of these vulnerabilities is easy to overlook, but has a very wide impact and is extremely harmful. You shouldn’t be surprised when I say its name, it’s the Spring Boot Actuator. Before writing this article, I did a little survey with my friends asking them about their knowledge of the Spring Boot Actuator and the results were amazingly consistent.

How to unit test Controllers in Spring MVC

Unit testing of Controllers is a capability natively supported by the Spring Framework, which simulates an HTTP client initiating a request to a service address and allows testing of the interface without the use of external tools such as Postman. Specifically, the implementation is provided by the spring-test module of the Spring Framework, see MockMvc. The following section details how to use the MockMvc test framework to implement unit tests

Using the CommandLineRunner hook interface

This article briefly talks about the hook interfaces CommandLineRunner and ApplicationRunner, which are sometimes referred to as Runner in the following. Runner callback timing Refer to the source code of the org.springframework.boot.SpringApplication#run() method. You can know the timing of the callbacks for CommandLineRunner and ApplicationRunner. Before all CommandLineRunner and ApplicationRunner callbacks, the following steps have been ensured to be executed. Environment built-in variables are created and properties are populated. Banner