Spring Boot支持Undertow服务器,支持http、https(ssl、tls)双协议,支持双端口

来源:互联网 发布:二当家的官网源码下载 编辑:程序博客网 时间:2024/05/21 11:32

1:Spring boot 项目默认的web服务器为tomcat,故排除tomcat依赖,加入undertow依赖,请看下图:

注:Spring boot版本:1.5.6.RELEASE Tomcat版本:3.3.8
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>    <exclusions>        <exclusion>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-tomcat</artifactId>        </exclusion>    </exclusions></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-undertow</artifactId></dependency>

undertow服务器

2:Spring boot undertow支持http、https(ssl、tls)双协议,支持双端口配置

2.1:application.yml配置,配置http的协议端口为5201,配置https的证书文件路径、密码、端口(7201)
server:  address: 192.168.1.119  port: 7201  context-path: /note1  undertow:    accesslog:      enabled: false  ssl:    key-store: D:/rsakey/loveshare.keystore    key-store-password: 123456http:  port: 5201
2.2:配置参数绑定bean的属性HttpsProperties
package me.loveshare.note1.properties;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;@Data@ConfigurationProperties(prefix = "http")public class HttpProperties {    private Integer port;}
2.3:注解该类HttpsConfiguration,配置Bean对象UndertowEmbeddedServletContainerFactory,进行Undertow初始化
package me.loveshare.note1.configuration;import io.undertow.Undertow;import lombok.extern.slf4j.Slf4j;import me.loveshare.note1.properties.HttpProperties;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Slf4j@Configuration@EnableConfigurationProperties(HttpProperties.class)public class HttpConfiguration {    @Autowired    private HttpProperties properties;    @Bean    public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() {        UndertowEmbeddedServletContainerFactory undertow = new UndertowEmbeddedServletContainerFactory();        undertow.addBuilderCustomizers((Undertow.Builder builder) -> {            builder.addHttpListener(properties.getPort(), "0.0.0.0");        });        log.info("\n*** Undertow http setting successful." + properties.getPort());        return undertow;    }}

3:协议测试

3.1:测试api接口配置
package me.loveshare.note1.api;import lombok.extern.slf4j.Slf4j;import me.loveshare.note1.data.entity.bo.common.JsonResult;import me.loveshare.note1.data.entity.bo.common.JsonResultMethod;import me.loveshare.note1.data.utils.DateUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import java.util.Date;@Slf4j@RestControllerpublic class TestDataApi extends BaseApi {    /**     * 协议测试     */    @ResponseBody    @RequestMapping(value = "test", produces = "application/json; charset=UTF-8", method = {RequestMethod.GET, RequestMethod.POST})    public JsonResult initDbDatasC() {        return JsonResultMethod.code_200("The request completed successfully.", DateUtils.timestamp(new Date()));    }}
3.2:请求参数监控:
package me.loveshare.note1.api;import lombok.extern.slf4j.Slf4j;import me.loveshare.note1.data.utils.NetworkUtils;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestHeader;import javax.servlet.http.HttpServletRequest;@Slf4jpublic class BaseApi {    @ModelAttribute    public void firstMC(HttpServletRequest request, @RequestHeader(value = "uAT", required = false) String uAT) {        request.setAttribute("uAT", uAT);  //此处可转换用户的id        //打印请求日志        printAccess(request);    }    private final void printAccess(HttpServletRequest request) {        StringBuilder su = new StringBuilder();        su.append("\nUser-Access-Args:").append("{");        su.append("\"protocol\":\"").append(request.getProtocol() + "(" + request.getScheme()).append(")\",");        su.append("\"ip\":\"").append(NetworkUtils.getIpAddr(request)).append("\",");        su.append("\"port\":\"").append(NetworkUtils.getPort(request)).append("\",");        su.append("\"method\":\"").append(request.getMethod()).append("\",");        su.append("\"url\":\"").append(NetworkUtils.getCurrentURL(request)).append("\",");        su.append("\"user-agent\":\"").append(NetworkUtils.getUserAgent(request)).append("\",");        su.append("\"uAT\":\"").append(request.getAttribute("uAT")).append("\"}");        log.info(su.toString());    }}
3.3:web请求地址和日志:
http://192.168.1.119:5201/note1/test.jsonhttps://192.168.1.119:7201/note1/test.json
User-Access-Args:{"protocol":"HTTP/1.1(http)","ip":"192.168.1.119","port":"52188","method":"GET","url":"/note1/test.json","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36","uAT":"null"}User-Access-Args:{"protocol":"HTTP/1.1(https)","ip":"192.168.1.119","port":"52197","method":"GET","url":"/note1/test.json","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36","uAT":"null"}

undertow启动日志
GitHub全源代码: https://github.com/loveshareme/spring-boot/tree/master/spring-boot-note1

阅读全文
0 0
原创粉丝点击