Method of creating a server using spring boot embedded container undertow
Undertow is a web server, so it needs to have the basic characteristics of modern web server, such as servlet, JSP, file server, proxy server, security authentication and so on. Undertow has implemented most functions, and because wildfly has passed the Java ee7 TCK certification, it can be said that undertow is a web server and container certified by servlet 3.1. This article only analyzes the main functions of undertow's backbone process, namely undertow core and undertow servlet.
1. Introduction
Undertow is a very lightweight and high-performance web server from JBoss. Supports blocking and non blocking NiO APIs.
Because it is written in Java, it can be used by JVM based applications in an embedded manner. Even JBoss's wilffly server uses undertow internally to improve server performance.
In this article, we will introduce the main functions of undertow and give the usage code.
2. Why choose undertow?
3. Use undertow
Not to mention, let's use undertow to create a simple web server!
3.1. Maven dependency
Add the following dependencies:
To build a running jar, we also need to add a maven shade plugin. Like this:
The latest version of undertow has been put in Maven central warehouse. Just play.
3.2. Simple server
Just the following lines of code are needed to create a simple web server. The undertow portal uses the builder API.
Here, we use the builder API to bind the server to port 8080. At the same time, we use a lambda expression to configure a handler. You can also do the same without lambda expressions:
The most important thing is the use of HttpHandler API here. This is the most important weapon to customize the undertow application according to our needs.
Here, we add a custom handler. The logic is to make each request have a content type: text / plain response header.
Similarly, if you want each response to return the default text, you can define it as follows:
3.3. Secure access
In most cases, we don't want all users to have access to our server. Usually, only those with legal "passports" can visit. On undertow, we can also implement such a mechanism.
To achieve this, we only need to create an authentication manager, which will check the user information of each request.
Let's implement a custom identitymanager:
Once the authentication mananger is created, we need to create a "territory" to save the user's information.
Here we use authentication mode PRO_ Active means that every request to the server will be actively authenticated through the authentication mechanism specified by us.
If we use authenticationmode CONSTRAINT_ In drive mode, only those requests that are explicitly required to be filtered will go through the authentication mechanism.
Now, we only need to associate the "territory" that stores user information with the authentication manager.
So far, we have created two user instances. Once the server is started, we need to use these two certificates to access it.
3.4. Web Socket
Creating a web socket exchange channel using the websockethttpexchange API of undertow is also very simple.
Next, open a socket communication channel on / importsourceapp path:
Then we can create an HTML page index HTML, and then use the websocket API of JS to connect to this channel.
3.5. File Server
Using undertow, we can also create a file server, which can display the contents of the directory and provide files directly from the directory:
You don't even need to wear any UI to display the directory. Undertow provides you with a page out of the box.
4. Spring Boot Plugin
Spring boot also embeds undertow as the third embedded servlet container after Tomcat and jetty. To use undertow in spring boot, you only need to add the following dependencies:
5. Summary
In this article, we learned about undertow and how to use it to create different types of servers.
The above is the spring boot embedded container undertow introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!